Hi there,
I have a problem with dynamically updating a QwtPlot. I based my code on the example "data_plot" but instead of having a timer inside of my data_plot-object I created a slot which gives me new values to add. Here is the code:
#ifndef _DATA_PLOT_H
#define _DATA_PLOT_H 1
#include <qwt_plot.h>
const int PLOT_SIZE = 201; // 0 to 200
{
Q_OBJECT
public:
public slots:
void refreshP(double val);
private:
double d_x[PLOT_SIZE];
double d_y[PLOT_SIZE];
double d_z[PLOT_SIZE];
};
#endif
#ifndef _DATA_PLOT_H
#define _DATA_PLOT_H 1
#include <qwt_plot.h>
const int PLOT_SIZE = 201; // 0 to 200
class DataPlot : public QwtPlot
{
Q_OBJECT
public:
DataPlot(QWidget* = NULL);
public slots:
void refreshP(double val);
private:
double d_x[PLOT_SIZE];
double d_y[PLOT_SIZE];
double d_z[PLOT_SIZE];
};
#endif
To copy to clipboard, switch view to plain text mode
#include <stdlib.h>
#include <qwt_painter.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_scale_draw.h>
#include <qwt_math.h>
#include "data_plot.h"
#include <iostream>
{
// Disable polygon clipping
// We don't need the cache here
// Initialize data
for (int i = 0; i< PLOT_SIZE; i++)
{
d_x[i] = 0.5 * i; // time axis
d_y[i] = 0;
}
// Insert new curves
cRight->attach(this);
// Set curve styles
cRight
->setPen
(QPen(Qt
::red));
// Attach (don't copy) data. Both curves use the same x array.
cRight->setRawData(d_x, d_y, PLOT_SIZE);
}
// Generate new values
void DataPlot::refreshP(double val)
{
for ( int j = 0; j < PLOT_SIZE - 1; j++ )
d_y[j] = d_y[j+1];
d_y[PLOT_SIZE - 1] = val;
// update the display
replot();
}
#include <stdlib.h>
#include <qwt_painter.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_scale_draw.h>
#include <qwt_math.h>
#include "data_plot.h"
#include <iostream>
DataPlot::DataPlot(QWidget *parent): QwtPlot(parent)
{
// Disable polygon clipping
QwtPainter::setDeviceClipping(false);
// We don't need the cache here
canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
// Initialize data
for (int i = 0; i< PLOT_SIZE; i++)
{
d_x[i] = 0.5 * i; // time axis
d_y[i] = 0;
}
// Insert new curves
QwtPlotCurve *cRight = new QwtPlotCurve("Data Moving Right");
cRight->attach(this);
// Set curve styles
cRight->setPen(QPen(Qt::red));
// Attach (don't copy) data. Both curves use the same x array.
cRight->setRawData(d_x, d_y, PLOT_SIZE);
}
// Generate new values
void DataPlot::refreshP(double val)
{
for ( int j = 0; j < PLOT_SIZE - 1; j++ )
d_y[j] = d_y[j+1];
d_y[PLOT_SIZE - 1] = val;
// update the display
replot();
}
To copy to clipboard, switch view to plain text mode
Then here is how it is used:
#include "window.h"
{
.
.
.
Widget2D *evolving_obj = new Widget2D;
DataPlot *dynamics = new DataPlot(this);
connect(evolving_obj, SIGNAL(newSignal(double)), dynamics, SLOT(replot())); // this does not work
connect(someButton , SIGNAL(clicked()), dynamics, SLOT(replot())); //this works!
.
.
.
}
#include "window.h"
Window::Window(QWidget *parent) : QWidget(parent)
{
.
.
.
Widget2D *evolving_obj = new Widget2D;
DataPlot *dynamics = new DataPlot(this);
connect(evolving_obj, SIGNAL(newSignal(double)), dynamics, SLOT(replot())); // this does not work
connect(someButton , SIGNAL(clicked()), dynamics, SLOT(replot())); //this works!
.
.
.
}
To copy to clipboard, switch view to plain text mode
Widget2D has a timer and produces data and of course Widget2D has a signal "void newSignal(double nS);" with the corresponding body. The new data is correctly stores in the array d_Y but the plot is not updated.
If I however click on the someButton, the plot is updated.
I have no clue how to solve this problem. Thanks for your help.
Bookmarks