QCustomPlot Discussion and Comments

Layering AxesReturn to overview

I have searched all over this forum, but have been unable to find an answer.

I want to be able to choose what my x axis displays. I have the data set up, and I have a drop down box containing my options (plot number or time). I want to be able to have either plot number or time as the x axis, based on the selection in the drop down.

The best way I can think to do this is to create a new layer, add a QCPAxis to it, and set its range. Then I can connect it to the x axis of the main QCPAxis, and they will scroll together. After that it is just a matter of which xAxis is set visible.

The construction code:
// setup the second x axis
ui.customPlot->setCurrentLayer("axes2");
QCPAxis *myAxis = ui.customPlot->axisRect()->axis(QCPAxis::atBottom);
myAxis->setLabel("Time");
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
ui.customPlot->axisRect()->axis(QCPAxis::atBottom)->setTicker(timeTicker);
ui.customPlot->axisRect()->axis(QCPAxis::atBottom)->setRange(0,(*data.getTime())[(*data.getTime()).size() - 1]);
ui.customPlot->setCurrentLayer("main");

Is this a good way of solving my problem?


Since implementing this I am getting draw errors. It says "ASSERT: "width > 0.0" in file painting\qrasterizer.cpp, line 728".

Stack Trace:
> QtCored4.dll!qt_message_output(QtMsgType msgType=QtFatalMsg, const char * buf=0x04cefeb8) Line 2270 C++
QtCored4.dll!qt_message(QtMsgType msgType=QtFatalMsg, const char * msg=0x672aa0d8, char * ap=0x003891c8) Line 2328 + 0x12 bytes C++
QtCored4.dll!qFatal(const char * msg=0x672aa0d8, ...) Line 2511 + 0xf bytes C++
QtCored4.dll!qt_assert(const char * assertion=0x65a4c0c0, const char * file=0x65a4c0a4, int line=728) Line 2027 + 0x16 bytes C++
QtGuid4.dll!QRasterizer::rasterizeLine(const QPointF & a={...}, const QPointF & b={...}, double width=1.#QNAN00000000000, bool squareCap=true) Line 728 + 0x21 bytes C++
QtGuid4.dll!QRasterPaintEngine::stroke(const QVectorPath & path={...}, const QPen & pen={...}) Line 1760 C++
QtGuid4.dll!QPaintEngineEx::drawLines(const QLineF * lines=0x0038a5fc, int lineCount=1) Line 818 C++
QtGuid4.dll!QRasterPaintEngine::drawLines(const QLineF * lines=0x0038a5fc, int lineCount=1) Line 3776 C++
QtGuid4.dll!QPainter::drawLines(const QLineF * lines=0x0038a5fc, int lineCount=1) Line 4636 C++
QtGuid4.dll!QPainter::drawLine(const QLineF & l={...}) Line 566 C++

Any ideas what could be causing this, based off of the provided code?

QCustomplot comes with 2 x axis and 2 y axis. you should be able to just change the ticker for ui.customplot->xAxis1 and xAxis2. then you can just set one visible.

Yes, I could do that, but xAxis2 displays on top of the graph. I want it to look seamless. The current xAxis should disappear, and a new one, with different data, should appear in its place. I want it to replace the old axis, not to just appear in a different place.

That is what I have currently. I added a second onBottom axis to the QCPAxis. But it does not look clean.

I gave up and went back to keeping the time axis below the plot number axis. So now I just have two bottom xAxes.

You should leave the normal xAxis and not create an additional one or play around with layers.

The only thing you need to switch is the axis ticker. So if you want to change the way the axis creates/displays ticks, call

ui.customPlot->xAxis->setTicker(ticker);

where ticker is just a new instance (wrapped in QSharedPointer) of the respective ticker you want, probably QCPAxisTicker for regular numbers and QCPAxisTickerDateTime for dates/times.
Don't forget to replot after the change.