Loop partly on variable names

Hi,

I have some codes including a series of instructions like follows:
1
2
3
4
5
	ui.lineEdit_1->setText(QString::number(param[0]));
	ui.lineEdit_2->setText(QString::number(param[1]));
	ui.lineEdit_3->setText(QString::number(param[2]));
	ui.lineEdit_4->setText(QString::number(param[3]));
        // continue... 


Here, ui is a structure name, lineEdit_1 is name of an object, param is an array. I have to repeat this type of instruction a lot, from 1 to 50. How can I create a loop to replace this stupid repetition, please?
Thanks a lot!
Doesn't QObject have a findchild function or something?
Well I am relatively new to Qt, so I don't really know how to deal with it. Can anyone please show me?
Why do you have 50(!) different named members of the same type in the structure?

1
2
3
4
5
6
7
8
9
10
struct type_of_ui
{
    // ...
    std::vector<smart pointer> line_edit ;
};

type_of_ui ui /* ... */ ;
// ...
for( std::size_t i = 1 ; i < line_edit.size() ; ++i ) 
    ui.line_edit[i]->setText( QString::number(param[i]) );
He probably has designed a form with Qt Designer or Creator.

Read about findChild()
http://qt-project.org/doc/qt-4.8/qobject.html
That allows iteration over array and corresponding widgets.
> He probably has designed a form with Qt Designer or Creator.

That explains it! The archetypical anti-C++ library, widely used, generating oodles of Java-like spaghetti masquerading as C++ code.
Thanks for your replies. Yes, I am doing gui with Qt but main code is still in C++. I am reading the doc related to findchild... :)
Last edited on
Topic archived. No new replies allowed.