gpt4 book ai didi

c++ - 我可以为QPixmap派生类嵌入缩放方法吗?

转载 作者:行者123 更新时间:2023-12-02 10:13:17 25 4
gpt4 key购买 nike

我想在创建时以所需的大小包含每个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;
}
由于 scaledstatic函数,因此无法在构造函数中进行缩放。另外,我可以像这样重载 QPixamp scaled():
QPixmap PiecePixmap::scaled()
{
int intSize{ toInt(m_size) };
return scaled(intSize, intSize);
}
但是,那时我仍然没有使用实际的 MyPixmap对象。
是否可以在类本身内部缩放像素图,还是按我的最佳拍摄建议重载 scaled来使某个片段具有内部硬编码的大小?
注意:我对这篇文章进行了大量编辑,因此要查看“大富翁”示例,请检查编辑历史记录。当前的问题代码尚未经过测试,因为它仅用于演示目的。

最佳答案

正如Igor Tandetnik所述,可以通过调用scaled方法并将其返回值传递给副本构造函数,来创建立即缩放的像素图,如下所示:

MyPixmap::MyPixmap(QString path, int height, int length)
: QPixmap{ QPixmap{ path }.scaled(size, size) }
{
}

关于c++ - 我可以为QPixmap派生类嵌入缩放方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62742395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com