Hi,

I'm new on the forum and I have an issue using the QWT graphic library.

My goal is to observe the movement of a point moving in a plot canvas. Therefore, it implies cleaning the previous data point before plotting the new one.
The coordinates of the point must be refreshed every 1 ms.
I'm using Qt 5.3 and MSVC2013 on a Windows 8 - 64bits machine, my version of qwt is 6.1.0.

Here is my code, the object is instantiated and set as child in a parent Widget.

---> SpotViewer.h
#ifndef SPOTVIEWER_H
#define SPOTVIEWER_H

#include <QWidget>
#include <QPoint>
#include <QPointF>
#include <QVector>
#include <qwt_plot_directpainter.h>
#include <QtGui>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <qwt_plot_canvas.h>
#include <qwt_scale_map.h>

class SpotViewer : public QWidget {

Q_OBJECT

private:

QwtPlot *plotter = NULL;
QwtPlotCurve *dataSet = NULL;
QwtSymbol *symbol1 = NULL;
QwtSymbol *symbol2 = NULL;
QwtPlotDirectPainter *painterRT = NULL;
QwtPlotCanvas *canvasRT = NULL;

double xcur;
double ycur;
double xpre;
double ypre;


public:

SpotViewer();

~SpotViewer();

void appendData(double x, double y);

};

#endif // SPOTVIEWER_H

---> SpotViewer.cpp
#include "SpotViewer.h"
#include <iostream>
#include <cstdlib>

using namespace std;


SpotViewer::SpotViewer(){

plotter = new QwtPlot(QwtText("Visualization spot" ), this);
plotter->setGeometry(0,0,800,800);
plotter->setAxisScale(QwtPlot::xBottom, -1.0, 1.0);
plotter->setAxisScale(QwtPlot::yLeft, -1.0, 1.0);

plotter->enableAxis(QwtPlot::xBottom,false);
plotter->enableAxis(QwtPlot::yLeft,false);

painterRT = new QwtPlotDirectPainter();

dataSet = new QwtPlotCurve("Spot Tag 1");

dataSet->setPen( Qt::blue, 25 ),
dataSet->setRenderHint( QwtPlotItem::RenderAntialiased, true );

symbol1 = new QwtSymbol( QwtSymbol::Ellipse,
QBrush( Qt::gray ), QPen( Qt::blue, 4 ), QSize( 25, 25 ) );
symbol2 = new QwtSymbol( QwtSymbol::Ellipse,
QBrush( Qt::white ), QPen( Qt::white, 4 ), QSize( 25, 25 ) );
dataSet->setSymbol( symbol1 );

dataSet->attach(plotter);

// canvas
canvasRT = new QwtPlotCanvas();

canvasRT->setLineWidth( 1 );
canvasRT->setFrameStyle( QFrame::Box | QFrame::Plain );
canvasRT->setBorderRadius( 400 );

canvasRT->setAutoFillBackground(true);

QPalette pal = palette();
pal.setColor(QPalette::Window, Qt::white);

plotter->setCanvas( canvasRT );

xpre = 0;
ypre = 0;
xcur = 0;
ycur = 0;

}

SpotViewer::~SpotViewer(){

delete plotter;
delete dataSet;
delete symbol1;
delete symbol2;
delete painterRT;
delete canvasRT;

}

void SpotViewer::appendData(double x, double y){

dataSet->setSamples(&xpre, &ypre, 1);

symbol1->setSize(QSize( 27, 27 ));
symbol1->setBrush(QBrush(QColor(255,255,255,255)));
symbol1->setPen(QPen(QColor(255,255,255,255)));

painterRT->drawSeries(dataSet, 0, 1);

xcur = x;
ycur = y;

dataSet->setSamples(&xcur, &ycur, 1);

cout << dataSet->data()->size() << endl;

symbol1->setSize(QSize( 25, 25 ));
symbol1->setBrush(QBrush(Qt::blue));
symbol1->setPen(QPen(Qt::red));

painterRT->drawSeries(dataSet, 0, 1);

xpre = xcur;
ypre = ycur;

}

The method appendData works like this:
First, the previous plot is cleaned by plotting a white (or the same colour than the canvas background) point at the xpre, ypre coordinates. If appendData is called for the first time, xpre and ypre have been set to (0,0) in the constructor.
Then the current observed point is plotted in a chosen colour (here is blue and red).

The method appendData is called every ms. It works fine during a few seconds and then a memory problem occurs, the OS changes my screen resolution because of graphic memory leak and everything drastically slows down.

So, does anybody have any suggestions to help me avoiding those graphic memory problems or advices to use the qwt library properly ? How can I quickly reset my plot canvas ?

In my previous code versions (not given here), I used the replot() method but I read on many forum it's quite slow and, indeed, I got memory errors an my program simply crashed.

Best regards!