Creating a matrix of QLabels



  • Hi all!

        Is it possible to create a 2D matrix of QLabels like
    

    @
    label1 label2 label3
    label4 label5 label6
    label7 label8 label9
    @

    I also want to set the attribute of each label as

    @
    label1.setText("This is label 1");
    label2.setText("This is label 2");
    @

    I tried declaring the array as

    @
    QVector<QVector<QLabel *> labels;
    @

    but I'm not too sure how to access it. I read "this":http://www.qtcentre.org/threads/49026-2D-array-in-Qt-QVector-or-QList and "this":http://stackoverflow.com/questions/12776872/how-to-use-qvector-as-two-dimensional-array and so many other links but I'm not able to understand how to get started.
    Could somebody please give me a nudge in the right direction? Thank you.



  • Hi!
    Array items can be accessed as:
    @labels[x][y] - >setText("This is label");@
    but i'm sure, you already know it.
    Probably i'm not clearly understand your issue, can you explain more?
    And maybe QTableView (with some qss) more prefered in your case?



  • You can also use the class "QTableWidget":http://qt-project.org/doc/QTableWidget.html for storage and display
    text data:

    @
    #include <QApplication>
    #include <QTableWidget>

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

    QApplication a(argc, argv);
    
    QTableWidget table(rows, columns);
    
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < columns; j++)
            table.setItem(i, j, new QTableWidgetItem(
                              QString("label %1")
                              .arg(i * columns + j + 1)));
    }
    
    // table:
    // label1    label2    label3
    // label4    label5    label6
    // label7    label8    label9
    
    table.item(0,0)->setText("first");
    table.item(0,2)->setText("right");
    table.item(2,2)->setText("last");
    
    // now table:
    // first     label2    right
    // label4    label5    label6
    // label7    label8    last
    
    table.show();
    
    return a.exec&#40;&#41;;
    

    }
    @



  • 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));
    }
    @



  • 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!



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



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



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



  • 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;;
    

    }
    @



  • [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 :)



  • 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

    @



  • 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! :)



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



  • Thanks for the tip! :)



  • how can i add layouts in this label matrix?



  • QGridLayout has an addLayout() method for that



  • thank you very much



  • @Asid i am trying but is not get a resoult


  • Moderators

    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();
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.