tags:

views:

45

answers:

2

Hello, I have some form with fiels, combos etc. I would like to go over through all the widgets on the form and if for example it's textfield, clear it, something like that:

foreach(QObject *child, this->ui->children())
{
    QLineEdit *txtField = qobject_cast<QLineEdit *>(child);
    if (txtField)
    {
        txtField->clear();
    }
}

The problem is the ui object doesn't have such property like children and I don't know how to get the whole collection of children widgets.

Maybe the form object has something like Clear method. It would be the best.

Thanks

+2  A: 

How many QLineEdits are we talking about? Unless there's a good reason I would just add a method to the class that has ui as a member and do it by hand, like this:

void clearform()
{
    this->ui->firstlineedit->clear();
    this->ui->secondlineedit->clear();
    ...
    this->ui->nthlineedit->clear();
}

A good reason why you can't do it this way is that the QLineEdits are dynamically added and you don't have them as members. In that case you can call QObject::children on the widget that is the parent of all your QLineEdits, if they share some common name, QObject::findChildren will also work.

Idan K
+2  A: 

Try it.

ui->setupUi(this);

foreach(QLineEdit *widget, this->findChildren<QLineEdit*>()) {
    widget->clear();
}
wrongite
Yeah, it worked for me, thanks! But I had to remove setupUi code because the form has some strange additional widgets inside. I suppose I don't need it because this method was called before.
Seacat