Results 1 to 9 of 9

Thread: How to create labels dynamically according to the slider value

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question How to create labels dynamically according to the slider value

    Hello everyone,

    How can I create QLabel dynamically depending on the slider input? eg. if slider value is 3, then I need to create 3 labels. any suggestions or examples will be really appreciated.

    Thanks in advance!!!

    jakr13
    Last edited by jakr13; 5th December 2014 at 07:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create labels dynamically according to the slider value

    The control structure for that in C++ is called a loop, in this case a good candidate would be a for loop.

    Any C++ tutorial on the net will likely have a section on loops.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create labels dynamically according to the slider value

    @anda_skoa I didn't mean about loops

    int no_of labels = ui->slider->value();

    for(int i=1; i<=no_of_labels; i++)
    {
    // what I have to do here??
    }


    Suppose if no_of_labels is 5, i have to create 5 labels dynamically label1, label2, label3, label4 & label5 and display images in each label. how to do this?

    I have created 8 labels statically and displaying the images according to the no_of_labels. Suppose if the no_of_label is 5, 5 labels are shown with corresponding images, the remaining three labels are left free. I dont want that. so I decided to create labels dynamically depending on the slider value.

    Jakr13
    Last edited by jakr13; 5th December 2014 at 09:22.

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

    Default Re: How to create labels dynamically according to the slider value

    So what exactly is the problem? You don't know how to create a label?

    Qt Code:
    1. QLabel *label = new QLabel;
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #5
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create labels dynamically according to the slider value

    @wysota I am familiar with creating labels, but I dont know how to create dynamic labels.

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

    Default Re: How to create labels dynamically according to the slider value

    What are "dynamic labels"?
    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.


  7. #7
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create labels dynamically according to the slider value

    gui.jpg I have static labels as show in attached image. I dont want to have static labels and I am trying to create labels at runtime. The no of labels depends on the slider value;
    Last edited by jakr13; 5th December 2014 at 14:24.

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

    Default Re: How to create labels dynamically according to the slider value

    To create labels at runtime use the "new" operator like I showed in post #4.
    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:

    jakr13 (6th December 2014)

  10. #9
    Join Date
    Jan 2013
    Location
    Bangalore, India
    Posts
    36
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create labels dynamically according to the slider value

    I was thinking of so much that I have to do to create labels at run time. I was so stupid, because it was so simple. Got it worked, thanks @Wysota and @anda_skoa

Similar Threads

  1. Dynamically change slider max range
    By Don_Hannes in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 17:19
  2. Replies: 2
    Last Post: 21st March 2010, 10:01
  3. how to dynamically add labels
    By mohanakrishnan in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2009, 11:19
  4. Tick Labels for Slider
    By jcraig in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 18:21

Tags for this Thread

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.