Important: Please read the Qt Code of Conduct -

Access to Widget created in Designer

  • Hello,
    I created a widget project with a mainwindow, then in the designer I created an OpenGL Widget.
    Now how can I use GL functions? because I cannot acces for example to my widget just created:

    ui->myGLwidget-paintGL() is impossibile to use because I didn`t declare it

    Could someone help me?

    Thank you

  • Moderators

    You should not call paintGL() directly. Call update() and Qt will schedule a repaint and call it for you, as described here.

  • Thank you Chris for your answer.
    So, if I create my OpenGL WIdget in Designer, after that I need to override OpenGL function (paint, resize....and so on), but where I have to do that? I have to create a class with the same name of the widget object I created? My problem is that I would like to know what is the relation between my main source -----> my Qwindow (main window) -------------> my opengl widget created in designer.

    Thank you

  • Moderators

    @aidoru said:

    what is the relation between my main source -----> my Qwindow (main window) -------------> my opengl widget created in designer

    By default - none. Adding QSomeWidget in the designer is essentially doing ui->someWidget = new QSomeWidget() somewhere in the setupUi() method, so it has nothing to do with code in whatever class you written yourself.

    This is a case where you would use widget promotion. Create your own QOpenGLWidget derived class in c++ and implement paintGL and the rest as usual. Then, in the designer, add QOpenGLWidget to the layout. Now right click on the added QOpenGLWidget and select "Promote to...". In the dialog fill in the info about your class (class name and header location) and hit "Add" and "Promote". This will make the generated code create an instance of your class instead of the base QOpenGLWidget (thus "promoting it" to be your class). You can access that instance from code via the usual ui->... pointer.

  • Thank you very much Chris