PDA

View Full Version : HOw to read Line edit through a for loop



raghvendramisra
2nd August 2007, 11:44
Hi have an application in which there are about 50 lineEdit boxes naming from E1 -E50 in my form.
On execution the user has to enter some values into these boxes and these values are to be stored in a file.
i Tried readig these valuses in my code through for loop but could,nt suceed.So i am reading it one by one as
E1->text();
E2->text();
and so on
So is there a way to read this boxes using for loop

marcel
2nd August 2007, 11:54
You can't identify them by name.
Well, I guess you could, but it is not a good solution.

Solutions:
1. Take the parent of those text edits.
Take the list of it's children.
Loop through them and do a dynamic cast to QLineEdit.
For each successful cast, take the text from the line edit.

2. Use a QSignalMapper.

Regards

raghvendramisra
3rd August 2007, 08:53
Thankx for your help.
But i am very new to QT so i am not that much familiar with the things.
Can you please reply me with some sample code so that i may proceed.

Regards

raghvendra

raghvendramisra
6th August 2007, 04:19
Hey someone plz reply to my problem.
i m realy stuck?????

bremer
6th August 2007, 11:28
Ok, try the following:


QVector<QLineEdit*> le;

for (int i = 0; i < 50; i++) {
le.append(new QLineEdit(this));
le[i]->setText("bla bla bla");
}

now you will have 50 le 's for your loop.

marcel
6th August 2007, 11:34
As I told you, you can loop through the children of the dialog.
But it would be easier that in designer, or in code, whatever, when you create your 50 line edits, do not add them directly to the main window, but to a holder widget.
Next, you can do:


QObjectList clist = holderWidget->children();
QLineEdit *current = NULL;
foreach(QObject* o, clist)
{
if((current = qobject_cast<QLineEdit*>(o))!=NULL)
{
QString txt = current->text();
// do something with txt
}
}



Also, you could loop directly through the children of your main window.

Regards

raghvendramisra
8th August 2007, 09:33
hi,
i tried with le.append(new QLineEdit(this))
but it showed error as "trying to access append which is not a member of QlineEdit class"

So how do i proceed?????????
:(

jpn
8th August 2007, 10:11
i tried with le.append(new QLineEdit(this))
but it showed error as "trying to access append which is not a member of QlineEdit class"

You declared variable le as "QLineEdit*" when it should have been "QVector<QLineEdit*>". For more details, take a closer look at bremer's post.

raghvendramisra
16th August 2007, 07:21
ya i have declared le the same way as bremer wrote but still it is giving error while making as

In member function `virtual void Form2::newSlot()':
.ui/../form2.ui.h:32: error: 'class QPtrVector<QLineEdit*>' has no member named 'append'
.ui/../form2.ui.h:33: error: request for member `setText' in `*(&le)->QPtrVector<type>::operator[] [with type = QLineEdit*](i)', which is of non-class type `QLineEdit*'

So plz tell me how to proceed

jpn
16th August 2007, 09:49
bremer's example is for Qt 4. So if you're using QPtrVector instead, read its docs:


Items are added to the vector using insert() or fill(). Items are removed with remove(). You can get a pointer to an item at a particular index position using at().

marcel
29th August 2007, 20:41
hi,
i have an application having 30 lineEdit boxes named as E1, E2, etc. Right now i am reading the contents of these boxex using
E1->text();
E2->text();

and so on .............

can i do this using a for loop?????????

May I ask what was wrong with my solution and why didn't you at least tried it?
It is easy enough, especially if the widgets are created with designer...

raghvendramisra
30th August 2007, 04:36
sorry marcel...........
but actually as i told you that i am too new to QT.
so i didn't understand your concept of a holder widget
and thats why i didn't tried it.......
can u plz explain it in again so that i may try it..........

Thanks