我在处理 QGraphicsScene 和 QPixmap 时遇到了一个问题。 我正在按顺序显示相机捕获的帧。 QTimer 对象每 100 毫秒调用一次 updateSingleView() 函数。这是我的内部功能:
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 正在显示相机的 View ,但不幸的是,在调用 scenes.at(0)->addPixmap(QPixmap::fromImage(Img));
时出现内存泄漏。我以为removeItem
函数应该销毁旧的 QPixmap,但显然不是。您知道为什么会发生泄漏以及如何解决吗?
最佳答案
来自 Qt 文档:
void QGraphicsScene::removeItem ( QGraphicsItem * item )
Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).
See also addItem().
因此您需要手动使用delete
删除项目。
关于c++ - 从 QGraphicsScene 移除 Qpixmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265046/