Creating a matrix of QLabels
-
wrote on 29 Apr 2013, 06:55 last edited by
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));
}
@ -
wrote on 29 Apr 2013, 08:39 last edited by
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!
-
wrote on 29 Apr 2013, 08:59 last edited by
I do not understand.
You need programmatic access in the form of a matrix or a special presentation of data in a graphical interface? -
wrote on 29 Apr 2013, 09:42 last edited by
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.
-
wrote on 29 Apr 2013, 10:03 last edited by
What about QMap<QString, QLabel *> ?
and access like labelMap["label1"]->setText("label1"); -
wrote on 29 Apr 2013, 11:09 last edited by
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();
}
@ -
wrote on 30 Apr 2013, 03:41 last edited by
[quote author="holygirl" date="1367224774"]
KonstantinThank 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 :)
-
wrote on 30 Apr 2013, 05:02 last edited by
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 |
@
-
wrote on 30 Apr 2013, 06:25 last edited by
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! :)
-
wrote on 30 Apr 2013, 06:54 last edited by
QList<QList<QLabel *>> :) if the size varies... or store the number of labels / row when you add them "dynamically"
-
wrote on 30 Apr 2013, 07:39 last edited by
Thanks for the tip! :)
-
wrote on 1 Aug 2017, 11:21 last edited by
how can i add layouts in this label matrix?
-
wrote on 1 Aug 2017, 11:48 last edited by
QGridLayout
has anaddLayout()
method for that -
wrote on 1 Aug 2017, 11:52 last edited by
thank you very much
-
wrote on 1 Aug 2017, 12:18 last edited by
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(); }