Important: Please read the Qt Code of Conduct -

Flip or rotate viewport within QOpenGLWidget in Qt 5.8

  • Hi,

    In Qt 5.8.0 (and msvc 2015), I'm using a subclass of QOpenGLWidget with opengl 3.3 core profile to display images as textures and sometimes I need to rotate the image by multiples of 90 degrees.
    I read that we can use either glPixelZoom or glScalef or other things to rotate, although it's not clear to me to which openGL version that applies. I tried both of them but the app does not compile.

    This compiles and works fine when I do not try to rotate:

    QOpenGLWidget::resizeGL(int w, int h) :
       glViewport( 0, 0, w, h );

    But this does not work, i get a LNK2019 unresolved external symbol:

    QOpenGLWidget::resizeGL(int w, int h) :
       glScalef(1, -1, 1); // Or glPixelZoom(1, -1) 
       glViewport( 0, 0, w, h );

    Maybe glScalef() and glPixelZoom() are not for openGL 3.3? If that's true, how may I simply display a rotated or flipped texture in resizeGL() with OpenGL 3.3 core profile? In addition, should I setup the flip / rotation in paintGL() in addition to / instead of resizeGL()?

    If that's relevant, here's my list of includes:

    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QtGui/QWindow>
    #include <QtGui/QOpenGLFunctions>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QOpenGLVertexArrayObject>
    #include <QOpenGLBuffer>
    #include <QOpenGLTexture>
    #include <QScreen>

    Thank you

  • Hi there

    You cannot use glScalef() in the core profile. You now rotate the images using a rotation matrix in the paintGL() method (not in resizeGL(...) ) and then pass it to the vertex shader where the rotation is applied. Something like this:

    void MyGL::paintGL()
        QMatrix4x4 view;
        view.rotate(90, 1.0f, 0.0f, 0.0f ); //90 degree around x axis
        view.scale(scaleX, scaleY, scaleZ);// also do some scaling if you want
        glUniformMatrix4fv(glGetUniformLocation(program , "matrixRotation"), 1, false, view.constData());// send the matrix to the shader

    Now in the vertex shader:

    uniform mat4 matrixRotation;
    layout (location = 0) in vec4 position;
    void main()
        gl_Position = matrixRotation * position;

Log in to reply