Important: Please read the Qt Code of Conduct -

[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) :
    ui(new Ui::MyForm)

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