QPainter and QGLWidget using Qt5.1
-
Hi,
I am experiencing an issue while porting my soft from Qt4.8 to Qt5.1. In my QGLWidget, I was drawing a QImage as background using a QPainter with the following code:
@
MyGLWidget::MyGLWidget(const QGLFormat& _format, QWidget* _parent, Qt::WindowFlags _flags):
QGLWidget(_format, NULL, _flags)
{
setAutoFillBackground(false);
setAutoBufferSwap(false);
}void MyGLWidget::glDraw()
{
QPainter painter(this);QImage background; // filling background painter.setBrush(QBrush(background)); painter.drawRect(0, 0, width(), height()); painter.end(); QGLWidget::glDraw(); swapBuffers();
}
@It was fine with Qt4.8, but fails with Qt5.1. The background remains black, and QPainter::drawRect() generates a GL_INVALID_OPERATION error. Am I missing something?
-
Hi,
I think if you use QPainter this way you should reimplement GLWidget::paintEvent(QPaintEvent *event)
have a look at "Overpainting Example":qthelp://com.trolltech.qt/qdoc/opengl-overpainting.html -
Hi and thanks for your answer. I looked back in the source code for QGLWidget::paintEvent(), which does the following:
@
void QGLWidget::paintEvent(QPaintEvent *)
{
if (updatesEnabled()) {
glDraw();
updateOverlayGL();
}
}
@Unfortunately reimplementing paintEvent() instead of glDraw() will not change anything. In fact I chose glDraw() because paintEvent() isn't called when using updateGL() (if you look at the overpainting example, the method animate() causes repaint using update() and not updateGL()). glDraw() is always called, regardless you're using update() or updateGL().
-
why you put glDraw in paintEvent? the code should be separated, what must be redrawn by calling explicitly updateGL from what is always painted by paintEvent ...
btw, you can set a background by simply calling gl functions:
i.e.
@void GLDrawer::ClearFrameBuffer()
{
qDebug()<<"GLDrawer::ClearFrameBuffer";
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
swapBuffers();
}@ -
I didn't put glDraw() in paintEvent(). This isn't my code, this is paintEvent() implementation of QGLWidget. This is the source code of Qt.
-
ah ok, I have not seen :) ... brr