Results 1 to 6 of 6

Thread: Is there any way to traverse amount of controls in a loop?

  1. #1
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Is there any way to traverse amount of controls in a loop?

    Hi,
    I have fifteen control (i.e QComboBox) , i want to get text from them. is there any way to traverse the controls from a loop?
    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to traverse amount of controls in a loop?

    It depends how you defined them.
    If you put the pointers in a vector for example - then sure!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any way to traverse amount of controls in a loop?

    Here are the problem because i defined them in the UI.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Is there any way to traverse amount of controls in a loop?

    Then you can still create a vector list with pointers.

    Qt Code:
    1. QVector myVector;
    2. myVector.append(ui->combobox1);
    3. myVector.append(ui->combobox2);
    4.  
    5. for (int i = 0; i < myVector.size(); ++i) {
    6. // Do something with myVector.at(i)
    7. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Is there any way to traverse amount of controls in a loop?

    Or if you want to run through all those items unconditionally:
    Qt Code:
    1. foreach (QComboBox *box, findChildren<QComboBox *>()) {
    2. // do the fancy stuff
    3. }
    To copy to clipboard, switch view to plain text mode 
    Note that QObject::findChlidren() might recurse into other child widgets to find children that match the type T & name.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Is there any way to traverse amount of controls in a loop?

    A correction:
    In my post above, it should be:
    Qt Code:
    1. QVector<QComboBox*> myVector;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 24th August 2008, 14:42
  2. amount of items in QTreeWidget
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2008, 22:38
  3. traverse a xml tree
    By mickey in forum General Programming
    Replies: 2
    Last Post: 27th January 2008, 12:46
  4. Hiding controls changes spacing of other controls
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 18th December 2007, 20:47
  5. Making 3D controls in QT, implementing a game loop in QT
    By smurrish in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2006, 04:37

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.