I am developing on RedHat Linux, cat /etc/redhat-release:
Red Hat Enterprise Linux Workstation release 7.2 (Maipo)
I am using Qt Creator 4.3.1:
Based on Qt 5.9.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
The project I'm developing is using Qt 5.6.2 GCC 64bit, the project has been developed with graphical objects derived from QWidget, this includes a live video stream.
Unfortunately we have experienced tearing in the video whilst it is playing back and this is also evident in other widgets displayed around the video, I believe this is because the video is not using vsync.
I believe using openGL will rectify this situation, the aim is to rewrite the widgets including the video playback using openGL. I've spent several days trying to find complete and working solutions but so far failed to find a complete and working solution.
I've been looking at using QOpenGLWidget, in a widget I am using to test:
class clsElevStrip : public QOpenGLWidget, protected QOpenGLFunctions {
Q_OBJECT
In the constructor, I set-up the format for offscreen rendering:
//Create surface format for rendering offscreen
mobjFormat.setDepthBufferSize(24);
mobjFormat.setSamples(4);
mobjFormat.setVersion(3, 0);
mobjFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
setFormat(mobjFormat);
In the paintGL method:
QOpenGLContext* pobjContext = context();
QSurface* pobjSurface = pobjContext->surface();
assert(pobjSurface != NULL);
int intSB1 = pobjSurface->format().swapBehavior();
qDebug() << (QString("paintGL:format: ")
+ QString::number(intSB1));
pobjContext->makeCurrent(pobjSurface);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
pobjContext->swapBuffers(pobjSurface);
Nothing is visible on the main display, the debug statement shows the format as 2 (DoubleBuffering).
If I comment out the line in the constructor:
setFormat(mobjFormat);
The debug statement shows the format as 0 (DefaultSwapBehavior). And the graphics are visible, what have I missed?
The solution for your problem is simple:
Just do not all that QOpenGLGLContext jugging. The whole point of paintGL
is, that this particular function is called inside a wrapper that already does all that context juggling for you. **There is no need to call makeCurrent
or swapBuffers
. Qt already does that for you!
From the Qt documentation
void QOpenGLWidget::paintGL()
This virtual function is called whenever the widget needs to be painted. Reimplement it in a subclass.
There is no need to call makeCurrent() because this has already been done when this function is called.
Before invoking this function, the context and the framebuffer are bound, and the viewport is set up by a call to glViewport(). No other state is set and no clearing or drawing is performed by the framework.
If you have just this as your paintGL
it will show something, iff you have either a compatibility profile >=OpenGL-3.x context OR if you're using a <=OpenGL-2.x context. You're using the legacy fixed function pipeline there, which will not work with OpenGL-3.x core profile contexts!
void glwidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}