qt - QGraphicsView additem() 越来越大
问题描述
我有一个信号会触发 QT 上的以下插槽:
void MainWindow::stream_frame_changed(bool change, const QImage& image)
{
if (stream == true && change == true)
{
QPixmap aux = QPixmap::fromImage(image);
item->setPixmap(aux);
ui->graphicsView->scene()->addItem(item);
}
}
该信号每 100 毫秒发出一次。我注意到存在内存泄漏,这是有道理的,因为我在场景中添加了许多项目而从未删除它们。
我不知道如何删除以前的项目。如果我执行 clear() 函数,窗口会关闭,所以它显然不是一个选项。
解决方案
您必须重用现有项目,而不是创建新项目:
* .cpp
在构造函数上:
ui->graphicsView->scene()->addItem(item);
和
void MainWindow::stream_frame_changed(bool change, const QImage& image)
{
if (stream == true && change == true)
{
QPixmap aux = QPixmap::fromImage(image);
item->setPixmap(aux);
}
}
推荐阅读
- ruby - 检查 path 是否是目录路径(它可能不存在)
- excel - 每次从两个日期和名称之间向列表框添加值
- c++ - enable_if 在返回类型中没有 decltype 失败
- javascript - 我卡在firebase cloud firestore上的上传图像中。谁能帮我 。?
- javascript - 使用回调循环函数并等待结果
- android - Retrofit2 Android Studio Emulator - json数组响应中的输入结束
- firebase - Application.kt:未解决的参考:firebasemessaging
- gnu-parallel - 来自远程主机的 GNU 并行日志
- c# - Tesseract 仅在 IIS 中的应用程序池标识设置为本地系统时才有效
- python - 我想要将字符串从 Python 发送到 Node-RED