QCustomPlot Discussion and Comments

How to Enable OpenGL acceleration in QCustomPlot?Return to overview

Hi, I'm using Qt version 5.12.2. As stated in the documentation I have added DEFINES += QCUSTOMPLOT_USE_OPENGL in my .pro file.

DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += QCUSTOMPLOT_USE_OPENGL

But when I use setOpenGl(true) to enable OpenGL, the following error is reported:

void QCustomPlot::setOpenGl(bool, int) QCustomPlot can't use OpenGL because QCUSTOMPLOT_USE_OPENGL was not defined during compilation (add 'DEFINES += QCUSTOMPLOT_USE_OPENGL' to your qmake .pro file)

Is there something I'm missing?

Sorry, My mistake I did not do Rebuild after adding the statement.
"Clean and Rebuild" solved the above issue.

But now I am getting other errors:
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (8 8 8 0)
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (1 8 8 0)
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (1 1 8 0)
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (1 1 1 0)
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (1 1 1 0)

Did anyone observe any strange behavior when OpenGL is set for multiple QCustomPlot widgets in a single instance?

I have three QCustomPlot widgets each set to a QDockWidget. All 3 dock widgets are set to a single mainwindow. When the application is launched only one QCustomPlot Widget is visible. If the other widgets are clicked with a mouse then they appear. And again if the handle between dock widgets is moved they disappear again.

Does OpenGL work for multiple QCustomPlot widgets?

QCustomplot 2.0.1 **OpenGL ISSUES SOLVED**

I solved the issue(warnings)
qt.glx: qglx_findConfig: Failed to finding matching FBConfig (8 8 8 0)
by setting a proper number of multi-samples in setOpenGL() . In my case setting the number of multi-samples as 8 solved the issue.

And I fixed the strange behavior when OpenGL is set for multiple QCustomPlot widgets in a single instance as follows:

When we create multiple QCustomPlot Widgets in a single instance (or single process), somehow QOpenGLContext::currentContext() is getting other OpenGL context.

Each time when the widget has to draw, it is calling the QCPPaintBufferGlFbo::draw(QCPPainter *painter) function. Here, the QopenGL's current context is not checked with the widget's mGLContext like in other QCPPaintBufferGlFbo class functions. So adding this check and making the mGLContext as current Context solved the issue.

Change the following function in qcustomplot.cpp

void QCPPaintBufferGlFbo::draw(QCPPainter *painter) const
{
  if (!painter || !painter->isActive())
  {
    qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
    return;
  }
  if (!mGlFrameBuffer)
  {
    qDebug() << Q_FUNC_INFO << "OpenGL frame buffer object doesn't exist, reallocateBuffer was not called?";
    return;
  }
  painter->drawImage(0, 0, mGlFrameBuffer->toImage());
}

To

void QCPPaintBufferGlFbo::draw(QCPPainter *painter) const
{
  if (!painter || !painter->isActive())
  {
    qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
    return;
  }
  if (!mGlFrameBuffer)
  {
    qDebug() << Q_FUNC_INFO << "OpenGL frame buffer object doesn't exist, reallocateBuffer was not called?";
    return;
  }

  if (QOpenGLContext::currentContext() != mGlContext.data())
    mGlContext.data()->makeCurrent(mGlContext.data()->surface());

  painter->drawImage(0, 0, mGlFrameBuffer->toImage());
}