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
QtWS25 Last Chance

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.
  • Y Offline
    Y Offline
    ytyisme
    wrote on 31 Jul 2017, 13:46 last edited by ytyisme 8 Jan 2017, 02:16
    #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.

    Y 2 Replies Last reply 31 Jul 2017, 13:58
    0
    • Y ytyisme
      31 Jul 2017, 13:46

      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.

      Y Offline
      Y Offline
      ytyisme
      wrote on 31 Jul 2017, 13:58 last edited by
      #2

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

      1 Reply Last reply
      0
      • Y ytyisme
        31 Jul 2017, 13:46

        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.

        Y Offline
        Y Offline
        ytyisme
        wrote on 2 Aug 2017, 07:04 last edited by
        #3

        @ytyisme It seems that the problem is no solution ah

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Aug 2017, 11:25 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 3 Aug 2017, 00:18 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();
            
            Y 2 Replies Last reply 3 Aug 2017, 02:04
            0
            • K kenchan
              3 Aug 2017, 00:18

              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();
              
              Y Offline
              Y Offline
              ytyisme
              wrote on 3 Aug 2017, 02:04 last edited by
              #6

              @kenchan I tried it and did not work

              1 Reply Last reply
              0
              • K kenchan
                3 Aug 2017, 00:18

                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();
                
                Y Offline
                Y Offline
                ytyisme
                wrote on 3 Aug 2017, 03:18 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 3 Aug 2017, 03:27 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

                  3/8

                  2 Aug 2017, 07:04

                  topic:navigator.unread, 5
                  • Login

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