Results 1 to 4 of 4

Thread: Can I Choose the label on runtime?

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can I Choose the label on runtime?

    Should I choose the label or other ui object before runtime? Can I choose them on runtime?

    What I mean is :

    Should I write:
    Qt Code:
    1. for (int i = 0; i < 5 ; i++)
    2. {
    3. ui->label_0->setText(QString::number(i));
    4. ui->label_1->setText(QString::number(i));
    5. ui->label_2->setText(QString::number(i));
    6. ui->label_3->setText(QString::number(i));
    7. ui->label_4->setText(QString::number(i));
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    But I want to write as:

    Qt Code:
    1. for (int i = 0; i < 5 ; i++)
    2. {
    3. ui->label_(i)->setText(QString::number(i));
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Is it possible to write in this way?

  2. #2
    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: Can I Choose the label on runtime?

    You can't do it like you wrote it.

    But, there are a couple of techniques that do let you do this.

    1. Use the name of the object.
    2. Create a list of objects.

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

    akrep55tr (13th February 2011)

  4. #3
    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: Can I Choose the label on runtime?

    EDIT:
    oops, beat to it by tbscope.

    No this is not possible the way you wrote it, as you will find out if you try to compile it.
    What you can do is put all your labels in a container:
    Qt Code:
    1. QVector<QLabel*> vecLabels;
    2. vecLabels.push_back(ui->label_0);
    3. vecLabels.push_back(ui->label_1);
    4. ...
    5. for (int i = 0; i < 5 ; i++)
    6. {
    7. vecLabels.at(i)->setText(QString::number(i));
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

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

    akrep55tr (13th February 2011)

  6. #4
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can I Choose the label on runtime?

    @tbscope , @high_flyer
    Thank you for your replies.

    @high_flyer
    Thank you for your example, it helped a lot.

Similar Threads

  1. get property from qss in runtime
    By steep8 in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2011, 23:29
  2. Replies: 1
    Last Post: 25th September 2010, 08:20
  3. NCReport2 and VC++ Runtime
    By wirasto in forum Newbie
    Replies: 0
    Last Post: 27th October 2009, 23:42
  4. IDE to choose
    By sepehr in forum Qt Programming
    Replies: 8
    Last Post: 11th September 2008, 18:31
  5. runtime libraries?
    By fwohlfert in forum Newbie
    Replies: 1
    Last Post: 5th January 2006, 18:12

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.