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);
    file.open(QFile::ReadOnly);
    QWidget *myWidget = loader.load(&file, this);
    file.close();@

    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()":http://qt-project.org/doc/qt-5/qobject.html#children

    You can also get the widget's "layout":http://qt-project.org/doc/qt-5/qwidget.html#layout and then "each item in it":http://qt-project.org/doc/qt-5/qlayout.html#itemAt 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();
    if(w)
    //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 =
    myWidget->findChildren<QLineEdit*>()
    QList<QPushButton*> btns =
    myWidget->findChildren<QPushButton*>()
    @
    Through that you can of course iterate and get all the names.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.