Qt World Summit: Register Today!

Using QOpenGLWidget

  • Hello,
    I'm trying to use OpenGL inside of Qt using QOpenGLWidget, but I am having a hard time finding any relevant examples. I am new to OpenGL, so I am trying to learn how to use it, but the tutorials that I find don't seem to apply particularly well in QOpenGLWidget. Right now, all I want to do is render a triangle to start with.

    Here's what I have so far.


    namespace Ui {
    class Widget;
    class Widget : public QOpenGLWidget, protected QOpenGLFunctions
        explicit Widget(QWidget *parent = 0);
        void initializeGL();
        void resizeGL(int, int);
        void paintGL();
        Ui::Widget *ui;


    Widget::Widget(QWidget *parent) :
        ui(new Ui::Widget)
    void Widget::initializeGL()
        // Set up the rendering context, load shaders and other resources, etc.:
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    void Widget::resizeGL(int w, int h)
        // Update projection matrix and other size-related settings:
    void Widget::paintGL()
        // Draw the scene:
        delete ui;

    Is there any example I could use to just render a basic triangle? I tried the one from here: https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf, but it threw a lot of errors that I couldn't work out.

    I also don't know how OpenGL contexts work in QOpenGLWidget.

  • http://www.trentreed.net/blog/qt5-opengl-part-0-creating-a-window/

    Follow thoose tutorials or at least the part 0, 1 ,2 ,3a, 3b and you will have decent basis to start.

    In the tutorial he use QOpenGLWindow which isn't really far from QOpenGLWidget so I think you should be able to adapt his code a bit to suit your need but if you didn't succed to do it ask here again.

    Do you look for opengl ressources to ?