Creating a matrix of QLabels
-
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();
}
@ -
[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 :)
-
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! :)
-
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(); }