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



  • 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.



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



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

    Any objection/suggestion?



  • 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 ;) ).


Log in to reply
 

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