[Solved] Trying QT5 + glew + QOpenGLContext, need help with error on glewInit
-
I'm trying to use glew with Qt 5 but my code is failing on the call to glewInit with error "Missing GL version".
I built a debug version of glew and stepped into the glewInit call. It is failing when it is trying to get the OpenGL version context when calling glGetString. glGetString is returning 0.
I know that glew is not necessary because QOpenGLFunctions exists but I would much rather use glew if I can. I'm creating a QWindow and QOpenGLContext for my window and OpenGl context. I have checked that the version of the created window and OpenGL context does match my requested version.
Things I've tried:
-
Defining QT_NO_OPENGL before including QOpenGLContext header but then got these compilation errors:
..\main.cpp(24) : error C2512: 'QOpenGLContext' : no appropriate default constructor available
..\main.cpp(25) : error C2027: use of undefined type 'QOpenGLContext' -
Created a QWindow-derived class so I could move the glewInit() call to be when the the window is exposed (in exposeEvent()). Received same "Missing GL version" error.
Did a search online but didn't find an answer. It looks like it's been done before. Is it that glew and QOpenGLContext can't be used together? That I need to write my own code to create context?
I pasted example code below. Any suggestions or help would be appreciated. Thanks.
This is what I'm using:
Qt 5.3.2 for Windows 64-bit (VS 2013, OpenGL).
Qt Creator 3.2.1.
GLEW 1.11.0, linking to 64-bit static version
Visual Studio 2013 Express
Windows 7 64-bitmain.cpp
@#define GLEW_STATIC 1
#include <GL/glew.h>#include <QGuiApplication>
#include <QDebug>
#include <QOpenGLContext>
#include <QWindow>int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);QSurfaceFormat requestedFormat;
requestedFormat.setVersion( 3, 3 );QWindow * window = new QWindow;
window->setFormat( requestedFormat );
window->show();QOpenGLContext * context = new QOpenGLContext;
context->setFormat( requestedFormat );
context->create();GLenum err = glewInit();
if( GLEW_OK != err ){
qDebug() << "[Error] GLEW failed to initialize. " << (const char*)glewGetErrorString(err);
}return a.exec();
}@
</code>pro file:
@QT += core guiTARGET = TEST
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
HEADERS +=
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../global/OpenGL/lib/x64/ -lglew32s
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../global/OpenGL/lib/x64/ -lglew32sd
else:unix: LIBS += -L$$PWD/../../../global/OpenGL/lib/x64/ -lglew32sINCLUDEPATH += $$PWD/../../../global/OpenGL/include@
-
-
You're not making the created context current, so basically glewInit is called without active OpenGL context. Call context->makeCurrent(window) before glewInit and context->doneCurrent() afterwards.
-
Thank you. That worked.