Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to refer to labels created in a for loop?
QtWS25 Last Chance

How to refer to labels created in a for loop?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlabelloopobject name
6 Posts 4 Posters 966 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BigBen
    wrote on 14 Jun 2022, 03:15 last edited by
    #1

    Hi.

    I am creating multiple instances of labels in a for loop, and displaying them.

    I want to ask, how can I refer to a particular label I create in the loop? Because I don't know their object name. How can I work with a specific label if I want to?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Jun 2022, 04:55 last edited by
      #2

      Store them in a container or let Qt collect them with QObject::findChild()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 1 Reply Last reply 14 Jun 2022, 05:33
      1
      • C Christian Ehrlicher
        14 Jun 2022, 04:55

        Store them in a container or let Qt collect them with QObject::findChild()

        B Offline
        B Offline
        BigBen
        wrote on 14 Jun 2022, 05:33 last edited by
        #3

        @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.

        C J 2 Replies Last reply 14 Jun 2022, 05:36
        0
        • B BigBen
          14 Jun 2022, 05:33

          @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.

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 14 Jun 2022, 05:36 last edited by
          #4

          @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...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 15 Jun 2022, 02:50 last edited by
            #5
            QLabel* label_one = new QLabel("label", parent);
            

            Viola! you have a handle to your label.

            1 Reply Last reply
            0
            • B BigBen
              14 Jun 2022, 05:33

              @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.

              J Offline
              J Offline
              JonB
              wrote on 15 Jun 2022, 05:57 last edited by JonB
              #6

              @BigBen

              1. 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);
              }
              
              1. 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");
              
              1. Collect them all via findChildren():
              QList<QLabel *> labels = someParentWidget->findChildren<QLabel *>();
              
              1 Reply Last reply
              1

              1/6

              14 Jun 2022, 03:15

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved