从QGraphicsScene删除Qpixmap [英] Removing Qpixmap from QGraphicsScene

查看:1571
本文介绍了从QGraphicsScene删除Qpixmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理一个问题,当处理QGraphicsScene和QPixmap。
我按顺序显示相机捕获的帧。 QTimer对象每100ms调用一次updateSingleView()函数。这是我的内在函数:

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显示相机的视图,但不幸的是有内存泄漏, scenes.at(0) - > addPixmap(QPixmap :: fromImage(Img)); 我认为 removeItem 函数应该销毁旧的QPixmap,但显然它不是。您是否知道为什么会发生泄漏以及如何解决?

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?

推荐答案

如建议

您需要在removeItem行之后删除该项。

you need to delete the item after removeItem line.

QPointer _item = scenes .at(0) - > items()。at(0);
scenes.at(0) - > removeItem(_item);
delete _item;

QPointer _item = scenes.at(0)->items().at(0); scenes.at(0)->removeItem( _item ); delete _item;

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

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

.....

这篇关于从QGraphicsScene删除Qpixmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆