質問

For example, there are 4 Qlabels named label_1, label_2, label_3 and label_4. Each has a different value and I want to access the value contained in each of them one by one using any loop.

役に立ちましたか?

解決

You should put those labels in a container like a QVector<QLabel*> instead of giving each of them a name, then access via a loop isn't a problem at all - just look them up in the vector by index.

If for some reason you have to declare those four labels independently, there's no problem with having a list of them on top of that as another class member.

In your class:

 QVector<QLabel*> labels;

In your constructor:

 // build the four labels
 labels.append(label_1);
 labels.append(label_2);
 ...

Iterate over them:

 foreach (QLabel *l, labels) {
   // whatever with l
 }
 for (unsigned i = 0; i < labels.size(); i++) {
   // whatever with labels[i]
 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top