Important: Please read the Qt Code of Conduct -

Drawing with QPainter in QOpenGLWidget

  • I've come across a number of solutions in my Google searches for getting QPainter 2D drawing to work in a QOpenGLWidget, but none seem to work for me using Qt 5.5.

    Here is pseudocode of what I'm doing:

    class MyOpenGLWidget : QOpenGLWidget
    void paintGL() // Qt override
    	QPainter painter(this);
    	// Draw GL
    	painter.setFont(QFont("Arial", 24));
    	painter.drawText(rect(), Qt::AlignCenter, "Test");

    From what I can tell, this is correct syntax, but I'm doing this on my PC (only platform), and the "Test" text does not appear; only the OpenGL rendering displays on the screen.

    I'm able to independently render the OpenGL or "Test" text if I comment out the respective sections, but I have no luck rendering the painter text on top of the OpenGL render.

    Another note, I'm able to draw the "Test" text before the OpenGL calls, but this would obviously result in the text appearing below the OpenGL drawing (and only if I don't do a glClear() of course), which is not what I'm looking to do.

    Anyone else having this problem? Any suggestions on fixing this?

    Thanks in advance.

  • Okay, I managed to narrow down what I was doing wrong. I followed the advice in this Stackoverflow thread:

    The push/pop state and attribs is probably good practice, but like the OP, it didn't solve my problem.
    What solved my problem was the little note in the thread from baysmith. The OP noticed that the VBO wasn't unbound after the VAO was unbound. However, in Qt 5.5, you use a QOpenGLVertexArrayObject::Binder, which automatically unbinds the VAO/VBO once it goes out of scope. Instead, I force released the vaoBinder before endNativePainting(), and it works now.

Log in to reply