为防止不断地addItem导致内存增长,建议在初始化时new Item、scene->addItem。在合适的地方scene->removeItem(或scene->clear)或者item->setVisible。
h头文件中
#include <QGraphicsView>
QGraphicsView* view;
QGraphicsScene* scene; QGraphicsPixmapItem* m_pix = nullptr;
cpp构造函数中
view = new QGraphicsView; centralLayout->addWidget(view); view->setBackgroundRole(QPalette::Dark); scene = new QGraphicsScene; view->setScene(scene); m_pix = new QGraphicsPixmapItem; scene->addItem(m_pix);
cpp显示图像的函数中
if (!m_qImg.isNull()) { m_pix->setPixmap(QPixmap::fromImage(m_qImg)); }
标签:Qt,QGraphicsPixmapItem,addItem,scene,pix,图像,new,view From: https://www.cnblogs.com/xixixing/p/18102068