Results 1 to 7 of 7

Thread: for loop application

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question for loop application

    Hi everyone,

    I have a question about the for() loop function in Qt.
    in my program, i have 10 labels, which indicate 10 led lights.
    I can change the color for each label by using:
    Qt Code:
    1. ui->label_led_1->setPixmap(picLedRed);
    2. ui->label_led_2->setPixmap(picLedRed);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    I wonder how can i use the For() loop to simplify the code.
    ex:
    Qt Code:
    1. for (int i = 1; i <=10; i ++)
    2. ui->label_led_i->setPixmap(picLedRed);
    To copy to clipboard, switch view to plain text mode 

    Can any one tell me how i can increment the label number i, please?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: for loop application

    Try this:

    Qt Code:
    1. for (int i = 1; i <= 10; i++)
    2. {
    3. QLabel *label = qFindChild<QLabel *>(this, "label_led_" + QString::number(i));
    4. if (label) { label->setPixmap(picLedRed); }
    5. }
    To copy to clipboard, switch view to plain text mode 

    Edit: (or replace 'this' with a pointer to the MainWindow if the loop is in another class)
    Last edited by helloworld; 1st February 2011 at 01:43.

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

    cooper (1st February 2011)

  4. #3
    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: for loop application

    Or put your labels in a list.

    example:
    Qt Code:
    1. QList<QLabel*> myLedLabels;
    2. myLedLabel->append(ui->label_led_1);
    3. // do append for all your led labels
    4.  
    5. for (int i = 0; i < 10; ++i)
    6. {
    7. myLedLabels.at(i).setPixmap(picLedRed);
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tbscope for this useful post:

    cooper (1st February 2011)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: for loop application

    I would probably also take the way with qFindChild. But to avoid to call qFindChild ten times, one could use
    Qt Code:
    1. QList<QLabel*> labels = qFindChildren<QLabel*>(this, QRegExp("^label_led_[0-9]+$"));
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lykurg for this useful post:

    cooper (1st February 2011)

  8. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: for loop application

    If I have 10 labels added with designer then I would construct a function or a macro and copy&paste the code ten times instead of creating artificial lists. Unless of course you intend to reuse the list multiple times for other purposes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    cooper (1st February 2011)

  10. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: for loop application

    Another way (if you have numbered your labels starting from 0):

    Qt Code:
    1. #include <boost/preprocessor.hpp>
    2.  
    3. #define TO_REPEAT(z,count,action) _ui->label_led_ ## count -> action ;
    4.  
    5. // and use it like:
    6.  
    7. QPixmap pixmap(":/pic.jpg");
    8. BOOST_PP_REPEAT(10, TO_REPEAT, setPixmap(pixmap) );
    9.  
    10. // expands to :
    11. _ui->label_led_0->setPixmap(pixmap);
    12. _ui->label_led_1->setPixmap(pixmap);
    13. // ...
    14. _ui->label_led_9->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to stampede for this useful post:

    cooper (1st February 2011)

  12. #7
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: for loop application

    Thanks for you all. I really appreciate you guys' help. I will give it a try

Similar Threads

  1. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  2. Application windows lags while loop doing.
    By Wojtek1990 in forum Newbie
    Replies: 8
    Last Post: 23rd July 2010, 20:41
  3. Application won't quit until event loop quits
    By caelestis in forum Qt Programming
    Replies: 6
    Last Post: 11th February 2010, 07:21
  4. Qt plug-in for GLib event loop based application
    By profoX in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2008, 14:27
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

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.