Important: Please read the Qt Code of Conduct -

How to call OpenGL draw calls from non QOpenGLWidget object?

  • Hi all,

    Sorry for noob question, but I am new to this and desperately need help. I am making a CAD model viewer; that is OpenGL 3D viewer in QT application.

    In short, code looks like this:

    class Viewer: public QOpenGLWidget, protected QOpenGLFunctions_4_3_Core;

    Viewer has InitializeGL, ResizeGL and PaintGL implemented and Viewer works fine. Imported model renders fine and interacts with mouse.

    Problems is that I want different paintGL modes(default, pick, polygon). If I create interface object :

    // interface
    class ViewerMode{
       virtual void paintGL() = 0;
    class DefaultViewerMode: public ViewerMode;
    class PickViewerMode: public ViewerMode;
    class PolygonViewerMode: public ViewerMode;

    While implementing paintGL, why OpenGL draw calls from these object crash application ? Why ? What am I missing?

    Do these modes also need to be widgets? Why
    Is it a context problem? Why?

    Any help, reference, link, explanation or hint is very much appreciated!!!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you using these ViewerMode subclasses ?

  • Thanks for your reply.

    Viewer class is my QOpenGLWidget, added to my central widget. And its paintGL overriden function initially has glDraw**** functions.

    I don't want to have three different Viewer::paintGL functions for three different viewer modes, my idea is to create these new ViewerMode subclasses which have "paintGL" function and place OpenGL draw calls in it.

    And simply call paintGL function from current mode:

    ViewerMode* tmpViewerMode = new DefaultViewerMode();

  • Lifetime Qt Champion

    Can you show the code of one of the ViewerMode subclass ?
    And the stack trace of the crash ?

    From the looks of it, you might be using something uninitialised.

  • @SGaist

    When I run my debugger, this is the line its says its the problem:

    inline void QOpenGLFunctions_4_3_Core::glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
        d_1_1_Core->f.DrawElements(mode, count, type, indices);   //<- this line 

    But what can be uninitialized ?

    Viewer class is initialized, and I also call initializeOpenGLFunctions() once before running DefaultViewerMode::paintGL().

    If I leave only this in paint function:


    then application runs fine, renders color I defined.
    For some reason, draw call from viewerModes crash with segmentation fault and I have no idea why.

    Is it because it doesn't know where to render? That it doesn't know "where", on "which" window to render? That it is missing some context? I don't understand how this works?

    I realize that if Viewer class inherits from QOpenGLWidget, it has some "context"(whatever that is), but what do I need to do to make my ViewerMode subclasses to successfully run glDraw**** functions? Since they are initialized properly and can successfully run glClear function? Does ViewerMode classes also need to be QOpenGLWidgets and why ? Is it enough that they simply inherit from QOpenGLFunctions_4_3_Core?

    class ViewerMode : public QOpenGLFunctions_4_3_Core

  • Lifetime Qt Champion

    I would both inherit from the QOpenFunctions_XXX class needed and initialise it.

  • Thanks for the tip. It is enough if object called initializeOpenGLFunctions().

    The problem was that I haven't properly defined VAO. That is why draw calls always crashed.

Log in to reply