Results 1 to 2 of 2

Thread: Qt Graphics Framework and OpenGL

  1. #1
    Join Date
    Oct 2025
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Qt Graphics Framework and OpenGL

    Hello everyone, I use c++17, ubuntu 20.04 and qt 5.12. I have a project where I use QGraphicsView, QGraphicsScene and QGraphicsItem. To increase performance, I switched to OpenGL render . So QGraphicsView 's viewport is plain QOpenGLWidget :
    Qt Code:
    1. std::unique_ptr<GraphicsView> _view;
    2. std::unique_ptr<QOpenGLWidget> _glViewport;
    3. // ......
    4. _view->setViewport(_glViewport.get());
    To copy to clipboard, switch view to plain text mode 

    And I also use OpenGL inside QGraphicsItem:aint() method:

    Qt Code:
    1. void GLitem::paint(QPainter *painter,
    2. const QStyleOptionGraphicsItem * option,
    3. QWidget * widget)
    4. {
    5.  
    6. painter->beginNativePainting();
    7. initializeOpenGLFunctions();
    8.  
    9. std::call_once(_onceFlag, &GLitem::initStuff, this);
    10. // draw stuff
    11.  
    12. _vao.release();
    13. _shaders.release();
    14.  
    15. painter->endNativePainting();
    16.  
    17. }
    18.  
    19. void GLitem::UploadGeometry()
    20. {
    21. // here i create VAO, VBO and shaders
    22. // this function is called only once in a valid context, 'cause std::call_once
    23. // QOpenGLVertexArrayObject _vao;
    24. // QOpenGLBuffer _vbo;
    25. }
    To copy to clipboard, switch view to plain text mode 



    So the problem is, that when I close this program, it crashes. And the problem is in GLitem D-tor:
    Qt Code:
    1. ~GLitem()
    2. {
    3. if (_vao.isCreated())
    4. _vao.destroy();
    5. if (_vbo.isCreated())
    6. _vbo.destroy();
    7. if (_shaderProgram->isLinked())
    8. _shaderProgram->removeAllShaders();
    9. }
    To copy to clipboard, switch view to plain text mode 

    I've read in the documentation, that you need
    void QOpenGLVertexArrayObject::destroy()
    Destroys the underlying OpenGL vertex array object. There must be a valid OpenGL context that supports vertex array objects current for this function to succeed.
    QOpenGLVertexArrayObject QOpenGLBuffer
    So I need QOpenGLContext, but I don't create it explicitly anywhere in my program. And I tried to create it in D-tor but it did not help. I also figured out, that since I use QGraphicsView, then my Context is viewport and I tried to work with that, but it also didn't help. So the main problem is that my program crashes when D-tor is getting called. I don't know how to fix this. I know, that it's not the best approach in Qt+OpenGL, but I can't switch to the better method (like defining my own QOpenGLWidgets)

    One caveat is that I initialize vao, vbo and shaders inside member function using std::call_once inside paint() method, so with that I can be assured that all that stuff is going to be created during a valid context, I don't think that it is a good approach, but I don't know how to do it otherwise.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Qt Graphics Framework and OpenGL

    In my use of QOpenGLVertexArrayObject and QOpenGLBuffer in a 3D plotting library, I create these as non-pointer member variables in each of the classes I use to draw graphical objects in a widget derived from QOpenGLWidget. For example, the class that defines the 3D axes cube, I have these member variables:

    Qt Code:
    1. // Vertex array object for the axis lines and box
    2. QOpenGLVertexArrayObject mAxisVAO;
    3.  
    4. // Holds the vertex coordinates
    5. QOpenGLBuffer mVBO = QOpenGLBuffer( QOpenGLBuffer::VertexBuffer );
    6.  
    7. // Holds the indices for the box
    8. QOpenGLBuffer mBoxIBO = QOpenGLBuffer( QOpenGLBuffer::IndexBuffer );
    9.  
    10. // Holds the indices for the axes
    11. QOpenGLBuffer mAxisIBO = QOpenGLBuffer( QOpenGLBuffer::IndexBuffer );
    12.  
    13. // Vertex array object for the ticks
    14. QOpenGLVertexArrayObject mTickVAO;
    15.  
    16. // Holds the tick vertex coordinates
    17. QOpenGLBuffer mTickVBO = QOpenGLBuffer( QOpenGLBuffer::VertexBuffer );
    18.  
    19. // Holds the indices for the tick vertices
    20. QOpenGLBuffer mTickIBO = QOpenGLBuffer( QOpenGLBuffer::IndexBuffer );
    To copy to clipboard, switch view to plain text mode 

    They are created on the stack during construction and are automatically destroyed in the destructor. The two VAO instances are initialized in the constructor with "this" as the parent. The axis class is derived from QObject. I do not create any OpenGL context.

    The QOpenGLShaderProgram is a pointer member variable in the axis class. It is created and compiled on demand before plotting starts. Because it is derived from QObject, it is also given "this" as a parent so it is automatically destroyed when the axis class instance is destroyed.

    During rendering, first the shader program is bound, then the tick VAO is bound and the ticks rendered, and the tick VAO released. Next the axis VAO is bound and the axes are rendered, and the axis VAO released. Finally the shader program is released. Native QPainter calls are used to draw the labels into a QOpenGLPaintDevice the same size as the widget, which overlays them onto the OpenGL rendering..

    Here's a screenshot of the axes cube.

    Capture.jpg
    Last edited by d_stranz; 14th October 2025 at 18:52.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Qt Coordinate System and the Graphics View Framework
    By djurodrljaca in forum Qt Programming
    Replies: 14
    Last Post: 17th February 2012, 12:19
  2. Replies: 4
    Last Post: 23rd November 2011, 08:18
  3. Issues with the Graphics View Framework
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 26th April 2009, 15:13
  4. about the contextMenuEvent in graphics view framework
    By bingoking in forum Qt Programming
    Replies: 1
    Last Post: 21st April 2009, 07:04
  5. Replies: 4
    Last Post: 5th August 2008, 20:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.