Nominate our 2022 Qt Champions!

QOpenGL fragment shader

  • Being new to it, I'm trying to run a very simple example of QOpenGL functionalities. I can build a program that runs, but appears to completely ignore the fragment shader even though its compilation completes successfully.

    MainWindow.h, defining the custom widget.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QtOpenGL>
    class MainWindow : public QOpenGLWidget, protected QOpenGLFunctions
        explicit MainWindow(QWidget *parent = 0);
        QOpenGLVertexArrayObject m_vao;
        QOpenGLBuffer m_vbo;
        QOpenGLShaderProgram *m_program;
        void initializeGL();
        void paintGL();
    #endif // MAINWINDOW_H


    // Shader sources
    const GLchar* vertexSource =
        "in vec2 position;"
        "void main()"
        "    gl_Position = vec4(position, 0.0, 1.0);"
    const GLchar* fragmentSource =
        "void main()"
        "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
    MainWindow::MainWindow(QWidget *parent) :
    void MainWindow::initializeGL()
        // Expose GL functions
        // Set clear color
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        // Instantiate program, link shaders, bind program
        m_program = new QOpenGLShaderProgram();
        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource);
        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentSource);
        // Vertex array object
        // Vertex buffer object
        GLfloat vertices[] = {
             0.0f,  0.5f,
             0.5f, -0.5f,
            -0.5f, -0.5f
        m_vbo.allocate(vertices, sizeof(vertices));
        // Specify the layout of the vertex data
        int posAttrib = m_program->attributeLocation("position");
        glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
    void MainWindow::paintGL()
        // Clear the screen to black
        // Draw a triangle from the 3 vertices
        glDrawArrays(GL_TRIANGLES, 0, 3);

    It draws the triangle as it should, but it's white instead of red. If I comment out the line where I compile the fragment shader, I get the same result.
    Main loop just instantiates a QApplication and a MainWindow.
    I've run the hellogl2 example project, which works regularly. Thanks in advance.

  • Start your debugging by checking the return values of link() and bind(). The log() call will return information on errors and warnings. Reference QT Assistant for further detail on the QOpenGLShaderProgram. If all is well after checking those, then the problem is in the assignment of the shader and we'll need to start debugging there.


  • @Orby Forgot to mention it, addShader, link and bind all return true and the log is accordingly empty. Can't really tell if anything's wrong from the docs, it looks in order to me.

  • Does removing the line: m_program->release(); fix the program? It looks like you are setting up the shader, and then destroying it before its actually used.

  • Hi! Releasing the program in the init function is correct but you have to bind it again in the rendering function. Same for the buffers.

  • Sorry I might be sending you on a wild goose chase, I haven't done OpenGL in QT C++ since 4.x, I use QML these days.

    Check out:

    Get that example or one of the many others running, and then work backwards to simply drawing vertex's in a triangle with your shader. Thats going to be your fast way to solve the problem at this point.

  • @Wieland That did it! Thanks everyone :)

  • @Aanok I always find OpenGL code very hard to debug. Glad this solved it! :-)

Log in to reply