You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, November 11th 2010, 7:07pm

dynamiclly updating lables

hey guys,
im trying to implement a GUI that scales based on a config file

Multiple labels defined on the gui, ie label_1, label_2, label_3

for( i = 0; i < parameternumber; i++)
{
ui,->label_(THIS NEEDS TO UPDATE BASED ON THE LOOP)->setText();
}

essentially it reads input in from a file, and updates the labels with that input accordingly in order
Thanks for any input

slevon

Trainee

Posts: 89

Location: Germany

Occupation: electrical engineer

  • Send private message

2

Thursday, November 11th 2010, 7:28pm

Hi,
you cant go to all the labels bx a loop, if thex are all defined seperatly
you have two options:
1) defining your labels yourself and the add them to your layout

Source code

1
2
3
4
5
6
7
8
9
10
QString myNewData("filled");
QList<QLabel*> labels;
labels.append(new QLabel("Empty1"));  // your first label
 labels.append(new QLabel("Empty2"));  // your second label
 //add this QLabel pointers to your layout

for (int i=0;i<labels.size();i++)
{
labels[i]->setText(myNewData);  //this set all your labels to the content of the String 
}


2) if you want to use the labels you have added in your designer xour could do something like:

Source code

1
2
3
4
5
6
7
8
9
10
QString myNewData("filled");
QList<QLabel*> labels;
labels.append(ui->label_1); // your first label
labels.append(ui->label_2); // your second label
//add this QLabel pointers to your layout

for (int i=0;i<labels.size();i++)
{
labels[i]->setText(myNewData); //this set all your labels to the content of the String 
}


I would prefer option one, because you can better scale it up if you need more labels in the future.

Do you know how to read Texts from files?
-If not have a look at QTextstream

Hope this helps,
slevon