Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved] Over painting OpenGL
epalmero last edited by
I am doing a scientific application and for that I need to draw in OpenGL and at the same time draw some Text with the standard QPainter. I have seem some examples like the one in this page "http://qt-project.org/doc/qt-4.8/opengl-overpainting.html" but I don't manage to get mine working.
My paintEvent look like this:
qglClearColor(getBackGroundColor()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); paintGL(); QPainter painter(this); painter.drawRect(1, 1, 100, 100); painter.end();
and in PaintGL I just call the OpenGl stuff. The problem is that I can't see the rectangle. If I remove the call to paintGL then I see it.
Is there someone who has doing this before?
Best and thanks
What are you doing in paintGL ?
kenchan last edited by
Which OpenGL widget are you using?
I think you need to call painter.beginNativePainting() before the open GL stuff
and painter,endNativePainting() after it.
That is what I do and it that works with a QGLWidget. I have no experience with the newer QOpenGLWidget however.
KiNgFrUiT last edited by
The code snippet you posted looks correct, without more detail it is hard to diagnose. I use the same technique with a QGLWidget based class.
If your text is not changing often then you could render it into an OpenGL texture. You only need to upload the texture data again when your text changes. This is also useful for 2d HUDs etc.
@void GLVideoWidget::setOverlay(QImage *overlay)
overlayTextureWidth_ = TM_GetNextPowerOf2(overlay->width());
overlayTextureHeight_ = TM_GetNextPowerOf2(overlay->height());
renderFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_, format);
textureFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_);
renderFBO_ = new QGLFramebufferObject(overlayTextureWidth_, overlayTextureHeight_);
textureFBO_ = renderFBO_;
fboSetup_ = true;
// draw the image on the render fbo
fboPainter.drawImage(0, 0, *overlay);
// copy from the render fbo to the texture fbo
if(renderFBO_ != textureFBO_)
QRect rect(0, 0, renderFBO_->width(), renderFBO_->height());
QGLFramebufferObject::blitFramebuffer(textureFBO_, rect, renderFBO_, rect);
To draw the texture add the following to the end of your paintGL function
@// draw the fbo overlay
if(useOverlay_ && textureFBO_)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f (0, 1); glVertex2f (0.0, 0.0 ); // flip image upside down using the tex coords
glTexCoord2f (1, 1); glVertex2f (textureFBO_->width(), 0.0 );
glTexCoord2f (1, 0); glVertex2f (textureFBO_->width(), textureFBO_->height());
glTexCoord2f (0, 0); glVertex2f (0.0, textureFBO_->height());
epalmero last edited by
thanks for your answer I have found the problem and it was a did not free the Shaders and the OpenGLBuffer.
Thanks a lot