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
A는 static
기능. 또는 QPixamp scaled()
다음과 같이 오버로드 할 수 있습니다 .
QPixmap PiecePixmap::scaled()
{
int intSize{ toInt(m_size) };
return scaled(intSize, intSize);
}
그러나 나는 여전히 실제 MyPixmap
객체를 사용하지 않습니다 .
클래스 자체 내에서 픽스맵의 크기를 조정할 수 있습니까, 아니면 scaled
특정 조각이 내부적으로 하드 코딩 된 크기가되도록 내 최선의 방법으로 제안한대로 오버로딩 됩니까?
참고 :이 게시물을 많이 편집 했으므로 Monopoly 예제를 보려면 편집 기록을 확인하십시오. 현재 질문 코드는 데모 용이므로 테스트되지 않았습니다.
으로 이고르 Tandetnik가 언급 한, 그것은 호출하여 즉시 조정 픽스맵을 만들 수 있어요 scaled
방법을 다음과 같이 복사 생성자에 반환 값을 전달 :
MyPixmap::MyPixmap(QString path, int height, int length)
: QPixmap{ QPixmap{ path }.scaled(size, size) }
{
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다