Nominate our 2022 Qt Champions!

Can I use my widgets created with code in the *.ui file

  • Hi everybody!
    I have a project in Qt made with the QWizard and QWizardPage classes. There are two ways to create a widget i.e: a Label:

    One is going to the *.ui file and search the element and put it where you want (visual way). Then you can access it on your code with


    The other one is going to your code and creating it like

    QLabel codedLabel;

    Actually I'm using the second way (it's easier for me to create, show and use) but my question is: Is there any way that I can see my label codedLabel on the *.ui file?

    I would like to move it to a space in the screen and in that case, it would be much easier for me to be able to do it through the visual way (but having the label created in the code instead of the ui).

    Thank you so much.

  • Moderators

    I'm pretty sure this is not possible. A form (the *.ui files) is just an XML definition that is translated by the uic (user interface compiler) into a C++ class. That being said, uic knows nothing of your code or what object you create, it just provides you with the class corresponding to the form.

    Kind regards.

  • @kshegunov Thanks for your reply. That's what I was afraid of... but thought that maybe there was a way that I didn't saw.

    Then... let me ask you another thing... I have a really big project with aprox 40 QWizardPage class forms (like a c++ class but with a *.ui file for each one). I used that class because I started learning Qt with no idea and a mate thought this would be the best class cause I could use the visual ui... but not. So my question is: can I delete now all that *.ui files and still be able to run my program? (Of course I have already commented every mention to a *.ui in the code).


  • Moderators

    Provided you never use the classes generated by the uic (these in the Ui namespace), then the answer is yes. Forms are optional, I do prefer them, but if you don't Qt will not stop you from creating your widgets and layouts in the source code.

  • @kshegunov why do you prefer using the ui files? I mean... a part from the easy "plug and play" way, is it better for the program like more optimized or somethhing like this...

    As I don't use them, I would like to delete them to try and make the executable weight less.

  • Moderators

    I prefer them because it spares me writing a lot of code for positioning, layout, margins, borders and the like. They certainly are not more optimized than handwritten code, but the class the uic generates is practically the same as I would have done it. All in all, the weight of using forms, is not more than doing it by hand, but it does spare you a lot of headaches (most of the time).

  • @kshegunov I see your point and yes maybe it "weights" the same (dunno the english word in that case sorry) but now I have both: the code and the empty uis so why keep them both? That's my point of deleting the uis :)

  • Moderators

    You probably don't need them both, since you've decided to do the coding yourself. It is, as I said, optional to use forms.

Log in to reply