Schwab
Schwab

Reputation: 91

How to use QCustomPlot in plot real time

QCustomPlot has setData function that accepts const variable. Is there a way to QCustomPlot can be use to plot dynamically? QCustomPlot setData function accepts constant vectors, but I have to change the values in this vector dynamically.

const QVector<double> yval(cl);
const QVector<int> xval(cl);


for (int j = 0; j<cl; j++)
    yval[j] = ui->tableView->model()->data(ui->tableView->model()->index(5, j)).toDouble();
for (int j = 0; j<cl; j++)
{
    xval[j] = j;
}
ui->widget->graph()->setData(xval, yval);

Upvotes: 3

Views: 5775

Answers (2)

Nejat
Nejat

Reputation: 32635

You can use QCPGraph::data(). The documentation of QCustomPlot sates that :

Returns a pointer to the internal data storage of type QCPDataMap. You may use it to directly manipulate the data, which may be more convenient and faster than using the regular setData or addData methods, in certain situations.

You can manipulate data in QCustomPlot like :

for(int i=0; i<count; i++)
    plot->graph()->data()[i] = y[i];

Upvotes: 1

AlexandreP
AlexandreP

Reputation: 440

You call setData() with your new data, then customPlot()->replot()

I use it within a 50ms timer and it works very well.

Upvotes: 0

Related Questions