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. [SOLVED] Overpainting on QOpenGLWidget erases opengl scene
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Overpainting on QOpenGLWidget erases opengl scene

Scheduled Pinned Locked Moved General and Desktop
opengl
6 Posts 3 Posters 3.9k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    BHeaney
    wrote on last edited by BHeaney
    #1

    I'm trying to render 2D graphics & text using QPainter over my OpenGL scene, using a QOpenGLWidget in Qt 5.4.1. The painter 2D graphics works, but the opengl scene is erased. I've tried to modify the 'hellogl2' sample program by adding a painter->drawText() call and get the same results.

    Here's my class definition:
    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions

    Here's my paintGL() function:

    GLWidget::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    drawMyOpenGlScene();   //  draws 3D 'Q' with a shader, from the sample program
    
    QPainter painter(this);
    painter.drawText(50, 50, "Testing");
    painter.end();
    

    }

    I see the text, but not the 3D scene. If I comment out the 3 painter lines, I see the 3D 'Q'.

    Note that the GLWindow has
    setAutoFillBackground(false);

    How do I get QPainter graphics to overpaint my 3D scene?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!

      Have a look at this: http://www.informit.com/articles/article.aspx?p=1405557&seqNum=2 . The article explains everything in great detail.

      Hope this helps!

      1 Reply Last reply
      1
      • B Offline
        B Offline
        BHeaney
        wrote on last edited by
        #3

        Excellent article on using QPainter & OpenGL, particularly with the now-deprecated QGLWidget.

        However, I'm trying to use QPainter with the new Qt5.4 QOpenGLWidget class. I tried using all the tips from this article with QOpenGLWidget, but it did not work. I also tried following all the overpainting instructions in the QOpenGLWidget documentation.

        Does anyone have a sample of using QPainter with QOpenGLWidget ?

        I'll need go back to using the old QGLWidget until QOpenGLWidget supports overpainting.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Hi again @BHeaney!

          Just made a small example. Works for me on Ubuntu 14.10 and Qt 5.4.0.

          #ifndef WIDGET_H
          #define WIDGET_H
          
          #include <QOpenGLWidget>
          #include <QOpenGLFunctions>
          #include <QMatrix4x4>
          
          class Widget : public QOpenGLWidget, protected QOpenGLFunctions
          {
          public:
              Widget(QWidget *parent = 0);
              void initializeGL();
              void resizeGL(int w, int h);
              void paintGL();
          
          private:
              QMatrix4x4 m_projection;
          };
          
          #endif // WIDGET_H
          
          #include "widget.h"
          
          #include <QPainter>
          
          Widget::Widget(QWidget *parent) : QOpenGLWidget(parent)
          {
              QSurfaceFormat format;
              format.setDepthBufferSize(24);
              setFormat(format);
          }
          
          void Widget::initializeGL()
          {
              initializeOpenGLFunctions();
          }
          
          void Widget::resizeGL(int w, int h)
          {
              m_projection.setToIdentity();
              m_projection.perspective(60.0f, w / float(h), 0.01f, 1000.0f);
          }
          
          void Widget::paintGL()
          {
              QPainter painter(this);
          
              painter.beginNativePainting();
              glClearColor(0,0.5,7,0);
              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          
              glBegin(GL_LINES);
               glColor3f(1.0,1.0,1.0);
                glVertex2f(0,0);
                glVertex2f(200,200);
               glEnd();
              painter.endNativePainting();
          
              // ----
              painter.drawText(50, 50, "Testing");
              painter.end();
          }
          
          1 Reply Last reply
          1
          • B Offline
            B Offline
            BHeaney
            wrote on last edited by
            #5

            Your example worked perfectly and is very easy to understand. Thanks!

            My shader-rendered object still disappears when I draw anything with QPainter. I think the shader may not be releasing all it's buffers/objects before I draw with QPainter. I'm using the shader code from the hellog2 sample program.

            I'll close this issue since you've shown me how to overpaint on top of an OpenGL scene.

            W 1 Reply Last reply
            0
            • B BHeaney

              Your example worked perfectly and is very easy to understand. Thanks!

              My shader-rendered object still disappears when I draw anything with QPainter. I think the shader may not be releasing all it's buffers/objects before I draw with QPainter. I'm using the shader code from the hellog2 sample program.

              I'll close this issue since you've shown me how to overpaint on top of an OpenGL scene.

              W Offline
              W Offline
              Wild Bill
              wrote on last edited by
              #6

              @BHeaney

              BHeaney,

              Did you ever get this to work with and did you have to use a compatibility profile? Would you mind posting your complete code?

              WB

              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