[SOLVED] Overpainting on QOpenGLWidget erases opengl scene
-
I'm trying to render 2D graphics & text using QPainter over my OpenGL scene, using a QOpenGLWidget in Qt 5.4.1. The painter 2D graphics works, but the opengl scene is erased. I've tried to modify the 'hellogl2' sample program by adding a painter->drawText() call and get the same results.
Here's my class definition:
class GLWidget : public QOpenGLWidget, protected QOpenGLFunctionsHere's my paintGL() function:
GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);drawMyOpenGlScene(); // draws 3D 'Q' with a shader, from the sample program QPainter painter(this); painter.drawText(50, 50, "Testing"); painter.end();
}
I see the text, but not the 3D scene. If I comment out the 3 painter lines, I see the 3D 'Q'.
Note that the GLWindow has
setAutoFillBackground(false);How do I get QPainter graphics to overpaint my 3D scene?
-
Hi!
Have a look at this: http://www.informit.com/articles/article.aspx?p=1405557&seqNum=2 . The article explains everything in great detail.
Hope this helps!
-
Excellent article on using QPainter & OpenGL, particularly with the now-deprecated QGLWidget.
However, I'm trying to use QPainter with the new Qt5.4 QOpenGLWidget class. I tried using all the tips from this article with QOpenGLWidget, but it did not work. I also tried following all the overpainting instructions in the QOpenGLWidget documentation.
Does anyone have a sample of using QPainter with QOpenGLWidget ?
I'll need go back to using the old QGLWidget until QOpenGLWidget supports overpainting.
-
Hi again @BHeaney!
Just made a small example. Works for me on Ubuntu 14.10 and Qt 5.4.0.
#ifndef WIDGET_H #define WIDGET_H #include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QMatrix4x4> class Widget : public QOpenGLWidget, protected QOpenGLFunctions { public: Widget(QWidget *parent = 0); void initializeGL(); void resizeGL(int w, int h); void paintGL(); private: QMatrix4x4 m_projection; }; #endif // WIDGET_H
#include "widget.h" #include <QPainter> Widget::Widget(QWidget *parent) : QOpenGLWidget(parent) { QSurfaceFormat format; format.setDepthBufferSize(24); setFormat(format); } void Widget::initializeGL() { initializeOpenGLFunctions(); } void Widget::resizeGL(int w, int h) { m_projection.setToIdentity(); m_projection.perspective(60.0f, w / float(h), 0.01f, 1000.0f); } void Widget::paintGL() { QPainter painter(this); painter.beginNativePainting(); glClearColor(0,0.5,7,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_LINES); glColor3f(1.0,1.0,1.0); glVertex2f(0,0); glVertex2f(200,200); glEnd(); painter.endNativePainting(); // ---- painter.drawText(50, 50, "Testing"); painter.end(); }
-
Your example worked perfectly and is very easy to understand. Thanks!
My shader-rendered object still disappears when I draw anything with QPainter. I think the shader may not be releasing all it's buffers/objects before I draw with QPainter. I'm using the shader code from the hellog2 sample program.
I'll close this issue since you've shown me how to overpaint on top of an OpenGL scene.
-
BHeaney,
Did you ever get this to work with and did you have to use a compatibility profile? Would you mind posting your complete code?
WB