PDA

View Full Version : Object Names



slava
15th July 2010, 19:27
If I have let's say 100 line edits and I need to write Hello! to each of those, can I just cycle through the object names with one command in the cycle, something like:


for (int c = 0; c < 100; c++) {

lineEdit_c->setText("Hello!");


}


I know the above example is weird, but is there a way to do something similar? I have something like a hundred lines code working with a tableWidget (taking data from a file and fill in the cells) but then there are 15 tableWidgets needs to be handled in same way. There will be no changeSignal or any other signals emmited.

franz
15th July 2010, 20:10
Make a QList<QLineEdit *>. Then step through the line edits:



QList<QLineEdit *> lineEditors;

...
// you can do this:
foreach (QLineEdit *editor, lineEditors) {
editor->doSomething();
}

// or this:
for (int i = 0; i < lineEditors.count(); i++) {
lineEditors[i]->doSomething();
}

// or use the java-style QMutableListIterator or the standard c++ QList::iterator


Anyway, this is a rather basic thing incorporated in several languages. Do some reading up on C++ (www.cplusplus.com).

slava
15th July 2010, 23:37
Thank you very much. Yeah, I agree I'm missing some basics and should catch up on that. But I have now resolved that particular thing which economized me something like 3000 lines of code, thank you very much again.

aamer4yu
16th July 2010, 05:18
You can also use QObject::children, iterate over them, type cast them into QLineEdit, and if valid set the text to "hello" :)

slava
17th July 2010, 11:24
O, that's goot idea too, thank you.