3

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);
Nejat
  • 31,784
  • 12
  • 106
  • 138
Schwab
  • 91
  • 1
  • 6

2 Answers2

1

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];
Nejat
  • 31,784
  • 12
  • 106
  • 138
0

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

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

AlexandreP
  • 410
  • 1
  • 5
  • 17
  • The above code is not working because the setData() function takes setData(const double Vector key , const double Vector value); I was giving int vector values. and QT was complaining about corresponding function not found and was pointing out rightly – Schwab Apr 27 '15 at 23:22