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. Creating Dynamic GridLayout
Qt 6.11 is out! See what's new in the release blog

Creating Dynamic GridLayout

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.4k 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.
  • F Offline
    F Offline
    fnoyanisi
    wrote on last edited by
    #1

    Hi

    For the code below, I am trying to arrange unknown number of buttons (below it is 34 to try the algorithm) in a grid layout. There will be 4 columns.

    But when I run the code all the buttons are located at the same point. Isn't it supposed to be arranged in grid?

    Thanks in advance...
    [code]int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget *window = new QWidget;
    QGridLayout *grid = new QGridLayout;

    int r_count = (int)34/4;

    for (uint r=0; r < r_count;r++){
    for (uint c=0; c<4; c++)
    grid->addWidget(new QPushButton("label"),r,c,0,0);
    }

    window->setLayout(grid);
    window->show();
    return app.exec();
    }[/code]

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      I have done this code this way. Hope it helps:

      @
      int row=0;
      int numberOfButtons=0;

      while (numberOfButtons<=34)
      {
          for (int f2=0; f2<4; f2++)
          {
              numberOfButtons++;
      
              if (numberOfButtons>34)
                  break;
      
              QPushButton *button=new QPushButton();
              button->setText(QString::number(numberOfButtons));
              glayout->addWidget(button, row, f2);
          }
          row++;
      }
      

      @

      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