Results 1 to 5 of 5

Thread: Object Names

  1. #1
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Object Names

    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:

    Qt Code:
    1. for (int c = 0; c < 100; c++) {
    2.  
    3. lineEdit_c->setText("Hello!");
    4.  
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Object Names

    Make a QList<QLineEdit *>. Then step through the line edits:

    Qt Code:
    1. QList<QLineEdit *> lineEditors;
    2.  
    3. ...
    4. // you can do this:
    5. foreach (QLineEdit *editor, lineEditors) {
    6. editor->doSomething();
    7. }
    8.  
    9. // or this:
    10. for (int i = 0; i < lineEditors.count(); i++) {
    11. lineEditors[i]->doSomething();
    12. }
    13.  
    14. // or use the java-style QMutableListIterator or the standard c++ QList::iterator
    To copy to clipboard, switch view to plain text mode 

    Anyway, this is a rather basic thing incorporated in several languages. Do some reading up on C++ (www.cplusplus.com).
    Last edited by franz; 15th July 2010 at 20:28. Reason: updated contents
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. The following user says thank you to franz for this useful post:

    slava (16th July 2010)

  4. #3
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: Object Names

    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.

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Object Names

    You can also use QObject::children, iterate over them, type cast them into QLineEdit, and if valid set the text to "hello"

  6. #5
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: Object Names

    O, that's goot idea too, thank you.

Similar Threads

  1. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 14:28
  2. How to know the object names of any generic application ?
    By soumyadeep_pan in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2009, 07:11
  3. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 06:33
  4. object files' names collision
    By PowerKiKi in forum Qt Programming
    Replies: 7
    Last Post: 30th October 2007, 21:32
  5. moc with same file names?
    By cjhuitt in forum Qt Programming
    Replies: 9
    Last Post: 27th April 2007, 20:36

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.