c++ - Qt 更新 QGraphicsScene 中的 QPixmapItem

标签 c++ qt

我想在我的主窗口中有一个图像,按钮位于图像的顶部。我正在使用 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/

相关文章:

c++ - 从 QSqlTableModel 获取数据并放入 QVector<QPointF> 的最快方法

c++ - 在 C++ 中一次读取一个文件

c++ - 函数指针作为 SLOT 参数

c++ - 线程可连接和分离之间的区别?

c++ - 英语和阿拉伯语混合字符串 Qt 排序不正确

c++ - 将变量从 C++ 公开到 qml

Qt QML Listview positionViewAtIndex 不工作

c++ - 如何使用 UTF-8 编码将 LPWSTR 转换为 char *

qt - 如何将 Windows 桌面应用程序转换为 Mac 桌面应用程序?

时间:2019-03-08 标签:c++typeidoperator