Results 1 to 3 of 3

Thread: Iterate through set of components

  1. #1
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Iterate through set of components

    Hello. I have following components on my MainWindow ui: groupBox1, label1, button1.

    Problem is that I need to set some properties at the same time to groupBox1, label1, button1 and groupBox2, label2 etc
    Is there a way or iterator (foreach, for) to do this by changing the index number of the components. Thanks.

    Qt Code:
    1. int i = 1;
    2.  
    3. ui->groupBox[i]->setEnabled(true);
    4. ui->label[i]->setText("some text");
    5. ----------------------
    6. current code:
    7. ui->groupBox1->setEnabled(true);
    8. ui->label1->setText("some text");
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 15th June 2011 at 09:42. Reason: missing [code] tags

  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: Iterate through set of components

    Generated code cannot do that for you. You would have to code the ui yourself to have readily available arrays or lists with the widgets. However, there is another approach:
    Qt Code:
    1. foreach (QGroupBox *box, findChildren<QGroupBox*>())
    2. box->setEnabled(true);
    3. foreach (QLabel *label, findChildren<QLabel *>())
    4. label->setText("some text");
    To copy to clipboard, switch view to plain text mode 

    You might need to filter a bit more though.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Iterate through set of components

    If you are using Qt Designer to create the components, then accessing by name is only option.

    If you create in your code, then you can store the list if widgets in a list, vector ( eg.g QList<QLabel*>...) then use them later to change the properties. Remember you only store the pointers of the widgets, you should not delete them, they are owned by the MainWindow, it will take care of deleting them, so don't use this list after MainWindow is done.

Similar Threads

  1. iterate within a a directory
    By kamlmish in forum Newbie
    Replies: 1
    Last Post: 1st February 2011, 04:50
  2. QSortFilterProxy and iterate
    By mif in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2010, 09:10
  3. iterate through QHash
    By rosenth in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2010, 07:55
  4. How to iterate QMap twice
    By sophister in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2009, 16:59
  5. Iterate the QListWidget
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 26th February 2007, 07:51

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.