Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing with QPainter in QOpenGLWidget

Drawing with QPainter in QOpenGLWidget

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aer0ace
    wrote on last edited by
    #1

    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);
    	
    	painter.beginNativePainting();
    	
    	// Draw GL
    	
    	painter.endNativePainting();
    	
    	painter.setPen(Qt::red);
    	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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aer0ace
      wrote on last edited by aer0ace
      #2

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

      http://stackoverflow.com/questions/4316119/using-qpainter-over-opengl-in-qglwidget-when-using-shaders

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved