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 Updated to NodeBB v4.3 + New Features

Creating a matrix of QLabels

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 7 Posters 11.2k Views 3 Watching
  • 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
    b1gsnak3
    wrote on 29 Apr 2013, 06:55 last edited by
    #4

    The easiest way (for me) would be to create a QList of QLabels having the size a multiple of 3 (for example). For display you could use QGridLayout and to go through them row by row you could use something like:

    @
    for(int i = 0; i < labels.size(); i += 3)
    {
    labels.at(i).setText("This is label " + QString::number(i));
    labels.at(i+1).setText("This is label " + QString::number(i+1));
    labels.at(i+2).setText("This is label " + QString::number(i+2));
    }
    @

    1 Reply Last reply
    0
    • H Offline
      H Offline
      holygirl
      wrote on 29 Apr 2013, 08:39 last edited by
      #5

      qxoz

      Hi there! I have used what you suggested in regular C++ with integers with defined bounds for the array. But I want to use QLabel here and I'm trying to figure out how to do it.

      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.

      b1gsnak3

      The number of rows and columns will be defined dynamically. So your technique may not work for me if the size is not a multiple of 3. Thanks!

      1 Reply Last reply
      0
      • P Offline
        P Offline
        podsvirov
        wrote on 29 Apr 2013, 08:59 last edited by
        #6

        I do not understand.
        You need programmatic access in the form of a matrix or a special presentation of data in a graphical interface?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          holygirl
          wrote on 29 Apr 2013, 09:42 last edited by
          #7

          Both! In the GUI, I want the labels to be displayed as:

          @
          label1 label2 label3
          label4 label5 label6
          label7 label8 label9
          @

          and programmatically I wish to access these labels using their respective indices.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 29 Apr 2013, 10:03 last edited by
            #8

            What about QMap<QString, QLabel *> ?
            and access like labelMap["label1"]->setText("label1");

            1 Reply Last reply
            0
            • P Offline
              P Offline
              podsvirov
              wrote on 29 Apr 2013, 11:09 last edited by
              #9

              It may suit:

              @
              #include <QApplication>
              #include <QGridLayout>
              #include <QLabel>

              class QGridLabelMatrix
              {
              public:
              QGridLabelMatrix(const QGridLayout *grid) :
              g(grid)
              {
              Q_ASSERT(grid != 0);
              }

              QLabel* label(int row, int column) const
              {
                  return qobject_cast<QLabel*>(g->itemAtPosition(row, column)->widget());
              }
              

              private:
              const QGridLayout *g;
              };

              int main(int argc, char *argv[])
              {
              int rows = 3, columns = 3;

              QApplication a(argc, argv);
              
              QGridLayout *grid = new QGridLayout();
              
              for(int i = 0; i < rows; i++) {
                  for(int j = 0; j < columns; j++) {
                      QLabel *label = new QLabel(QString("label %1")
                                                 .arg(i * columns + j + 1));
                      grid->addWidget(label, i, j);
                  }
              }
              
              // grid:
              // label1    label2    label3
              // label4    label5    label6
              // label7    label8    label9
              
              QGridLabelMatrix matrix(grid);
              matrix.label(0, 0)->setText("first");
              matrix.label(0, 2)->setText("right");
              matrix.label(2, 2)->setText("last");
              
              // now grid:
              // first     label2    right
              // label4    label5    label6
              // label7    label8    last
              
              QWidget w;
              w.setLayout(grid);
              w.show();
              
              return a.exec&#40;&#41;;
              

              }
              @

              1 Reply Last reply
              0
              • H Offline
                H Offline
                holygirl
                wrote on 30 Apr 2013, 03:41 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
                • P Offline
                  P Offline
                  podsvirov
                  wrote on 30 Apr 2013, 05:02 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 30 Apr 2013, 06:25 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 30 Apr 2013, 06:54 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 30 Apr 2013, 07:39 last edited by
                        #14

                        Thanks for the tip! :)

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Asid
                          wrote on 1 Aug 2017, 11:21 last edited by
                          #15

                          how can i add layouts in this label matrix?

                          VRoninV 1 Reply Last reply 1 Aug 2017, 11:48
                          0
                          • A Asid
                            1 Aug 2017, 11:21

                            how can i add layouts in this label matrix?

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on 1 Aug 2017, 11:48 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 1 Aug 2017, 11:52 last edited by
                              #17

                              thank you very much

                              A 1 Reply Last reply 1 Aug 2017, 12:08
                              0
                              • A Asid
                                1 Aug 2017, 11:52

                                thank you very much

                                A Offline
                                A Offline
                                Asid
                                wrote on 1 Aug 2017, 12:08 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 1 Aug 2017, 12:18 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