0

C++ で独自のシグナル スコープを開発したいと考えています。だから私はこの目的のためにqtを使います。ui に graphicsView と push Button を追加します。ボタンを押すための接続されたメソッドで、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 
}
4

2 に答える 2