我想用 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
}