Hello!

For a few hours I've been scratching my head about what seems to be a simple problem. What I want my application to do is that I want to collect sensor data and plot them. I'm running my application on a Raspberry PI so I tested to use the SetRawSamples() method, which worked but was to slow on some screens. What I want to do instead is something similar to the oscilloscope example where they use QwtPlotDirectPainter instead, even though I've not been able to test the performance of the application in it's final state I found out that it was fast enough for my needs.

So, what my application is doing right now:
  1. Store 10 sensor values in CurveData
  2. Draw a curve segment of the 10 latest values
  3. Repeat 100 times until 1000 values are plotted
  4. Return to the left side of the plot and continue with plotting segments, thus drawing a new curve over the existing one


Here is an image of the result:
2014-03-17 16.54.57.jpg

Now, what I want to do is to clear my current plot when 1000 values are plotted and only see 1 curve in my plot window. I'm currently calling QwtPlot::replot() when I clear my curve data but it still wont clear the plot.

This is my plot.cpp file in it's current state:
Qt Code:
  1. #include "plot.h"
  2. #include <qwt_plot.h>
  3. #include <qwt_plot_grid.h>
  4. #include <qwt_plot_layout.h>
  5. #include <qwt_plot_canvas.h>
  6. #include <qwt_plot_curve.h>
  7. #include <qwt_plot_directpainter.h>
  8. #include <qwt_curve_fitter.h>
  9. #include <qwt_painter.h>
  10.  
  11. class CurveData: public QwtArraySeriesData<QPointF>
  12. {
  13. public:
  14. CurveData()
  15. {
  16. }
  17.  
  18. virtual QRectF boundingRect() const
  19. {
  20. if ( d_boundingRect.width() < 0.0 )
  21. d_boundingRect = qwtBoundingRect( *this );
  22.  
  23. return d_boundingRect;
  24. }
  25.  
  26. inline void append( const QPointF &point )
  27. {
  28. d_samples += point;
  29. }
  30.  
  31. void clear()
  32. {
  33. d_samples.clear();
  34. d_samples.squeeze();
  35. d_boundingRect = QRectF( 0.0, 0.0, -1.0, -1.0 );
  36. }
  37. };
  38.  
  39. Plot::Plot( QWidget *parent ):
  40. QwtPlot( parent )
  41. {
  42. d_directPainter = new QwtPlotDirectPainter(this);
  43.  
  44. setAutoReplot(false);
  45.  
  46. setAxisTitle(QwtPlot::xBottom, "Time [s]");
  47. setAxisScale(QwtPlot::yLeft, 0.014,0.016);
  48. setAxisScale(QwtPlot::xBottom, 0, 1000);
  49.  
  50. d_curve = new QwtPlotCurve();
  51. d_curve->setData(new CurveData());
  52. d_curve->attach(this);
  53. }
  54.  
  55. Plot::~Plot()
  56. {
  57. delete d_directPainter;
  58. }
  59.  
  60. void Plot::AppendPoint(const QPointF &point)
  61. {
  62. CurveData *data = static_cast<CurveData *>(d_curve->data());
  63. data->append(point);
  64. }
  65.  
  66. void Plot::DrawCurveSegment()
  67. {
  68. CurveData *data = static_cast<CurveData *>(d_curve->data());
  69.  
  70. d_directPainter->drawSeries(d_curve, data->size()-11, data->size()-1);
  71. }
  72.  
  73. void Plot::ClearPlot()
  74. {
  75. d_curve->setData(new CurveData());
  76. QwtPlot::replot();
  77. }
To copy to clipboard, switch view to plain text mode 

And here is the interesting parts of my mainwindow.cpp
Qt Code:
  1. #include "mainwindow.h"
  2. #include "plot.h"
  3. #include <QtWidgets>
  4.  
  5. MainWindow::MainWindow(QWidget *parent):
  6. QWidget( parent )
  7. {
  8. d_plot = new Plot(this);
  9. counter = 0;
  10. loopCounter = 0;
  11.  
  12. // ... //
  13.  
  14. connect(generateButton, SIGNAL(clicked()), this, SLOT(generate()));
  15.  
  16. // ... //
  17. }
  18.  
  19. void MainWindow::generate()
  20. {
  21. (void)startTimer(50);
  22. }
  23.  
  24. void MainWindow::timerEvent(QTimerEvent *) {
  25. if (counter>0 && counter%1000==0)
  26. {
  27. d_plot->ClearPlot();
  28. loopCounter++;
  29. }
  30.  
  31. for (int ii=0; ii<10;ii++)
  32. {
  33. double y = sensorVals[counter];
  34. double x = (double)counter-((double)loopCounter*1000);
  35.  
  36. counter++;
  37. d_plot->AppendPoint(QPointF(x,y));
  38. }
  39. d_plot->DrawCurveSegment();
  40. }
To copy to clipboard, switch view to plain text mode 

Would appreciate any help, also please let me know if any information in my post is missing.

Best regards!