QCustomPlot Discussion and Comments

Hello ,
Is it possible to create animate bargraphs
my architecture is
1.In qtablewidget i am getting vales for Particular ipaddress bytesrecived,bytessent.
my reqiurement from QCP is
1.to animate dynamically ipaddress bytessent ,byterecived in bargraph.
how to send values to bargraph i am not getting....
Thanks

Yes, the QCPBars plottable can be animated just as other plottables. maybe you can look at the realtime demo (in the plot examples project) to get an idea how to do animated plots. For example before every refresh of the plot you could set new bar values via the QCPBars::setData method.

I am having difficulty implementing this exact idea.

In the example, the graph is accessed using the pointer:

void MainWindow::realtimeDataSlot()
{
ui->customPlot->graph(0)->addData(key, value0);
}

I am having difficulty accessing the pointer to QCPBars, so that I can modify it using something like:
fossil->setData(ticks, fossilData);

I have tried using the call to QCustomPlot::plottable() such as:
ui->customPlot->plottable(2)->setData(ticks, fossilData);

but plottable() returns a pointer to QCPAbstarctPlottable, not to QCPBars.

I guess my basic question is, from within my realtimeDataSlot() function, how can I point to the object QCPBars 'fossil' to modify it?

I am a C++ newb if it wasn't obvious.

Thanks!

There are two possible approaches:
1.
You could store a pointer to the bars instance in your parent class (e.g. your QMainWindow). So you declare a member variable

QCPBars *myBars;

and while generating the bars, store a pointer to it in that variable:

myBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
customPlot->addPlottable(myBars);
...

Now you can access myBars in any method of QMainWindow. Note that QCustomPlot takes ownership of the bars instance, so you shouldn't externally delete myBars. Further, if the bars plottable is removed from the QCustomPlot, e.g. via QCustomPlot::removePlottable, the instance will be deleted and turn your external myBars pointer invalid. Using/dereferencing it after the deletion will crash your program. To avoid that, you'll either need to use some extra care-taking code to make sure myBars is never accessed after the deletion of the bars, or you could use a QPointer<QCPBars> variable type for storing the pointer. Search for safe pointer if you don't know what that is.

2.
The alternative way is casting the QCPAbstractPlottable base class pointer, which is returned by customPlot->plottable(i), to a pointer of type QCPBars*, like so:

QCPBars *b = qobject_cast<QCPBars*>(customPlot->plottable(i));
if (b != 0)
{
  b->setData(...)
  ...
}
// the first two lines are usually combined to the idiom:
// if (QCPBars *b = qobject_cast<QCPBars*>(customPlot->plottable(i)))

Note that checking the validity (i.e. != 0) of the return value of qobject_cast is vital, because if the returned plottable is not a QCPBars (but a different subclass of QCPAbstractPlottable, like QCPGraph or QCPCurve), it returns 0. And of course, calling setData on a 0 pointer will crash your program once again.

Excellent. Thank you for the extremely rapid response (and the great software)!