Qt更新Pixmap的QGraphicsPixmapItem [英] Qt Update Pixmap of QGraphicsPixmapItem
问题描述
我使用QGraphicsPixmapItem在显示器上显示一个图像。现在,我想能够在运行中更新此映像,但我似乎遇到了一些问题。
I am using the QGraphicsPixmapItem to show an image on the display. Now, I want to be able to update this image on the fly, but I seem to be running into some issues.
这是头 file:
class Enemy_View : public QGraphicsPixmapItem
{
public:
Enemy_View(QGraphicsScene &myScene);
void defeat();
private:
QGraphicsScene &scene;
QPixmap image;
}
这里是 cpp 文件
Enemy_View::Enemy_View(QGraphicsScene &myScene):
image{":/images/alive.png"}, scene(myScene)
{
QGraphicsPixmapItem *enemyImage = scene.addPixmap(image.scaledToWidth(20));
enemyImage->setPos(20, 20);
this->defeat();
}
void Enemy_View::defeat(void)
{
image.load(":/images/dead.png");
this->setPixmap(image);
this->update();
}
这样的想法是我想能够调用 defeat
方法,然后编辑一些属性,最终更改图像。但是,我现在做的不工作。 alive.png
图片显示,但未更新为 dead.png
。
SO the idea is that I want to be able to call the defeat
method on my object, that then edits some attributes and eventually changes the image. However, what I am doing now does not work. The alive.png
image does display, but does not get updated to the dead.png
one.
正如Marek RI所提到的似乎是复制很多内置的功能。
As mentioned by Marek R I appear to be replicating a lot of built-in functionality. I tried to clean this up but now nothing is showing up on the scene anymore.
.h 档案
class Enemy_View : public QGraphicsPixmapItem
{
public:
Enemy_View(QGraphicsScene &myScene);
void defeat();
private:
QGraphicsScene &scene;
/* Extra vars */
};
.cpp 档案
Enemy_View::Enemy_View(QGraphicsScene &myScene):
scene(myScene)
{
/* This part would seem ideal but doesn't work */
this->setPixmap(QPixmap(":/images/alive.png").scaledToWidth(10));
this->setPos(10, 10);
scene.addItem(this);
/* This part does render the images */
auto *thisEl = scene.addPixmap(QPixmap(":/images/Jackskellington.png").scaledToWidth(10));
thisEl->setPos(10, 10);
scene.addItem(this);
this->defeat();
}
void Enemy_View::defeat(void)
{
this->setPixmap(QPixmap(":/images/dead.png"));
}
所以我删除了 QPixmap
,但我不知道是否可以删除 QGraphicsScene
。在我的 cpp
- 文件中,你可以看到我有两个版本的构造函数。第一部分,使用这
看起来像一个理想的解决方案,但不显示在屏幕上的图像(即使它编译没有错误)。第二个版本用 thisEl
会渲染它。
So I removed the QPixmap
, but I'm not sure whether I can remove the QGraphicsScene
. In my cpp
-file you can see I have two versions of the constructor now. The first part, using the this
seems like an ideal solution, but does not display the image on the screen (even though it does compile without errors). The second version with thisEl
does render it. What am I doing wrong with the first version?
推荐答案
为什么FGS是子类化 QGraphicsPixmapItem
? QGraphicsPixmapItem
具有您需要的所有功能。你添加的那些新字段什么都不做,他们只尝试已经存在的复制功能(但是这个实现不起作用)。
Why FGS you are subclassing QGraphicsPixmapItem
? QGraphicsPixmapItem
has all functionality you need. And those new fields you have added does nothing, they only try replicate functionality which is already there (but with this implementation it does nothing).
这假设是:
QPixmp image(":/images/alive.png");
QGraphicsPixmapItem *enemyItem = scene.addPixmap(image.scaledToWidth(20));
enemyItem->setPos(20, 20);
// and after something dies
QPixmap dieImage(":/images/dead.png");
enemyItem->setPixmap(dieImage);
这篇关于Qt更新Pixmap的QGraphicsPixmapItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!