![](/img/trans.png)
[英]Qt QGraphicsProxyWidget interaction with QGraphicsScene
[英]Qt Update QPixmapItem in QGraphicsScene
我想在主窗口中有一个图像,并在图像顶部放置按钮。 我正在使用视图,场景和像素图项目,但是当我尝试在插槽功能中更改像素图项目时,我崩溃了。
这是我所拥有的一个例子:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
//scene, view, pixitem and button are private variables in header
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(scene);
QGraphicsPixmapItem *pixitem = new QGraphicsPixmapItem;
pixitem->setPixmap(QPixmap(":/img/this.png"));
scene->addItem(pixitem);
QPushButton *button = new QPushButton(view);
this->connect(button,SIGNAL(clicked()),this,SLOT(slot()));
this->setCentralWidget(view);
}
void MainWindow:slot()
{
pixitem->setPixmap(QPixmap(":/img/that.png"));
}
可能重复,但是此解决方案对我不起作用: Qt更新QGraphicsPixmapItem的Pixmap
在构造函数中,创建pixmap项:
QGraphicsPixmapItem *pixitem = new QGraphicsPixmapItem;
然后在广告位中致电
pixitem->setPixmap(QPixmap(":/img/that.png"));
为了成功编译,您必须具有QGraphicsPixmap *作为该类的成员。 因此,我得出结论,由于在构造函数中的指针上创建了该项,因此这是NULL或至少无效。
假设这是正确的,并且您确实在类的标题中声明了pixitem,请将构造函数中的创建更改为:-
pixitem = new QGraphicsPixmapItem;
您已经遮蔽了成员变量pixitem
因为您正在使用
QGraphicsPixmapItem *pixitem = new QGraphicsPixmapItem;
添加到场景的pixitem
将由局部变量而不是成员变量引用。 只需更改为
pixitem = new QGraphicsPixmapItem;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.