How to refer to labels created in a for loop?
Unsolved
General and Desktop
-
-
Store them in a container or let Qt collect them with QObject::findChild()
-
@Christian-Ehrlicher
Thanks for your answer. I wanted to request an example of storing the labels in a container or collecting them in findChild().Thank you.
-
@BigBen said in How to refer to labels created in a for loop?:
I wanted to request an example of storing the labels in a container or collecting them in findChild().
Why? This is basic c++ stuff which you should know. for findChild() there is even an example in the link I gave you...
-
QLabel* label_one = new QLabel("label", parent);
Viola! you have a handle to your label.
-
- Store them in a container yourself as you create them:
QList<QLabel *> labels; for (int i = 0; i < 10; i++) { QLabel *label = new QLabel; someLayout->addWidget(label); labels.append(label); }
- Assign them an
objectName
for future reference to recall an individual one:
QList<QLabel *> labels; for (int i = 0; i < 10; i++) { QLabel *label = new QLabel; label->setObjectName(QString("label_%1").arg(i)); someLayout->addWidget(label); } QLabel *label_2 = someParentWidget->findChild<QLabel *>("label_2");
- Collect them all via
findChildren()
:
QList<QLabel *> labels = someParentWidget->findChildren<QLabel *>();