PDA

View Full Version : How to real time plot?



RafaelRSE
6th August 2014, 14:44
I'm want to draw a sinusoid that is updated every 100ms after a button is clicked. What I'm doing is, when the button is clicked, 2000 points are plotted with x and y values equal 0 and then, the timer is started. Every tick of the clock updates the plot, "moving" all the values to the left (what will remove the first value of the series) and adding a new valeu to the end.

I make this but the plot just don't update. I'm sure the problem isn't with the Timer.

PS: sorry for my English.

Global variables:


double i;
QwtPlotCurve *curve;
QwtPlotGrid *grid;
QPolygonF points;
QPointF point;

Event button clicked:


void MainWindow::on_btnDraw_clicked()
{
i=0;

ui->qwtPlot->setTitle("Test Plot");
ui->qwtPlot->setCanvasBackground(Qt::white);
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, -2.0, 2.0);
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0.0, 20.0);
ui->qwtPlot->updateAxes();

curve = new QwtPlotCurve("Points");
curve->setPen(Qt::blue,1);
curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);

grid = new QwtPlotGrid();
grid->show();
grid->attach(ui->qwtPlot);

for(i=0;i<2000;i++)
{
point.setX(0);
point.setY(0);
points += point;
}

curve->setSamples(points);
curve->attach(ui->qwtPlot);

ui->qwtPlot->update();

timer = new QTimer(this);
timer->setInterval(100);
timer->start();
connect(timer,SIGNAL(timeout()),this,SLOT(Update() ));
}

Update() function:


void MainWindow::Update()
{
ui->qwtPlot->setAxisAutoScale(QwtPlot::yLeft, true);
ui->qwtPlot->setAxisAutoScale(QwtPlot::xBottom, true);
ui->qwtPlot->updateAxes();

for(int j=0;j<points.count()-1;j++)
{
points.value(i)=points.value(i+1);
}

i+=0.1;
point.setX(i);
point.setY(sin(i));
points += point;

curve->setSamples(points);
curve->attach(ui->qwtPlot);

ui->qwtPlot->update();
}

Uwe
7th August 2014, 09:23
See QwtPlot::replot()

Uwe

RafaelRSE
7th August 2014, 18:14
Thanks, I found the solution. I just needed to add the line:



ui->qwtPlot->setAutoReplot(true);