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. Draw 2D graphics onto a QOpenGLWidget subclass using QPainter

Draw 2D graphics onto a QOpenGLWidget subclass using QPainter

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.3k 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.
  • ytyismeY Offline
    ytyismeY Offline
    ytyisme
    wrote on last edited by ytyisme
    #1

    I found that in paintGL() QPainter affected by the OpenGL state.
    My Qt version is 5.9.1_VS2015_X86.
    If you had install Qt5.9.1, you will find qopenglwidget under the Qt5.9.1\Examples\Qt-5.9.1\opengl folder. qopenglwidget is a example show that how to Draw 2D graphics onto a QOpenGLWidget subclass using QPainter.
    The following is the original code

    void GLWidget::paintGL()
    {
        createBubbles(bubbleNum - m_bubbles.count());
    
        QPainter painter;
        painter.begin(this);
    
        painter.beginNativePainting();
    
        ...
    
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_CULL_FACE);
    
        painter.endNativePainting();
    
        if (m_showBubbles)
            foreach (Bubble *bubble, m_bubbles) {
                bubble->drawBubble(&painter);
            }
    
        ...
    
        painter.end();
    
        ...
    }
    

    0_1501507701176_1.png
    If you delete or comment out

    glDisable(GL_CULL_FACE);
    

    0_1501507822734_2.png
    As you see QPainter draw the bubble is gone.
    Now I want to draw OSG on QOpenglwidget, because the state of OpenGL can not be controlled, so QPainter sometimes draws no graphics. How to solve this problem?
    link textHere's the code I'm using
    My English is relatively bad, these are all translated by Google.
    Any help would be much appreciated.

    ytyismeY 2 Replies Last reply
    0
    • ytyismeY ytyisme

      I found that in paintGL() QPainter affected by the OpenGL state.
      My Qt version is 5.9.1_VS2015_X86.
      If you had install Qt5.9.1, you will find qopenglwidget under the Qt5.9.1\Examples\Qt-5.9.1\opengl folder. qopenglwidget is a example show that how to Draw 2D graphics onto a QOpenGLWidget subclass using QPainter.
      The following is the original code

      void GLWidget::paintGL()
      {
          createBubbles(bubbleNum - m_bubbles.count());
      
          QPainter painter;
          painter.begin(this);
      
          painter.beginNativePainting();
      
          ...
      
          glDisable(GL_DEPTH_TEST);
          glDisable(GL_CULL_FACE);
      
          painter.endNativePainting();
      
          if (m_showBubbles)
              foreach (Bubble *bubble, m_bubbles) {
                  bubble->drawBubble(&painter);
              }
      
          ...
      
          painter.end();
      
          ...
      }
      

      0_1501507701176_1.png
      If you delete or comment out

      glDisable(GL_CULL_FACE);
      

      0_1501507822734_2.png
      As you see QPainter draw the bubble is gone.
      Now I want to draw OSG on QOpenglwidget, because the state of OpenGL can not be controlled, so QPainter sometimes draws no graphics. How to solve this problem?
      link textHere's the code I'm using
      My English is relatively bad, these are all translated by Google.
      Any help would be much appreciated.

      ytyismeY Offline
      ytyismeY Offline
      ytyisme
      wrote on last edited by
      #2

      @ytyisme Does anybody know how to make QPainter and paintGL() harmonious coexistence

      1 Reply Last reply
      0
      • ytyismeY ytyisme

        I found that in paintGL() QPainter affected by the OpenGL state.
        My Qt version is 5.9.1_VS2015_X86.
        If you had install Qt5.9.1, you will find qopenglwidget under the Qt5.9.1\Examples\Qt-5.9.1\opengl folder. qopenglwidget is a example show that how to Draw 2D graphics onto a QOpenGLWidget subclass using QPainter.
        The following is the original code

        void GLWidget::paintGL()
        {
            createBubbles(bubbleNum - m_bubbles.count());
        
            QPainter painter;
            painter.begin(this);
        
            painter.beginNativePainting();
        
            ...
        
            glDisable(GL_DEPTH_TEST);
            glDisable(GL_CULL_FACE);
        
            painter.endNativePainting();
        
            if (m_showBubbles)
                foreach (Bubble *bubble, m_bubbles) {
                    bubble->drawBubble(&painter);
                }
        
            ...
        
            painter.end();
        
            ...
        }
        

        0_1501507701176_1.png
        If you delete or comment out

        glDisable(GL_CULL_FACE);
        

        0_1501507822734_2.png
        As you see QPainter draw the bubble is gone.
        Now I want to draw OSG on QOpenglwidget, because the state of OpenGL can not be controlled, so QPainter sometimes draws no graphics. How to solve this problem?
        link textHere's the code I'm using
        My English is relatively bad, these are all translated by Google.
        Any help would be much appreciated.

        ytyismeY Offline
        ytyismeY Offline
        ytyisme
        wrote on last edited by
        #3

        @ytyisme It seems that the problem is no solution ah

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

          Hi,

          It rather looks like no one here tried that. Did you also took a look at the OSG forum ? Or tried to contact the example author ?

          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
          • K Offline
            K Offline
            kenchan
            wrote on last edited by
            #5

            Just a thought but did you try using the painter save and restore, like this
            I am not sure if it does the same thing as beginNativePainting does anyway but I did use it once (long ago) and it made some kind of difference.
            Might be worth a try in your case too.

            painter.save();
            painter.beginNativePainting();
            ...
            painter.endNativePainting();
            painter.restore();
            
            ytyismeY 2 Replies Last reply
            0
            • K kenchan

              Just a thought but did you try using the painter save and restore, like this
              I am not sure if it does the same thing as beginNativePainting does anyway but I did use it once (long ago) and it made some kind of difference.
              Might be worth a try in your case too.

              painter.save();
              painter.beginNativePainting();
              ...
              painter.endNativePainting();
              painter.restore();
              
              ytyismeY Offline
              ytyismeY Offline
              ytyisme
              wrote on last edited by
              #6

              @kenchan I tried it and did not work

              1 Reply Last reply
              0
              • K kenchan

                Just a thought but did you try using the painter save and restore, like this
                I am not sure if it does the same thing as beginNativePainting does anyway but I did use it once (long ago) and it made some kind of difference.
                Might be worth a try in your case too.

                painter.save();
                painter.beginNativePainting();
                ...
                painter.endNativePainting();
                painter.restore();
                
                ytyismeY Offline
                ytyismeY Offline
                ytyisme
                wrote on last edited by
                #7

                @kenchan Thank you for the idea, I put the opengl state before the painting to save, after drawing and then pop up, just fine.
                Thank you very much
                Just as fllow

                    glPushAttrib(GL_ALL_ATTRIB_BITS);
                    glPushClientAttrib(GL_ALL_ATTRIB_BITS);
                
                    viewer_->frame(); // or OpenGL code
                
                    glPopAttrib();
                    glPopClientAttrib();
                
                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  kenchan
                  wrote on last edited by
                  #8

                  Great! I am glad to hear you got it working. Pleased to know I could be of some help :-)

                  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