Results 1 to 12 of 12

Thread: HOw to read Line edit through a for loop

  1. #1
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Unhappy HOw to read Line edit through a for loop

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HOw to read Line edit through a for loop

    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

  3. #3
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: HOw to read Line edit through a for loop

    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

  4. #4
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: HOw to read Line edit through a for loop

    Hey someone plz reply to my problem.
    i m realy stuck?????

  5. #5
    Join Date
    Jul 2006
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HOw to read Line edit through a for loop

    Ok, try the following:

    Qt Code:
    1. QVector<QLineEdit*> le;
    2.  
    3. for (int i = 0; i < 50; i++) {
    4. le.append(new QLineEdit(this));
    5. le[i]->setText("bla bla bla");
    6. }
    To copy to clipboard, switch view to plain text mode 
    now you will have 50 le 's for your loop.
    Last edited by bremer; 6th August 2007 at 10:36. Reason: sorry, first time for me to reply

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HOw to read Line edit through a for loop

    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:
    Qt Code:
    1. QObjectList clist = holderWidget->children();
    2. QLineEdit *current = NULL;
    3. foreach(QObject* o, clist)
    4. {
    5. if((current = qobject_cast<QLineEdit*>(o))!=NULL)
    6. {
    7. QString txt = current->text();
    8. // do something with txt
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 


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

    Regards

  7. #7
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: HOw to read Line edit through a for loop

    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?????????

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: HOw to read Line edit through a for loop

    Quote Originally Posted by raghvendramisra View Post
    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.
    J-P Nurmi

  9. #9
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: HOw to read Line edit through a for loop

    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>:perator[] [with type = QLineEdit*](i)', which is of non-class type `QLineEdit*'

    So plz tell me how to proceed

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: HOw to read Line edit through a for loop

    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().
    J-P Nurmi

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HOw to read Line edit through a for loop

    Quote Originally Posted by raghvendramisra View Post
    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...

  12. #12
    Join Date
    Feb 2007
    Posts
    63
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: HOw to read Line edit through a for loop

    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

Similar Threads

  1. Slot to color background of line edit on textedited
    By tpf80 in forum Qt Programming
    Replies: 5
    Last Post: 21st June 2007, 09:02
  2. Replies: 8
    Last Post: 15th May 2007, 09:21
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.