Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read all widgets from the loaded .ui file
QtWS25 Last Chance

Read all widgets from the loaded .ui file

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amry
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Amry
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved