PDA

View Full Version : Is there any way to traverse amount of controls in a loop?



mismael85
28th November 2010, 13:35
Hi,
I have fifteen control (i.e QComboBox) , i want to get text from them. is there any way to traverse the controls from a loop?
Thank you

high_flyer
28th November 2010, 14:11
It depends how you defined them.
If you put the pointers in a vector for example - then sure!

mismael85
28th November 2010, 15:23
Here are the problem because i defined them in the UI.

tbscope
28th November 2010, 15:49
Then you can still create a vector list with pointers.


QVector myVector;
myVector.append(ui->combobox1);
myVector.append(ui->combobox2);

for (int i = 0; i < myVector.size(); ++i) {
// Do something with myVector.at(i)
}

franz
28th November 2010, 19:37
Or if you want to run through all those items unconditionally:


foreach (QComboBox *box, findChildren<QComboBox *>()) {
// do the fancy stuff
}

Note that QObject::findChlidren() (http://doc.trolltech.com/latest/qobject.html#findChildren) might recurse into other child widgets to find children that match the type T & name.

tbscope
29th November 2010, 10:31
A correction:
In my post above, it should be:

QVector<QComboBox*> myVector;