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. loop over QLabel Attributes?
QtWS25 Last Chance

loop over QLabel Attributes?

Scheduled Pinned Locked Moved Solved General and Desktop
loopforattributecenter
6 Posts 4 Posters 2.1k 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.
  • P Offline
    P Offline
    pauledd
    wrote on 30 Oct 2019, 11:40 last edited by
    #1

    Hi

    I need to have a row of checkboxes (23) with numbering QLabels above.
    I created a QGridLayout and put all the QLabel's in the first row and all
    QCheckBox'es in the second row.
    Bildschirmfoto_2019-10-30_12-29-29.png
    As you can see the numbers above are not perfectly centered over the individual checkboxes. I found that I can center each label by setting the

    QLabel::setAlignment(Qt::AlignCenter)
    

    for each label, but this is a LOAD of code for 23 labels:

    label0->setAlignment(Qt::AlignCenter);
    label1->setAlignment(Qt::AlignCenter);
    label2->setAlignment(Qt::AlignCenter);
    ...
    label23->setAlignment(Qt::AlignCenter);
    

    Is it somehow possible to loop over the labels and set the alignment
    with a simple for loop or so?

    J J 2 Replies Last reply 30 Oct 2019, 11:43
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Oct 2019, 11:43 last edited by
      #2

      If you want to get all QLabel in one form you can use QWidget::findChildren()

      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
      5
      • P pauledd
        30 Oct 2019, 11:40

        Hi

        I need to have a row of checkboxes (23) with numbering QLabels above.
        I created a QGridLayout and put all the QLabel's in the first row and all
        QCheckBox'es in the second row.
        Bildschirmfoto_2019-10-30_12-29-29.png
        As you can see the numbers above are not perfectly centered over the individual checkboxes. I found that I can center each label by setting the

        QLabel::setAlignment(Qt::AlignCenter)
        

        for each label, but this is a LOAD of code for 23 labels:

        label0->setAlignment(Qt::AlignCenter);
        label1->setAlignment(Qt::AlignCenter);
        label2->setAlignment(Qt::AlignCenter);
        ...
        label23->setAlignment(Qt::AlignCenter);
        

        Is it somehow possible to loop over the labels and set the alignment
        with a simple for loop or so?

        J Offline
        J Offline
        JonB
        wrote on 30 Oct 2019, 11:43 last edited by JonB
        #3

        @pauledd
        Put your labels into an array/vector, and iterate through that to access them. This will be useful for all sorts of operations. By the time you have that many items it's "usual" to access them by array instead of having each one have its own variable, though you can still keep individual variables if you wish, just copy them into an array/vector as well for when you want to loop over them.

        P.S.
        You can also do @Christian-Ehrlicher's way for dynamic finding, and/or also use QObject::setObjectName() on each one. I still suspect you ought have an array/vector of them anyway.

        1 Reply Last reply
        5
        • P pauledd
          30 Oct 2019, 11:40

          Hi

          I need to have a row of checkboxes (23) with numbering QLabels above.
          I created a QGridLayout and put all the QLabel's in the first row and all
          QCheckBox'es in the second row.
          Bildschirmfoto_2019-10-30_12-29-29.png
          As you can see the numbers above are not perfectly centered over the individual checkboxes. I found that I can center each label by setting the

          QLabel::setAlignment(Qt::AlignCenter)
          

          for each label, but this is a LOAD of code for 23 labels:

          label0->setAlignment(Qt::AlignCenter);
          label1->setAlignment(Qt::AlignCenter);
          label2->setAlignment(Qt::AlignCenter);
          ...
          label23->setAlignment(Qt::AlignCenter);
          

          Is it somehow possible to loop over the labels and set the alignment
          with a simple for loop or so?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 30 Oct 2019, 11:48 last edited by J.Hilk
          #4

          @pauledd if this is the only thing you want to do with it, I would recommend doing the legwork and writing it all down.

          It should enable the compiler to do more optimizations.

          However you can use https://doc.qt.io/qt-5/qobject.html#findChildren

          to loop over all children of a widget, and then use qobject_cast<QLabel*>() to check if the child is a QLabel or not.

          Make sure to select the correct "parent widget" or you may end up finding other QLabels where you do not want to change the property.

          Also findChildren is a rather costly execution, its probably fine during startup, but I would recommend not to use it during runtime of your program.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply 30 Oct 2019, 11:54
          0
          • J J.Hilk
            30 Oct 2019, 11:48

            @pauledd if this is the only thing you want to do with it, I would recommend doing the legwork and writing it all down.

            It should enable the compiler to do more optimizations.

            However you can use https://doc.qt.io/qt-5/qobject.html#findChildren

            to loop over all children of a widget, and then use qobject_cast<QLabel*>() to check if the child is a QLabel or not.

            Make sure to select the correct "parent widget" or you may end up finding other QLabels where you do not want to change the property.

            Also findChildren is a rather costly execution, its probably fine during startup, but I would recommend not to use it during runtime of your program.

            J Offline
            J Offline
            JonB
            wrote on 30 Oct 2019, 11:54 last edited by
            #5

            @J-Hilk said in loop over QLabel Attributes?:

            Also findChildren is a rather costly execution, its probably fine during startup, but I would recommend not to use it during runtime of your program.

            @pauledd
            ...which is why even if you use that you probably want to end up putting them into an array...

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pauledd
              wrote on 30 Oct 2019, 12:51 last edited by pauledd
              #6

              Thanks a lot for your suggestions. I ended up doing it with the vectors because it was most elegant for my brain...

              *.h

              QVector<QLabel*> labels;
              

              *.cpp

              for(int i=0;i<24;i++){
                  labels.append(new QLabel(QString::number(23-i)));
              }
              for(int i=0;i<labels.size();i++){
                 labels[i].setAlignment(Qt::AlignCenter);
              }
              

              and then the same for the checkboxes. I guess the vector saved about 100 lines of code.

              Works like a charm :)

              Bildschirmfoto_2019-10-30_13-42-22.png

              1 Reply Last reply
              2

              5/6

              30 Oct 2019, 11:54

              • Login

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