Important: Please read the Qt Code of Conduct -

How to pass a parameter to a widget created by designer

  • Hi everyone!

    I'm currently working on a project where I need to integrate an OpenGL widget to a window. Hence I've created an UI with the designer where I've put my widget, inheriting the QGLWidget class. The default constructor, generated after editing the window on Qt designer, only takes the parent widget as a parameter for construction. Thus I would like to also pass the format to the widget constructor but it is a non constant parameter that I need to initialize before calling the constructor, and yet the constructor is automatically called when setting up the UI.

    Does anyone know how I can give the format once set during initialization to the constructor called by UI init?

    Thanks a lot!

  • Hi,

    there's a way to pass this parameter via a set method? or it's mandatory to pass in contructor??

  • Hi mcosta!

    Actually the setFormat method is deprecated, I tried to call it from my widget constructor but it either generates a segfault (when trying to set the format to OpenGL 3.3) or doesn't change anything since the value of GL_VERSION is the same as when the widget format is not set.

    So I guess the only way is to give my format to the constructor!

  • Hi,

    I think the only option is to create it at runtime manually, instead of using directly code generated from your UI. You may copy code directly from your compiled file myForm_ui.h, and modify it to your needs. You can indeed integrate this code into a form-based widget class.

    If you really want to stick with QtDesigner, you can create a wrapper widget, with a setter for your parameter, delaying the OpenGL widget or rebuilding it from inside your widget. It can then be used in Designer using the right click "Promote to...", or by creating a plugin for your widget, to integrate it to Designer.

    But I do recommand the first solution, which is much easier !

  • Salut Adrien (plus simple en français!)

    Je vais essayer ça de suite, merci beaucoup pour ta réponse!

  • Pas de quoi, tiens nous au courant :)

  • Salut,

    Il n'y a pas vraiment de moyen de le passer d'autres arguments dans le constructeur si tu utilises designer, je te conseilles d'utiliser une methode publique dans ton objet et le faire en deux temps.

    -passer paramètres (fonction publique)

    Bonne chance!

  • Well Adrien's advice did the trick!

    I used the compiled myWindow_ui.h to get the existing code and integrated it directly in my MainWindow constructor, actually I was sure I would have many issues by doing it both by scripting and by using designer, whereas it happened to be quite easy!

    All I had to do was to rewrite my widget constructor to pass the format argument, call it in the constructor and manually integrate my widget to the layout created with designer.

    Thanks a lot everyone!

  • Glad it helped :)
    Happy coding with Qt !

  • Lifetime Qt Champion

    Hi guys,

    Please keep the language of the threads in english, it's the official language for the forum except the regional forums. You can always ask our forum chief to create one for french.

Log in to reply