是否可以为 QPixmap 派生类嵌入缩放方法?
Can I embed the scaled method for a QPixmap derived class?
我希望每个QPixmap
派生对象在创建时具有所需的大小。假设我有这个虚构的类:
#pragma once
#include <QPixmap>
#include <QString>
class MyPixmap : public QPixmap
{
public:
enum class Size { icon, veryVerySmall, medium, large };
static int toInt(Size size);
MyPixmap (QString fileName, Size size);
const QString& getPath();
private:
QString m_fileName; //
Size m_size;
};
#include "MyPixmap.h"
#include "MyGraphicsScene.h"
int MyPixmap::toInt(Size size)
{
switch (size)
{
case MyPixmap::Size::icon:
return 20;
case MyPixmap::Size::veryVerySmall:
return MyGraphicsScene::size / 20;
case MyPixmap::Size::medium:
return MyGraphicsScene::size / 32;
case MyPixmap::Size::large:
return MyGraphicsScene::size / 40;
default:
throw std::exception{ "Unreachable code reached" };
}
}
MyPixmap::MyPixmap(QString fileName, Size /* size*/)
:
m_fileName{ fileName }
{
load("PATH/TO/FOLDER/" + fileName + ".png");
// scaled(size, size);
}
const QString& MyPixmap::getPath()
{
return m_piece;
}
所以我可以执行以下代码:
#include "MyPixmap.h"
int main()
{
MyPixmap myPixmap{ "Butterfly", MyPixmap::Size::veryVerySmall };
// pass myPixmap to various functions and objects
return 0;
}
在构造函数内部缩放将不起作用,因为scaled
是一个static
函数。或者,我可以重载QPixamp scaled()
,如下所示:
QPixmap PiecePixmap::scaled()
{
int intSize{ toInt(m_size) };
return scaled(intSize, intSize);
}
但是,我仍然没有使用实际的MyPixmap
对象。
是否可以在类本身内部缩放像素图,或者重载scaled
我建议的最佳方法是强制某个片段具有内部硬编码大小?
注意:我大量编辑了这篇文章,所以要查看大富翁示例,请查看编辑历史记录。当前的问题代码尚未经过测试,因为它仅用于演示目的。
正如Igor Tandetnik所说,可以通过调用scaled
方法并将其返回值传递给复制构造函数来创建立即缩放的 pixmap,如下所示:
MyPixmap::MyPixmap(QString path, int height, int length)
: QPixmap{ QPixmap{ path }.scaled(size, size) }
{
}
相关文章:
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 计算缩放多边形的比例,得到给定的多边形面积
- QwtPlot具有相等的轴和自动缩放
- 如何在directx/c++中进行平移/缩放操作
- 在Qt中实现无限可缩放的画布
- 是否可以为 QPixmap 派生类嵌入缩放方法?
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何在OpenGL(GLFW,很高兴)中进行2D缩放?
- 改变或缩放两个正态分布以具有特定的相关系数
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在OpenGL中正确旋转和缩放对象?
- 仅当类型为 std::complex 时,才进行缩放
- 将 VS Code 用于跨平台可缩放C++项目
- 根据帧速率缩放/缩小数字
- 在 c++ 中缩放浮点值
- 在Qt5中缩放高DPI图像的最佳方法是什么?
- 防止将临时传递为常量引用的方法,该方法比删除 r 值重载更好地缩放
- 如何仅使用数学方法旋转16x16位图数组的内容(无缩放,只需剪切角)
- 在direct3D9中有没有一种方法可以通过最近的邻居来缩放精灵