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. [Solved] How to iterate all widgets in a QWidget window?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to iterate all widgets in a QWidget window?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 25.4k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    stuartx
    wrote on last edited by
    #1

    Hi, I created an UI form based on QWidget, then added some widgets in the form including some customized widgets. In the constructor of the UI class, I want to iterate through all the widgets and process on my customized widgets. I use the Focus Chain to loop through all widgets in the UI form. This is what I do:

    @MyForm::MyForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyForm)
    {
    ui->setupUi(this);

    QWidget *w = this;
    while (w)
    {
        MyCustomeWidget *d = qobject_cast<MyCustomeWidget *>(w);
        if (d) {
              //process on d
        }
        w = w->nextInFocusChain();
        if (w == this) break;
    }
    

    }@

    I am wondering if this is the right way to do this or if there is better way to perform the iteration?

    Thanks.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      http://doc.qt.nokia.com/4.8-snapshot/qobject.html#findChild
      @
      this->findChild<MyCustomWidget*>();
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stuartx
        wrote on last edited by
        #3

        Thanks. I think I should do:
        @QList<MyCustomeWidget*> list = this->findChildren<MyCustomeWidget *>();
        foreach(MyCusteWidget *w, list) {
        //process w
        }@

        Any objection/suggestion?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          Not at all, that was an example. I didn't know how many of those you had so I settled with the easier variant to write down, thinking you would adapt it appropriately (and you did ;) ).

          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