QT & raw OpenGL with glew not working

  • It's been 2 days now that I'm trying to setup a working window where I can issue raw openGL commands with glew, but all I can see on screen is just a white empty window while my glClearColor is set to red

    So here's glwindow.h :

    #ifndef GLWINDOW_H
    #define GLWINDOW_H
    #include <QWindow>
    #include <QOpenGLContext>
    class GLWindow : public QWindow {
        QOpenGLContext* context;
        GLWindow(const QSurfaceFormat& format);
        void initGL();
        void resize(int w, int h);
    #endif // GLWINDOW_H


    #define GLEW_STATIC
    #include <glew.h>
    #include "glwindow.h"
    #include <QDebug>
    GLWindow::GLWindow(const QSurfaceFormat& format) : context(0) {
        setSurfaceType( OpenGLSurface );
        context = new QOpenGLContext(this);
                glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    void GLWindow::resize(int w, int h) { }
    void GLWindow::initGL() { }


    #include <QGuiApplication>
    #include <glwindow.h>
    #include <QSurfaceFormat>
    int main(int argc, char *argv[])
        QGuiApplication a(argc, argv);
        QSurfaceFormat format;
        format.setMajorVersion( 3 );
        #if !defined(Q_OS_MAC)
        format.setMinorVersion( 3 );
        format.setMinorVersion( 2 );
        format.setDepthBufferSize( 24 );
        format.setSamples( 4 );
        format.setProfile( QSurfaceFormat::CoreProfile );
        GLWindow window(format);
        return a.exec();

    Lastly here's the .pro file:

    QT       += core gui 
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = Tentativo5
    TEMPLATE = app
    SOURCES += main.cpp\
    HEADERS  += \
    INCLUDEPATH += "C:/....../OpenGL/headers/GL"
    LIBS += "C:/......./OpenGL/lib/glew32s.lib"
    LIBS += opengl32.lib

    I'm pretty desperate at this point so feel free to ask anything about the setup, I'm really not suited to set all the mess that qt apparently need to have a working opengl window

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    With Qt 5 you don't need glew at all, you can use QOpenGLFunctions and derived class.

  • @SGaist
    Is there a step-bystep tutorial that explains them without too much magic?
    Maybe ending up with a simple openGL window up and running

    PS: Possibly without using QGLWidget since performance-wise seems not to be the best solution

  • Lifetime Qt Champion

    The OpenGL window example might be a good starting point.

  • @SGaist
    Unfortunately I didn't understand many of the called functions in there like the need of exposeEvent or renderlater which was what I was referring before as "magic"

  • Lifetime Qt Champion

    renderLater is used to trigger an update of the scene while exposeEvent will trigger it when the window exposure changed.

  • @SGaist
    Last question, I found a working example here: https://wiki.qt.io/Using-QOpenGLFunctions-and-QOpenGLContext
    which I blindly implemented and played a bit around till I finally rendered a triangle on screen, the problem is that as with QGLWidget, if I resize fast enough I start to see white flickering as shown in this screenshot: http://oi63.tinypic.com/11wgi9t.jpg
    what could be causing it?

    Fun thing is that I switched from qglwidget since I thought the performance impact could have caused it

  • Lifetime Qt Champion

    I can't say from the top of my head. However, QGLWidget is old tech, you should move to the new QOpenGLWidget class directly.

  • @SGaist
    Ok I guess i'll open a different question for this issue, thanks anyway for your help

Log in to reply