Qt中如下代码加载图片是否会造成内存泄漏

dianyancao 2013-06-23 06:21:38
结束函数on_pushButton_clicked()调用后,graphicsView是否持有子对象scene的指针,scene持有子对象image的指针?
void MainWindow::on_pushButton_clicked()
{
QGraphicsScene* scene=new QGraphicsScene;
QPixmap* image=new QPixmap("C:/test/2.bmp");
scene->addPixmap(*image);
ui->graphicsView->setScene(scene);
ui->graphicsView->resize(image->width() + 10, image->height() + 10);
ui->graphicsView->show();

//delete scene;
//delete image;
}
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这里的image完全可以使用局部变量
Inhibitory 2013-06-23
  • 打赏
  • 举报
回复
QPixmap* image=new QPixmap("C:/test/2.bmp"); // delete image需要 delete scene不需要
dianyancao 2013-06-23
  • 打赏
  • 举报
回复
的确被析构了。
// Remove the previously assigned scene.
    if (d->scene) {
        disconnect(d->scene, SIGNAL(changed(QList<QRectF>)),
                   this, SLOT(updateScene(QList<QRectF>)));
        disconnect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
                   this, SLOT(updateSceneRect(QRectF)));
        d->scene->d_func()->removeView(this);
        d->connectedToScene = false;

        if (isActiveWindow() && isVisible()) {
            QEvent windowDeactivate(QEvent::WindowDeactivate);
            QApplication::sendEvent(d->scene, &windowDeactivate);
        }
        if(hasFocus())
            d->scene->clearFocus();
    }
Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n)
{
    if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v);
    else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T();
}
dianyancao 2013-06-23
  • 打赏
  • 举报
回复
如下函数,执行多次,是不是每次执行ui->graphicsView->setScene(scene)时,会delete之前的scene,然后再绑定新的scene?
void MainWindow::on_pushButton_clicked()
{
    QGraphicsScene* scene=new QGraphicsScene;
    QPixmap image("C:/test/2.bmp");
    scene->addPixmap(image);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->resize(image.width() + 10, image.height() + 10);
    ui->graphicsView->show();
}
jdwx 2013-06-23
  • 打赏
  • 举报
回复
引用 2 楼 heksn 的回复:
这里的image完全可以使用局部变量
+1

16,214

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧