Important: Please read the Qt Code of Conduct -

Wrapping a QOpenGLWidget in a QAbstractScrollArea

  • When I set a subclass of QOpenGLWidget as the viewport to a QAbstractScrollArea it seems a lot of the initialization for the widget does not happen.

    I have two classes

    class ScrollCanvas : public QAbstractScrollArea


    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions

    ScrollCanvas has a GLWidget as a member and I set it to the viewport using setViewport(glWidget);

    In QAbstractScrollArea there is a function setupViewport where I can initialize the viewport after it's set. In this function I call initializeGL() from GLWidget. However the call to initializeOpenGLFunctions();, which is the first thing in initializeGL() causes a crash. I put a print statement just above this line and printed the address of the QOpenGLContext and found that it was set to 0.

    When the GLWidget is not set as the viewport of the QAbstractScrollArea the QOpenGLContext has a valid memory address at this point. So it seems that a function is not being called during the initialization of the widget. Can anyone tell me what initialization needs to occur before initializeGL() such that the QOpenGLContext is valid?