首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除列出的"QGraphicsPathItem“对象来控制进程内存的使用?

如何删除列出的"QGraphicsPathItem“对象来控制进程内存的使用?
EN

Stack Overflow用户
提问于 2018-10-13 20:00:42
回答 1查看 179关注 0票数 1

我想在QGraphicsView中画一些路径。我想要管理溺水的道路。因此,在path_ploter(double)方法中,我绘制一个路径并将对象的指针传递给PathItemList

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

在头文件中:

代码语言:javascript
复制
#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();
};

在源文件中:

代码语言:javascript
复制
#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);

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-13 20:45:41

您的代码有几个错误:

  • 为什么要创建一个QPainterPath指针?没有必要永久拥有该信息,因为addPath()获取该值的副本。
  • 为什么要添加两个具有相同myPath的项目?,只要一个就够了,并将其存储在列表中。
  • 为什么要创建2 pathItem?用addPath()创建一个addPath(),用new QGraphicsPathItem创建另一个,第二个没有必要。
  • 为什么在for-循环中使用scene->addItem(PathItemList.back());?这是没有意义的。

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

考虑到上述情况,守则应如下:

代码语言:javascript
复制
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);
    }
}

最后,建议您使用新的连接语法:

代码语言:javascript
复制
connect(btn_Ok, &QAbstractButton::clicked, this, &QtGuiApplication1::plot_fn);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52796814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档