c++ - 从 QGraphicsScene 移除 Qpixmap

标签 c++ qt qgraphicsview

我在处理 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 删除项目。

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

关于c++ - 从 QGraphicsScene 移除 Qpixmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265046/

相关文章:

c++ - Qt 等待用户选择 QGraphicsScene 中的项目

c++ - 在模板中表达左移或右移的优雅方式

c++ - 使用 C 类将华氏度转换为摄氏度

C++ 为调试目的创建 SIGSEGV

c++ - 在Qt中划分应用程序屏幕

c++ - 如何从计算线程到GUI线程获取结果-我需要QSharedMemory吗?

C++ 禁用忙碌/等待光标

mysql - QSqlRecord : Access numeric value from SQLite Table in QT, 结果为空

Qt QGraphicsView 不显示场景

python - 在 QtCore.Qt.CrossPattern 上设置图案颜色和线条粗细