PDA

View Full Version : How to create labels dynamically according to the slider value



jakr13
5th December 2014, 05:25
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

anda_skoa
5th December 2014, 07:06
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,
_

jakr13
5th December 2014, 08:05
@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

wysota
5th December 2014, 12:48
So what exactly is the problem? You don't know how to create a label?


QLabel *label = new QLabel;

jakr13
5th December 2014, 13:01
@wysota I am familiar with creating labels, but I dont know how to create dynamic labels.

wysota
5th December 2014, 13:06
What are "dynamic labels"?

jakr13
5th December 2014, 13:09
10783 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;

wysota
5th December 2014, 13:34
To create labels at runtime use the "new" operator like I showed in post #4.

jakr13
6th December 2014, 09:10
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