c++ - 如何删除列出的 "QGraphicsPathItem"对象以控制进程内存使用?

标签 c++ qt qgraphicsview qgraphicsitem qlist

我想在QGraphicsView 中绘制一些路径。我想管理淹没路径。所以在 path_ploter(double) 方法中,我绘制了一条路径并将对象的指针传递给 PathItemList

我重绘了 plot_fn() 槽中的路径。我将这个插槽连接到一个按钮。当我按下 plot 按钮时,尽管在重绘路径之前删除了列表中的所有路径项指针,但进程内存使用量增加了。如果我在创建后立即删除列表中的指针,例如 path_ploter(double) 方法中的 //delete PathItemList.back(); ,进程使用内存不会'增加。在这种情况下,不会显示路径。为什么当我删除 plot 槽中的指针时内存使用量会增加?我该如何控制它?

在头文件中:

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include <QGraphicsView>
#include <QGraphicsPathItem>
#include <QGraphicsLineItem>
#include <QtWidgets/QPushButton>

class QtGuiApplication1 : public QMainWindow
{
    Q_OBJECT

public:
    QtGuiApplication1(QWidget *parent = Q_NULLPTR);

private:
    Ui::QtGuiApplication1Class ui;
    QGraphicsView* qGraph;
    QGraphicsScene* scene;
    QGraphicsPathItem* pathItem = new QGraphicsPathItem();
    QList<QGraphicsPathItem *>PathItemList;
    void path_ploter(double);
private slots:
    void plot_fn();
};

在源文件中:

#include "QtGuiApplication1.h"

QtGuiApplication1::QtGuiApplication1(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    //////////////
    qGraph = new QGraphicsView(ui.centralWidget);
    qGraph->setGeometry(QRect(0, 0, 300, 300));
    scene = new QGraphicsScene(qGraph);
    scene->setSceneRect(0, 0, 300, 300);
    qGraph->setScene(scene);    
    qGraph->show();
    ////////////
    QPushButton* btn_Ok = new QPushButton(ui.centralWidget);
    btn_Ok->setObjectName(QStringLiteral("btn_Ok"));
    btn_Ok->setGeometry(QRect(300, 300, 75, 23));
    btn_Ok->setText("Plot");
    connect(btn_Ok, SIGNAL(clicked()), this, SLOT(plot_fn()));
    //plot_fn();
    //path_ploter(1);
}

void QtGuiApplication1::path_ploter(double alpha)
{
    QPainterPath* myPath = new QPainterPath();
    QPolygon pol;

    QPen graphPen;
    graphPen.setColor(QColor(255, 0, 0, 255));
    graphPen.setWidth(2);


    QPoint pos;

    for (size_t i = 0; i < 100; i++)
    {

        pos.setX(i);
        pos.setY(i*alpha);
        pol.append(pos);
    }

    myPath->addPolygon(pol);
    pol.clear();


    pathItem = scene->addPath(*myPath, graphPen);
    PathItemList << pathItem;
    //delete PathItemList.back();
    pathItem = new QGraphicsPathItem();

}

void QtGuiApplication1::plot_fn()
{

    for (size_t i = 0; i < PathItemList.size(); i++)
    {
        scene->removeItem(PathItemList.at(i));
    }
    qDeleteAll(PathItemList.begin(), PathItemList.end());
    PathItemList.clear();

    for (size_t i = 0; i < 3; i++)
    {
        path_ploter(i+1);

    }

}

最佳答案

您的代码有几个错误:

  • 为什么要创建一个QPainterPath 指针?没有必要永久拥有该信息,因为 addPath() 会获取该值的拷贝。

  • 为什么要添加2个myPath相同的项?一个就够了,存入列表。

  • 为什么要创建 2 个 pathItem?使用 addPath() 创建一个 pathItem,使用 new QGraphicsPathItem 创建另一个,第二个不是必需的。

  • 为什么要在 for 循环中使用 scene->addItem(PathItemList.back());?这没有意义。

请记住,当您创建指针时,您的任务是确保在应用程序关闭之前清除内存。

考虑到以上情况,代码应该是这样的:

void QtGuiApplication1::path_ploter(double alpha)
{
    QPainterPath myPath;
    QPolygonF pol;
    QPen graphPen;
    graphPen.setColor(QColor(255, 0, 0, 255));
    graphPen.setWidth(2);
    for (size_t i = 0; i < 100; i++){
        pol.append(QPointF(i, i*alpha));
    }

    myPath.addPolygon(pol);
    QGraphicsPathItem *pathItem = scene->addPath(myPath, graphPen);
    PathItemList << pathItem;
}

void QtGuiApplication1::plot_fn()
{
    // Remove the item from the scene
    for(QGraphicsPathItem * pathItem: PathItemList){
        scene->removeItem(pathItem);
    }
    // Delete the memory pointed by the pointers
    qDeleteAll(PathItemList.begin(), PathItemList.end());
    // Clean the container
    PathItemList.clear();
    for (size_t i = 0; i < 3; i++)
    {
        path_ploter(i+1);
    }
}

最后,推荐大家使用新的连接语法:

connect(btn_Ok, &QAbstractButton::clicked, this, &QtGuiApplication1::plot_fn);

关于c++ - 如何删除列出的 "QGraphicsPathItem"对象以控制进程内存使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52796814/

相关文章:

c++ - 对于结构中的数组,我可以期待哪些对齐保证?

c++ - QLibrary - 导入一个类

c++ - Vim C++(Qt,C)开发强大的插件

c++ - 使用 glTextCoordPointer 贴花纹理

python - 如何在 QGraphicsView 中制作 2 层?

c++ - 通过 IShellFolder 接口(interface)枚举打印机文件夹导致堆分配泄漏

c++ - std::sqrt 与 C++ 中的 sqrt 相同吗

c++ - std::string::reserve() 和 std::string::clear() 难题

c++ - 子类化 QGraphicsView,自定义鼠标事件

python - pyqt在Qgraphicsscene中添加矩形