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. QOpenGLWidget and QPainter
Forum Updated to NodeBB v4.3 + New Features

QOpenGLWidget and QPainter

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.8k Views 1 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.
  • Qt_crazyerQ Offline
    Qt_crazyerQ Offline
    Qt_crazyer
    wrote on last edited by
    #1

    Hi, I want to draw some 2D grath on my qopenglwidget by qpainter. But I don not know how to blend opengl and qpainter. And I've made some attempts. 2D and 3D can be displayed individually, not together. Override the paintEvent()? Or glPushAttrib(GL_ALL_ATTRIB_BITS) and glPopAttrib()?
    I have some confusion. How should I modify the code?
    Thanks in advance.
    Qt Creator 5.9.1 on windows 10.

    void my_OpenGL::paintGL()
    {
        float currentFrame = glutGet(GLUT_ELAPSED_TIME); 
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;
    
        const qreal retinaScale = devicePixelRatio();
        glViewport(0, 0, width() * retinaScale, height() * retinaScale);
    
        QPainter ptr(this);
        ptr.beginNativePainting();
    
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
        /******3D.begin*******/
        m_program->bind();
        glBindVertexArray(VAO);
    
        m_program->setUniformValue(m_program->uniformLocation("projection"), projection);
        m_program->setUniformValue(m_program->uniformLocation("view"), view);
        m_program->setUniformValue(m_program->uniformLocation("model"), model);
    
        m_program->setUniformValue(m_program->uniformLocation("color"), QVector3D(190.0, 190.0, 190.0));
        glDrawElements(GL_TRIANGLES, elementVector.count()*3, GL_UNSIGNED_INT, 0);
    
        m_program->setUniformValue(m_program->uniformLocation("color"), QVector3D(0.0, 0.0, 0.0));
        glDrawElements(GL_LINES, elementVector.size()*3, GL_UNSIGNED_INT, 0);
    
        m_program->release();
        /******3D.end*******/
    
        ptr.endNativePainting();
    
        ptr.fillRect(QRect(geometry().x(), geometry().y(), width(), height()), backgroundBrush);
    
        update();
    }
    
    S 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The 2D painting example might be what you are looking for.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Qt_crazyerQ Offline
        Qt_crazyerQ Offline
        Qt_crazyer
        wrote on last edited by
        #3

        Thank you for your reply.
        Yes, I have noticed this example. But it just tell us how to draw 2D graphics primitives onto paint devices. I have learned it. And now I want to know how to draw 3D graphics on the basis of the 2D graphics background. For instance, I want to add a background of a gradual color for my 3D graphics.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I may have misunderstood your goals.

          Can you give an example/screenshot of what you are trying to do ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Qt_crazyerQ Offline
            Qt_crazyerQ Offline
            Qt_crazyer
            wrote on last edited by
            #5

            0_1521677877529_BaiduShurufa_2018-3-22_8-17-23.png
            Just like this. My idea is drawing the blade by OpenGL, and the others by QPainter, including the text, the background, the color bar and the scale. Now I can draw it individually, but fail to combine together.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              From the looks of it, you might want to consider QtQuick for the UI part. Could prove easier.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Qt_crazyerQ Qt_crazyer

                Hi, I want to draw some 2D grath on my qopenglwidget by qpainter. But I don not know how to blend opengl and qpainter. And I've made some attempts. 2D and 3D can be displayed individually, not together. Override the paintEvent()? Or glPushAttrib(GL_ALL_ATTRIB_BITS) and glPopAttrib()?
                I have some confusion. How should I modify the code?
                Thanks in advance.
                Qt Creator 5.9.1 on windows 10.

                void my_OpenGL::paintGL()
                {
                    float currentFrame = glutGet(GLUT_ELAPSED_TIME); 
                    deltaTime = currentFrame - lastFrame;
                    lastFrame = currentFrame;
                
                    const qreal retinaScale = devicePixelRatio();
                    glViewport(0, 0, width() * retinaScale, height() * retinaScale);
                
                    QPainter ptr(this);
                    ptr.beginNativePainting();
                
                    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
                
                    /******3D.begin*******/
                    m_program->bind();
                    glBindVertexArray(VAO);
                
                    m_program->setUniformValue(m_program->uniformLocation("projection"), projection);
                    m_program->setUniformValue(m_program->uniformLocation("view"), view);
                    m_program->setUniformValue(m_program->uniformLocation("model"), model);
                
                    m_program->setUniformValue(m_program->uniformLocation("color"), QVector3D(190.0, 190.0, 190.0));
                    glDrawElements(GL_TRIANGLES, elementVector.count()*3, GL_UNSIGNED_INT, 0);
                
                    m_program->setUniformValue(m_program->uniformLocation("color"), QVector3D(0.0, 0.0, 0.0));
                    glDrawElements(GL_LINES, elementVector.size()*3, GL_UNSIGNED_INT, 0);
                
                    m_program->release();
                    /******3D.end*******/
                
                    ptr.endNativePainting();
                
                    ptr.fillRect(QRect(geometry().x(), geometry().y(), width(), height()), backgroundBrush);
                
                    update();
                }
                
                S Offline
                S Offline
                SubyfanJack
                wrote on last edited by
                #7

                @Qt_crazyer

                You seem to be on the right path. But if you're using QPainter::fillRect() to paint the background, you'd want to do it BEFORE your OpenGL 3D painting code, not after. Otherwise fillRect() would wipe out all your 3D painting.

                It's OK to mix QPainter and OpenGL painting in QOpenGLWidget::paintGL() in any order, as long as OpenGL parts are enclosed by beginNativePainting() and endNativePainting(). You can do 1) QPainter::fillRect() background 2) OpenGL 3D 3) more QPainter painting for other 2D objects over the background.

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  rbharadw
                  wrote on last edited by
                  #8

                  I have a similar thread with no responses to it. Please can you let me know how this problem was fixed. My thread: Rendering 2D with QPainter and QOpenGLWidget

                  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