我可以为 QPixmap 派生 class 嵌入缩放方法吗?
Can I embed the scaled method for a QPixmap derived class?
我希望每个 QPixmap
派生对象在创建时都具有所需的大小。假设我有这个虚构的 class:
#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
对象。
是否可以在 class 本身内部缩放像素图,或者按照我最好的建议重载 scaled
以强制某个片段具有内部硬编码大小?
注意:我对这个 post 进行了大量编辑,因此要查看大富翁示例,请查看编辑历史记录。目前的问题代码还没有经过测试,因为它只是为了演示目的。
正如 Igor Tandetnik 所述,可以通过调用 scaled
方法并将其 return 值传递给复制构造函数来创建立即缩放的像素图如下:
MyPixmap::MyPixmap(QString path, int height, int length)
: QPixmap{ QPixmap{ path }.scaled(size, size) }
{
}
我希望每个 QPixmap
派生对象在创建时都具有所需的大小。假设我有这个虚构的 class:
#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
对象。
是否可以在 class 本身内部缩放像素图,或者按照我最好的建议重载 scaled
以强制某个片段具有内部硬编码大小?
注意:我对这个 post 进行了大量编辑,因此要查看大富翁示例,请查看编辑历史记录。目前的问题代码还没有经过测试,因为它只是为了演示目的。
正如 Igor Tandetnik 所述,可以通过调用 scaled
方法并将其 return 值传递给复制构造函数来创建立即缩放的像素图如下:
MyPixmap::MyPixmap(QString path, int height, int length)
: QPixmap{ QPixmap{ path }.scaled(size, size) }
{
}