QCustomPlot Discussion and Comments

Unexpected behavior plotting Lissajous patternReturn to overview

Experienced unexpected and very unfortunate behavior in QCustomPlot that can be seen by plotting a Lissajous pattern. If you aren't familiar with such patterns, a simple example is to generate two variables, say x and y, that are both sinusoidal functions of another variable, often just a linearly growing time variable, t. That is plot y(t) vs x(t) for appropriate values of time.

If I do this, the x!y scatter points are plotted at the correct locations. Unfortunately, if I try to connect the time-sequential points with a connecting line, the resulting line is not at all what one would expect. It appears that internal to QCustomPlot, the x and y variables have been sorted by ascending values of x. Hence it is NOT points adjacent in time that are connected, but points by ascending x.

There is a flag, alreadySorted, for method setData for data that are guaranteed to be sorted, but I apparently can't trick the plot routine using that flag - the plot just doesn't work at all.

Any suggestions?

FYI, my plot strategy is a pretty basic QCustomPlot -> addGraph -> setData, etc.

A QCPGraph, which is what you get by plot->addGraph(), uses sorted x data and the corresponding y-data. What you want to use is a QCPCurve, which uses a sorted t-coordinate, "which defines the order of the points described by the other two coordinates x and y", see http://www.qcustomplot.com/documentation/classQCPCurve.html#details


Thanks a bunch - looks like that ultimately solve my problem.

I am presently stuck by not finding curve functionality equivalent to addGraph, graphCount, selectedGraphs, etc for QCustomPlot

Does that functionality exist? If not, do know how I would determine the "type" of a QCPAbstractPlottable so I could add the equivalent addCurve, curveCount, selectedCurves capability to a new myQCustomPlot widget?

Thanks for any pointer


these functions don't exist for Curves. As all QCPLayerable inherit from QObject, one way to test if a plottable is in fact a curve would be qobject_cast. As far as I know, a seperate type ID is not avalable for plottables. Another way would be dynamic_cast, but according to https://stackoverflow.com/questions/43994584/what-is-qobject-cast this is slower than qobject_cast (never checked by myself).
The code below was not testet, but "should" work ;-)

for (int n=0; n<plot->plottableCount(); n++) {
  if (QCPCurve* curve = qobject_cast<QCPCurve*>(plot->plottable(n))) {
    // do something


Problem solved - thanks for pointing me to the QCPCurve class.

For reference, what I did was implement a class myQCustomPlot
that replaced the QCPGraph-specific functionality with
replacement work-alikes (at least all of the ones I care about at
present). For specific example, it keeps its own list of
QCPGraphs and relies on functions like selectedPlottables instead
of selectedGraphs. By comparing a QCPAbstractPlottable to the
internal list of QCPGraphs, it builds its own list to return for

This was straightforward and worked properly with minimal grief.
Then I just replaced QCPGraph with QCPCurve and voila,
myCustomPlot treats a QCPCurve just like it treats a QCPGraph.