Setting the OpenGL Version in Qt

Masa
Masa
Jul 20, 2017 · 1 min read

a.k.a. Stupid OpenGL tricks in Qt, Part 1

Sometimes, it’s too hard to build a GLFW application, and it’s just easier to do it in Qt. Or, maybe, just stick with what you know. GLFW is cool, but it’s more of a prototyping/learning framework. So, if you know enough Qt, why not do use that?

I was starting up my application, and getting syntax errors in my GLSL. Really? Turns out, my OpenGL version was 2.1 and my GLSL version was 1.2. Current-ish versions, as of today, are 4.x and 2.x, and, even though my Macbook Pro is a little old, according to Apple, it supported 4.1.

initializeOpenGLFunctions();
qDebug() << "version: "
<< QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION)));
qDebug() << "GSLS version: "
<< QLatin1String(reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)));
// Prints out:
shigmas@kohaku:[~/src/qt/sb6/simpleTri]$ simpleTri.app/Contents/MacOS/simpleTri
NoProfile
version: "2.1 INTEL-10.25.17"
GSLS version: "1.20"

So, let’s start playing around with QSurfaceFormat:

QSurfaceFormat format;
format.setVersion(4,1);
PrintProfile(format); // Prints out the QSurfaceFormat::OpenGLContextProfile

No dice. Exactly the same thing. I think it’s because it’s set in compatibility mode. Even though it says “NoProfile”, it’s being backwards compatible to 2.1. So, the trick was to set the profile to “CoreProfile”, forcing us to use the current one:

QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4,1);
PrintProfile(format); // Prints out the QSurfaceFormat::OpenGLContextProfile

And, running it this time:

shigmas@kohaku:[~/src/qt/sb6/simpleTri]$ simpleTri.app/Contents/MacOS/simpleTri 
CoreProfile
version: "4.1 INTEL-10.25.17"
GSLS version: "4.10"

A simple note, which, if you’re reading this, you almost certainly know already: Core Profile is distinguished with Fixed Pipeline. Fixed Pipeline is incompatible from OpenGL ES.

)

Written by

Masa

QCog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade