Draw 2D graphics onto a QOpenGLWidget subclass using QPainter



  • 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.



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



  • @ytyisme It seems that the problem is no solution ah


  • Lifetime Qt Champion

    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 ?



  • 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();
    


  • @kenchan I tried it and did not work



  • @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();
    


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.