我想用C ++开发自己的信号范围。所以我用qt就是为了这个目的。 我在ui中添加了一个graphicsView和一个按钮。在连接方法按下按钮我更新了graphicsView(最后我将这个方法传递给一个线程)。 每次按下按钮,尽管删除指针,内存的使用量也会增加。我应该如何控制它?
我在vs15诊断工具中检查内存使用情况。
c ++头文件:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication.h"
#include <QGraphicsScene>
#include <QGraphicsPathItem>
class QtGuiApplication : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplicationClass ui;
QGraphicsScene* scene = new QGraphicsScene();
QPolygon pol;
QGraphicsPathItem* pathItem = new QGraphicsPathItem();
int index_ = 0; // graph offset
QPen* myPen = new QPen(Qt::red, 2);
private slots:
void btn_Display_clicked();
};
c ++源文件:
#include "QtGuiApplication.h"
#include <math.h> /* sin */
#define pi 3.14159265
QtGuiApplication::QtGuiApplication(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
ui.graphicsView->setScene(scene);
ui.graphicsView->show();
connect(ui.btn_Display, SIGNAL(clicked()), this, SLOT(btn_Display_clicked()));
}
void QtGuiApplication::btn_Display_clicked()
{
scene->removeItem(pathItem);
QPoint pos;
double x_amp_, y_amp_;
int x_pix_, y_pix_;
double phi_ = 0;
double freq_ = 5;
for (int i = 0; i<800; i++)
{
y_amp_ = 100 * sin(2 * pi*freq_*i / 811 + phi_) + 20 * index_;
x_amp_ = i;
x_pix_ = x_amp_;
y_pix_ = y_amp_;
pos.setX(x_pix_);
pos.setY(y_pix_);
pol.append(pos);
}
QPainterPath* myPath = new QPainterPath();
(*myPath).addPolygon(pol);
pathItem = scene->addPath(*myPath, *myPen);
delete myPath;
pol.clear();
index_ = (index_ + 1) % 20; // just for sense of change in graph
}
答案 0 :(得分:3)
在
pathItem = scene->addPath(*myPath, *myPen);
创建了新的QGraphicsPathItem
并将指针返回到pathItem
。迟早会再次单击该按钮
scene->removeItem(pathItem);
从场景中删除QGraphicsPathItem
。不幸的是according to the documentation
项目的所有权传递给调用者(即,QGraphicsScene在销毁时将不再删除项目。)
删除pathItem
取决于程序员。 pathItem
未被删除,并在随后的
pathItem = scene->addPath(*myPath, *myPen);
解决方案:在泄露之前删除pathItem
。
scene->removeItem(pathItem);
delete pathItem;
应该做的。
答案 1 :(得分:2)
我有几条评论。
您正在做很多完全不必要的手动内存管理。为什么不好?因为如果你不是,你的错误就不可能通过施工来实现。您不必使用手动内存分配(显式new
)的所有用法。按值保存对象:它们被设计为以这种方式工作,让编译器担心其余部分。
您声明范围太大的对象。您在类中声明了多边形和笔,即使它们属于btn_Display_clicked
的范围;你在循环之外声明了循环变量(x_amp_
和y_amp_
)。
connect
语句是多余的:setupUi
为您进行连接。
你没有完全利用C ++ 11。
包含<QtModule/QClass>
形式推迟检测项目配置错误,直到链接时为止且不必要。您的意思是直接包含<QClass>
。如果出现编译错误,那么您的项目配置错误 - 也许您需要重新运行qmake(典型解决方案)。
在C ++中,<cmath>
是惯用的,而不是<math.h>
。保留后者以与C兼容。
也许您可能希望使用M_PI
。它是C ++中pi广泛使用的名称。
预先分配多边形可避免在点存储增长时过早地重新分配点存储。
使用基于整数的多边形会导致精度下降,除非您出于特定原因而做出选择。否则,您应该使用QPolygonF
。
因此,界面变为:
#pragma once
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPathItem>
#include "ui_QtGuiApplication.h"
class GuiApplication : public QMainWindow
{
Q_OBJECT
public:
GuiApplication(QWidget *parent = {});
private:
Ui::GuiApplicationClass ui;
QGraphicsScene scene;
QGraphicsPathItem pathItem; // the order matters: must be declared after the scene
int index_ = 0;
Q_SLOT void btn_Display_clicked();
};
实施:
#include "GuiApplication.h"
#define _USE_MATH_DEFINES
#include <cmath>
#if !defined(M_PI)
#define M_PI (3.14159265358979323846)
#endif
GuiApplication::GuiApplication(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
ui.graphicsView->setScene(&scene);
ui.graphicsView->show();
pathItem.setPen({Qt::red, 2});
scene.addItem(&pathItem);
}
void GuiApplication::btn_Display_clicked()
{
const double phi_ = 0;
const double freq_ = 5;
const int N = 800;
QPolygonF pol{N};
for (int i = 0; i < pol.size(); ++i) {
qreal x = i;
qreal y = 100 * sin(2 * M_PI * freq_ * i / 811 + phi_) + 20 * index_;
pol[i] = {x, y};
}
QPainterPath path;
path.addPolygon(pol);
pathItem.setPath(path);
index_ = (index_ + 1) % 20; // just for sense of change in graph
}
在C ++ 14中,如果您希望在其他地方重复使用它,您可以轻松地将生成器分解出来;那么循环将被替换为:
auto generator = [=,i=-1]() mutable {
++i;
return QPointF{qreal(i), 100 * sin(2 * M_PI * freq_ * i / 811 + phi_) + 20 * index_};
};
std::generate(pol.begin(), pol.end(), generator);
唉,就目前而言,btn_Display_clicked
实现抛弃了分配用于存储多边形的堆,并且它不必要地将数据从多边形复制到路径中的内部元素存储。我们可以通过直接修改画家项目的路径来避免这两种情况:
void GuiApplication::btn_Display_clicked()
{
const double phi_ = 0;
const double freq_ = 5;
const int N = 800;
if (pathItem.path().isEmpty()) { // preallocate the path
QPainterPath path;
path.addPolygon(QPolygon{N});
pathItem.setPath(path);
}
auto path = pathItem.path();
pathItem.setPath({}); // we own the path now - item's path is detached
Q_ASSERT(path.elementCount() == N);
for (int i = 0; i < path.elementCount(); ++i) {
qreal x = i;
qreal y = 100 * sin(2 * M_PI * freq_ * i / 811 + phi_) + 20 * index_;
path.setElementPositionAt(i, x, y);
}
pathItem.setPath(path);
index_ = (index_ + 1) % 20; // just for sense of change in graph
}
因为QPainterPath
是隐式共享值类,所以path = item.path(); item.setPath({});
序列相当于将路径移出路径项。随后的setPath(path)
会将其移回,因为我们不会对本地副本进行进一步更改。