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 Updated to NodeBB v4.3 + New Features

Arrange multiple labels in grid layout created in loop.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 594 Views 1 Watching
  • 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 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?

    JKSHJ 1 Reply Last reply
    0
    • B BigBen

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on 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 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?

        Christian EhrlicherC 1 Reply Last reply
        0
        • B BigBen

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

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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 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

            • Login

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