PDA

View Full Version : Dynamic QLabels



rajeshs
30th May 2008, 09:23
Hi All,

I am having one QFrame in QDialog, I need to create dynamic labels based on some input given by user.the number of label is based on user input.

[ for Ex if user gives 6 as input i need 6 labels and then next if he gives 4 second time i need only 4 ,so i need to remove 2 labels now]

How to achieve this ?

lyuts
30th May 2008, 10:22
Hi All,

I am having one QFrame in QDialog, I need to create dynamic labels based on some input given by user.the number of label is based on user input.

[ for Ex if user gives 6 as input i need 6 labels and then next if he gives 4 second time i need only 4 ,so i need to remove 2 labels now]

How to achieve this ?

What if you do something like this:


for (int i = 0; i < numberOfLabelsSpecified; i++) {
...
yourLayout->addWidget(new QLabel(........), row, column);
...
}

rajeshs
30th May 2008, 12:10
Thank you for you reply . i have achieved dynamic adding , how to remove dynamically?

rajeshs
30th May 2008, 12:26
Now i got Solution ,

I used the folowing code ,

Every time when user gives some input and clicks some button, in button click,



for(int i=0;i<listLabel->count();i++)
{
delete listLabel->at(i);
}

listLabel->clear();
for(int i = 0;i<lineEdit->text().toInt();i++)
{
QLabel* l = new QLabel("Hello"+QString::number(i));
layout->addWidget(l,0,i);
listLabel->append(l);
}

Here listLabel is the QList<QLabel*> ,layout is QGridLayout applied to QFrame. I have the following code in Constructor



setupUi(this);

layout = new QGridLayout(frame);

listLabel = new QList<QLabel*>();

connect(pushButton,SIGNAL(pressed()),this,SLOT(add ()));


Thank you for ur Guidence.