Results 1 to 2 of 2

Thread: Qwt real-time plot memory problem

  1. #1
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qwt real-time plot memory problem

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt real-time plot memory problem

    Quote Originally Posted by speak_no_evil View Post
    The method appendData is called every ms.
    Didn't see a memory issue in your code, but QwtPlotDirectPainter has to post a local paint event, when the target paint device doesn't have the Qt::WA_PaintOutsidePaintEvent property. Memory issues might be related to this.
    But trying an update rate ( 1000Hz ) - far above the physical limtations of a LCD - what do you expect to happen ?

    Better decouple sample and refresh rate - f.e. by not drawing new points in appendData(). Instead set up a timer ( maybe 100ms, 10Hz should be enough for human eyes and the kind of visual changes we have here ) that always draws the appended points since the last refresh period. This is what the oscilloscope example does.

    Uwe

Similar Threads

  1. How to real time plot?
    By RafaelRSE in forum Qwt
    Replies: 2
    Last Post: 7th August 2014, 18:14
  2. Replies: 3
    Last Post: 12th April 2013, 06:18
  3. How to plot real time x/y graph in Qtopia
    By desperado_43 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th May 2012, 08:00
  4. real time graph plot
    By robotics in forum Qt Programming
    Replies: 5
    Last Post: 24th May 2011, 05:04
  5. QFileSystemWatcher with a Qwt Real-time plot
    By gen_mass in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2010, 21:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.