Read all widgets from the loaded .ui file

  • hi guys,

    I would like to get all the widgets that are presented in .ui loaded file.
    So I am loading the file like this:
    @QUiLoader loader;
    QFile file(fname);;
    QWidget *myWidget = loader.load(&file, this);

    And afterwards I do not know how to get the list of widgets.
    Any help really appreciated.

  • Moderators

    Do you mean you want instances of them, QObject names of them or the classes names?

    myWidget is a QObject like any other. You can get the children list via "children()":

    You can also get the widget's "layout": and then "each item in it": and so forth.

    QUIloader has also methods for obtaining names of classes and layouts in the file.

  • Hi Chris,

    Thanks for respond.

    I have played around with children method.
    I guess, I would like to have QObject names, since I want to know which widget they are to work further with them.
    But children() gives me a lot other widgets like scroolarea_hcontainer, scrollarea_vcontainer and others,
    but I have only QLineEdit, and QPushButton, and I would like to get only these two objects.
    Can you please show me how I can get widgest layout and each item in it? I guess it will give what I am searching?

    Thanks forward.

  • Moderators

    You can use the methods I already linked for traversing layouts if you want to know the structure (what is in which container etc.).
    //Pack it into recursive function
    QLayout *layout = myWidget->layout();
    if (layout) {
    for (int i = 0; i < layout->count(); ++i) {
    QWidget w = layout->itemAt(i)->widget();
    //do smthn with w or go deeper via w->layout()

    You can also use something like this to get all children of specific type.
    @QList<QLineEdit*> lineEdits =
    QList<QPushButton*> btns =
    Through that you can of course iterate and get all the names.

Log in to reply