从 QGraphicsScene 中删除 Qpixmap
在处理 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 正在显示相机的视图,但不幸的是,在调用 scenes.at(0)->addPixmap(QPixmap::fromImage(Img));
时出现内存泄漏认为 removeItem
函数应该销毁旧的 QPixmap,但显然事实并非如此。您知道为什么会出现泄漏以及如何解决吗?
I enconutered a problem, when dealing with QGraphicsScene and QPixmap.
I am sequentially displaying frames, captured by the camera. QTimer object is calling updateSingleView() function every 100ms. That is my inner function:
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 is displaying the camera's view but unfortunatelly there is a memory leak, when calling scenes.at(0)->addPixmap(QPixmap::fromImage(Img));
I thought that removeItem
function should destroy the old QPixmap, but apparently its not. Do you know why the leak occurs and how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 Qt 文档:
因此,您需要使用
delete
手动删除该项目。http://doc.trolltech.com/4.7/qgraphicsscene.html#removeItem
From the Qt documentation:
Hence you need to delete the item using
delete
manually.http://doc.trolltech.com/4.7/qgraphicsscene.html#removeItem
根据建议,
您需要在removeItem行之后删除该项目。
即
QPointer _item = Scenes.at(0)->items().at(0);
scene.at(0)->removeItem( _item );
删除_item;
scene.at(0)->addPixmap(QPixmap::fromImage(Img));
……
As suggested
you need to delete the item after removeItem line.
i.e
QPointer _item = scenes.at(0)->items().at(0);
scenes.at(0)->removeItem( _item );
delete _item;
scenes.at(0)->addPixmap(QPixmap::fromImage(Img));
.....