Arrange multiple labels in grid layout created in loop.
-
wrote on 14 Jun 2022, 03:31 last edited by
I am creating multiple labels in a loop and adding them to a grid layout as follows:
for(int i=0; i<Count; i++){ QLabel *label = new QLabel(this); label->setStyleSheet("border: 1px solid white"); grid->addWidget(label); }
However, all of the labels I add, get added in a vertical manner. I want to arrange them in a n x 3 grid. Where n is the number of rows and 3 is the number of columns. is there a way to do this?
-
I am creating multiple labels in a loop and adding them to a grid layout as follows:
for(int i=0; i<Count; i++){ QLabel *label = new QLabel(this); label->setStyleSheet("border: 1px solid white"); grid->addWidget(label); }
However, all of the labels I add, get added in a vertical manner. I want to arrange them in a n x 3 grid. Where n is the number of rows and 3 is the number of columns. is there a way to do this?
@BigBen said in Arrange multiple labels in grid layout created in loop.:
all of the labels I add, get added in a vertical manner. I want to arrange them in a n x 3 grid.
Specify the row and column numbers in QGridLayout::addWidget(): https://doc.qt.io/qt-6/qgridlayout.html#addWidget-1
-
@JKSH
But I add the labels to the layout inside the for loop. How will I change the row and column number at every iteration?Lifetime Qt Championwrote on 14 Jun 2022, 04:56 last edited by Christian Ehrlicher@BigBen said in Arrange multiple labels in grid layout created in loop.:
How will I change the row and column number at every iteration?
Maybe add 1 to row in each iteration and after your row is full, add 1 to column?
-
wrote on 14 Jun 2022, 05:16 last edited by
Yes. That was simple. I don't know why I did not think of that. Thanks guys.
1/5