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. Creating a matrix of QLabels
Forum Update on Monday, May 27th 2025

Creating a matrix of QLabels

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 11.2k 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #10

    [quote author="holygirl" date="1367224774"]
    Konstantin

    Thank you so much for your reply. I've tried using QTableWidget and QTableView before but I want to use QLabel only because the width of each cell varies in the grid.
    [/quote]

    Hi again!

     As I mentioned above, each cell varies in its width. So I cannot use a _GridLayout_ either. Let me give you a pictorial idea of how my labels will look after populating it with data.
    

    @

    | Label1 | Label2 | Label3 | Label4 |

    | Label5 | Label6 | Label7 | Label8 |

    | Label9 | Label10 | Label11 | Label12 |

    @

    So I not only have to generate these labels on the GUI, but also access them using indices so I can manipulate their properties like

    @
    label [0][2]->setText("Changed label");
    @

    Thanks for your replies guys!

    P.S. - qxoz, I haven't looked into what you suggested. I will keep you posted on whether it works for me. Thanks very much :)

    1 Reply Last reply
    0
    • podsvirovP Offline
      podsvirovP Offline
      podsvirov
      wrote on last edited by
      #11

      Hi again!

      This will be the matrix? The number of elements in each row should be the same?
      Or depending on the width of the labels their number may vary?

      For example:

      @

      | Label 1 | Long label 2 | Very loooooooooooooooong label 3 |

      | Label 4 | Label 5 | Label 6 | Label 7 |

      | BIG LABEL 8 |

      @

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #12

        Yes! The number may vary. Exactly like how you've depicted it. I found a solution after days and days of googling and reading books. Ha ha! Here it is:

        @
        QVector<QVector<QLabel *> > vectorOfVectorsOfLabels;
        @

        @
        for(int i=0;i<num_of_rows;i++) {
        QVector<QLabel > foo; //QVector of QLabels
        for(int i=0;i<row.length();i++) {
        QLabel
        label = new QLabel(this);
        //Do whatever with label
        foo.append(label);
        }
        vectorOfVectorsOfLabels.append(foo);
        }
        @

        and the labels can be accessed as:

        @
        for(int i = 0; i < vectorOfVectorsOfLabels.size(); i++) {
        for(int j = 0; j < vectorOfVectorsOfLabels [i].size(); j++) {
        vectorOfVectorsOfLabels [0][1]->setText("Changed label");
        // or do anything with the QLabel vectorOfVectorsOfLabels [i][j]
        }
        }
        @

        Anyway, I thank all of you for your constant support and help! I'm marking the thread solved. Thanks people! :)

        1 Reply Last reply
        0
        • B Offline
          B Offline
          b1gsnak3
          wrote on last edited by
          #13

          QList<QList<QLabel *>> :) if the size varies... or store the number of labels / row when you add them "dynamically"

          1 Reply Last reply
          0
          • H Offline
            H Offline
            holygirl
            wrote on last edited by
            #14

            Thanks for the tip! :)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asid
              wrote on last edited by
              #15

              how can i add layouts in this label matrix?

              VRoninV 1 Reply Last reply
              0
              • A Asid

                how can i add layouts in this label matrix?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #16

                QGridLayout has an addLayout() method for that

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Asid
                  wrote on last edited by
                  #17

                  thank you very much

                  A 1 Reply Last reply
                  0
                  • A Asid

                    thank you very much

                    A Offline
                    A Offline
                    Asid
                    wrote on last edited by
                    #18

                    @Asid i am trying but is not get a resoult

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #19

                      Hi! Here's a minimal example:

                      #include <QApplication>
                      #include <QWidget>
                      #include <QGridLayout>
                      #include <QVBoxLayout>
                      #include <QLabel>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          QWidget mainWindow;
                          auto gridLayout = new QGridLayout(&mainWindow);
                          for (int row = 0; row < 3; ++row) {
                              for (int column = 0; column < 3; ++column) {
                                  if (row == 1 && column == 1) {
                                      auto innerLayout = new QVBoxLayout;
                                      gridLayout->addLayout(innerLayout, 1, 1);
                                      for (int i = 0; i < 3; ++i) {
                                          auto label = new QLabel(&mainWindow);
                                          innerLayout->addWidget(label);
                                          label->setText(QString::number(i));
                                      }
                                  } else {
                                      auto label = new QLabel(&mainWindow);
                                      gridLayout->addWidget(label, row, column);
                                      label->setText( QString("%1,%2").arg(row).arg(column) );
                                  }
                              }
                          }
                          mainWindow.show();
                          return a.exec();
                      }
                      
                      1 Reply Last reply
                      2

                      • Login

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