2010-11-24 59 views
1

在處理QGraphicsScene和QPixmap時,我解決了一個問題。 我順序顯示由相機捕獲的幀。 QTimer對象每100ms調用一次updateSingleView()函數。這是我的內部函數:從QGraphicsScene中刪除Qpixmap

void CCIGui::updateSingleView() 
{ 

    unsigned char *const img = PGRSystem->SnapShot(); 

    QImage Img(img, 1024, 768, QImage::Format_RGB888); 

    scenes.at(0)->removeItem(scenes.at(0)->items().at(0)); 
    scenes.at(0)->addPixmap(QPixmap::fromImage(Img)); 

    ui_camViews.at(0).graphicsView->setScene(scenes.at(0)); 

    delete [] img; 
} 

的GUI顯示相機的看法,但可惜的是有內存泄漏,呼籲scenes.at(0)->addPixmap(QPixmap::fromImage(Img));當我以爲removeItem函數應該摧毀舊的QPixmap,但顯然它不是。你知道爲什麼會發生泄漏以及如何解決它?

+0

什麼版本的Qt?我在4.6中遇到了一些麻煩。?在Windows上。 – sje397 2010-11-24 09:12:10

+0

我正在使用Qt版本4.6.3。 – Marcin 2010-11-24 09:24:34

回答

1

至於建議

您需要刪除的removeItem行之後的項目。

QPointer _item = scenes.at(0) - >項()在(0)。 scenes.at(0) - > removeItem(_item); 刪除_item; (0) - > addPixmap(QPixmap :: fromImage(Img));} addPixmap(QPixmap :: fromImage(Img));

.....

2

從Qt文檔:

無效QGraphicsScene ::的removeItem(*的QGraphicsItem項目)

刪除項項目,並從現場的所有兒童。項目的所有權被傳遞給調用者(即,QGraphicsScene在銷燬時不再刪除項目)。

另請參閱addItem()。

因此,您需要手動刪除使用delete的項目。

http://doc.trolltech.com/4.7/qgraphicsscene.html#removeItem