我想在我的主窗口中有一个图像,按钮位于图像的顶部。我正在使用 View 、场景和像素图项,但是当我尝试更改槽函数中的像素图项时,我崩溃了。
这是我的例子:
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 Update Pixmap of QGraphicsPixmapItem
最佳答案
你已经隐藏了成员变量pixitem
因为你正在使用
QGraphicsPixmapItem *pixitem = new QGraphicsPixmapItem;
添加到场景中的pixitem
将被局部变量引用,而不是被成员变量引用。只需更改为
pixitem = new QGraphicsPixmapItem;
关于c++ - Qt 更新 QGraphicsScene 中的 QPixmapItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30571122/