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 codevoid 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(); ... }
If you delete or comment outglDisable(GL_CULL_FACE);
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. -
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 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 fllowglPushAttrib(GL_ALL_ATTRIB_BITS); glPushClientAttrib(GL_ALL_ATTRIB_BITS); viewer_->frame(); // or OpenGL code glPopAttrib(); glPopClientAttrib();