Load/Render UI inside MainWindow

  • I have a Qt Widgets application with 3 main GUI areas, like this image below:

    • Item menu
    • Load UI Area
    • Console


    In the left side, each item x will have some properties, but theses properties are not the same for each item type like (person and house), so they may not share the same input fields.

    So, to user fill (type) the item property, I think that I have to develop a custom user interface for each item type. Right?

    The questions are:
    a) How can I load a *.ui file inside the Load UI area when the user clicks in the item x?
    b) Is there another good way to load these properties GUI inside the Load UI Area?

  • Lifetime Qt Champion

    Without know what the Item A,b,c really are its hard to give advice.
    But if the Items ABC etc are plain UI files with no .h or cpp. you can use
    https://doc.qt.io/qt-5/quiloader.html to load that UI as a widget to be shown.

    Regarding the different properties of each type.
    Qt has a property system and you can actually enumerate what a class has if its a QObject so its
    possible to write an editor that can do that without hardcoding which properties to edit.

    int count = metaobject->propertyCount();
    for (int i=0; i<count; ++i) {
        QMetaProperty metaproperty = metaobject->property(i);
        const char *name = metaproperty.name();
        QVariant value = object->property(name);

  • @mrjj thank you for your quick response...

    Well, each item type (person, house, etc...) should be your own GUI to the user fill the properties input fields.

    I will need to have for each *.ui file the correspondent *.cpp and the *.h files to manage and validate the input fields.


    • person.ui
    • person.cpp
    • person.h

    What is the best way to do that?

  • Lifetime Qt Champion

    Well if you have many types and many properties, i would go with generic editor that
    would loop over the properties and dynamically add lineEdits/widgets to allow altering values.

    If you only have a few, it might be easier just to give them the needed fields and
    let them handling the editing themself pr UI file.

    Those Types, are those data items, or real Widgets with some other GUI than the editing part ?

  • @mrjj said in Load/Render UI inside MainWindow:

    Well if you have many types and many properties, i would go with generic editor that
    would loop over the properties and dynamically add lineEdits/widgets to allow altering values.

    For this a QDataWidgetMapper does the job for you, if you present your data as a Qt model.

  • Lifetime Qt Champion

    Yes so that is why i want to know more about the
    types if they be data item or actual widgets.
    And if the editing of the data is besides some other GUI elements etc.

    You are right that ifs data classes, then a model would fit much better and
    gives all the editing for free.

  • @mrjj Each item type is not only fill Line Edits....
    Each item type has another input fields like checkboxes, sliders, images, etc...So, I think that will be better to have a well defined GUI for each item type...and not use a "dynamic generic editor"...

  • @JonB Each item type is a Qt Widgets Class (so I have the *.ui and the *.cpp and *.h) to do the input data validation, save data, etc...

    I think that will be good to load each Qt Widget class UI on the Load UI area, but I don't know how to do it.
    And I don't know if this idea is the best way to do that.

    Could you help me a little bit more?

  • Lifetime Qt Champion

    Agree if many types of widgets needed for editing, then dynamic is not optimal.

    If you have a fully defined UI + cpp and .h

    you can simply new an instance and put it into the "Load UI Area"

    You could use a QStackedWidget (as UI Area) to have a page for each type to avoid having to exchange them, and make switching fast.

  • @mrjj thank you guys...I will try the QStackedWidget solution!

  • @fem_dev
    I'm sorry, I think we struggled to understand what exactly you were asking for here! As @mrjj has said, if you wish to choose between a number of widgets to place in a single area such that at any time only one is present, QStackedWidget is the way to go.

Log in to reply