QCustomPlot Discussion and Comments

QCPFinancial and updating data via slotReturn to overview

Testing driving QCP for the first time, and trying to feed data to a candlestick chart as they come in like the realtimeDataDemo in "plot-examples". But how should I add data in the slot, which in the examples only are shown for graph (customPlot->addGraph() then ui->customPlot->graph(n)->addData(..)).


void MainWindow::setupFinancialDemo(QCustomPlot *customPlot) {
  QCPFinancial *candlesticks = new QCPFinancial(customPlot->xAxis, customPlot->yAxis);
  connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeFinancialDataSlot()));


void MainWindow::realtimeFinancialDataSlot()

Keep your QCPFinancial instance accessible by storing a pointer, e.g. as a private member variable of your MainWindow. To be safe, you could use a smart pointer (QPointer).

Then you can access it normally also in the realtimeFinancialDataSlot.

Alternatively, you can retrieve any plottable in the plot with customPlot->plottable(i), where i is its index (from 0 to plottableCount()-1). This gives you the QCPAbstractPlottable pointer, which you can qobject_cast to the respective plottable subclass, such as QCPFinancial. Of course, you should check whether the cast returns zero and not dereference in that case. (Means that the plottable wasn't of the cast target type.)

Thanks for the extremely quick reply!

I went the QPointer way. Works like a charm.