Qt顯示圖像之QGraphicsPixmapItem

爲防止不斷地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));
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章