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. Arrange multiple labels in grid layout created in loop.
Forum Update on Monday, May 27th 2025

Arrange multiple labels in grid layout created in loop.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 575 Views
  • 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
    BigBen
    wrote on 14 Jun 2022, 03:31 last edited by
    #1

    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?

    J 1 Reply Last reply 14 Jun 2022, 04:16
    0
    • B BigBen
      14 Jun 2022, 03:31

      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?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 14 Jun 2022, 04:16 last edited by
      #2

      @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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BigBen
        wrote on 14 Jun 2022, 04:52 last edited by
        #3

        @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?

        C 1 Reply Last reply 14 Jun 2022, 04:56
        0
        • B BigBen
          14 Jun 2022, 04:52

          @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?

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 14 Jun 2022, 04:56 last edited by Christian Ehrlicher
          #4

          @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?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • B Offline
            B Offline
            BigBen
            wrote on 14 Jun 2022, 05:16 last edited by
            #5

            Yes. That was simple. I don't know why I did not think of that. Thanks guys.

            1 Reply Last reply
            0

            1/5

            14 Jun 2022, 03:31

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved