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. adusting the spacing in qgridlayout
Forum Updated to NodeBB v4.3 + New Features

adusting the spacing in qgridlayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 8.0k Views 2 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.
  • A Offline
    A Offline
    amruz
    wrote on last edited by
    #1

    i am having a program where i am creating many buttons in a gridlayout as shown below.
    but i am getting many spaces between the widgets. i tried setting the spacing between them.. but no luck can anyone guide me in this

    for(i=1;i<6;i++)

    {
    
        for(j=0;j<6;j++)
    
        {
    
            if(count<=26)
    
            {
    
                QString str= QChar(s);
    
                //QString myString = "a";
    
                button[i][j] = new QPushButton(str);
    
                s=s+1;
    
                button[i][j]->setText(str);
    
                button[i][j]->setFixedWidth(60);
    
                controlsLayout->addWidget(button[i][j], i, j,0);
    
                controlsLayout->setHorizontalSpacing(0);
    
                controlsLayout->setVerticalSpacing(0);
    
     //           controlsLayout->setContentsMargins(  0,  0,  0,  0 );
    
                controlsLayout->setSpacing(0);
    
                connect(button[i][j], SIGNAL(clicked()), this ,SLOT(buttonWasClicked()));
    
                count++;
    
            }
    
        }
    
    }
    
    controlsLayout->setHorizontalSpacing(0);
    
    controlsLayout->setVerticalSpacing(0);
    
    controlsLayout->setSpacing(0);
    
    
    centralWidget->setLayout(controlsLayout);
    
    setCentralWidget(centralWidget);
    

    0_1505382457956_screenshot.png

    raven-worxR 1 Reply Last reply
    0
    • A amruz

      i am having a program where i am creating many buttons in a gridlayout as shown below.
      but i am getting many spaces between the widgets. i tried setting the spacing between them.. but no luck can anyone guide me in this

      for(i=1;i<6;i++)

      {
      
          for(j=0;j<6;j++)
      
          {
      
              if(count<=26)
      
              {
      
                  QString str= QChar(s);
      
                  //QString myString = "a";
      
                  button[i][j] = new QPushButton(str);
      
                  s=s+1;
      
                  button[i][j]->setText(str);
      
                  button[i][j]->setFixedWidth(60);
      
                  controlsLayout->addWidget(button[i][j], i, j,0);
      
                  controlsLayout->setHorizontalSpacing(0);
      
                  controlsLayout->setVerticalSpacing(0);
      
       //           controlsLayout->setContentsMargins(  0,  0,  0,  0 );
      
                  controlsLayout->setSpacing(0);
      
                  connect(button[i][j], SIGNAL(clicked()), this ,SLOT(buttonWasClicked()));
      
                  count++;
      
              }
      
          }
      
      }
      
      controlsLayout->setHorizontalSpacing(0);
      
      controlsLayout->setVerticalSpacing(0);
      
      controlsLayout->setSpacing(0);
      
      
      centralWidget->setLayout(controlsLayout);
      
      setCentralWidget(centralWidget);
      

      0_1505382457956_screenshot.png

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @amruz
      i guess you want the buttons aligned compact and centered?

      If so there are a few ways:

      (1)
      you need to add spacing rows and columns surrounding your widget-grid -> add extra first and last row and same for columns. Inside is your current widget grid. Then use QGridLayouts setColumnStretch()/setRowStretch() for those rows/columns to a high value.

      or

      (2)
      create another widget and set your current layout to it. Then set the widget sizePolicy: w->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed). Place this widget in a container widget (which will then become your central widget):

      QVBoxLayout* containerLayout = new QVBoxLayout;
           containerLayout->addWidget( w, 0, Qt::AlignCenter );
      QWidget* containerWidget = new QWidget;
         containerWidget->setLayout( containerLayout );
      mainWin->setCentralWidget( containerWidget );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply
      1
      • raven-worxR raven-worx

        @amruz
        i guess you want the buttons aligned compact and centered?

        If so there are a few ways:

        (1)
        you need to add spacing rows and columns surrounding your widget-grid -> add extra first and last row and same for columns. Inside is your current widget grid. Then use QGridLayouts setColumnStretch()/setRowStretch() for those rows/columns to a high value.

        or

        (2)
        create another widget and set your current layout to it. Then set the widget sizePolicy: w->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed). Place this widget in a container widget (which will then become your central widget):

        QVBoxLayout* containerLayout = new QVBoxLayout;
             containerLayout->addWidget( w, 0, Qt::AlignCenter );
        QWidget* containerWidget = new QWidget;
           containerWidget->setLayout( containerLayout );
        mainWin->setCentralWidget( containerWidget );
        
        A Offline
        A Offline
        amruz
        wrote on last edited by
        #3

        @raven-worx thanks for the reply.. but i want to align the buttons as in a keyboard..with 3 rows

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          QPushButton has a maximum size policy by default. just add button[i][j]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          A 1 Reply Last reply
          2
          • VRoninV VRonin

            QPushButton has a maximum size policy by default. just add button[i][j]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

            A Offline
            A Offline
            amruz
            wrote on last edited by
            #5

            @VRonin thanks for the reply..but this code expands the button and the entire buttons occcupy the screen..i want the buttons as in keyboard at the bottom of the screen with minimum size and spacing between them..

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              in that case use use QSpacerItems all around your buttons.

              • change controlsLayout->addWidget(button[i][j], i, j); to controlsLayout->addWidget(button[i][j], i+1, j+1);
              • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding),0,1);
              • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding),6+2,1);
              • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred),1,0);
              • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred),1,6+2);

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              Q 1 Reply Last reply
              4
              • VRoninV VRonin

                in that case use use QSpacerItems all around your buttons.

                • change controlsLayout->addWidget(button[i][j], i, j); to controlsLayout->addWidget(button[i][j], i+1, j+1);
                • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding),0,1);
                • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding),6+2,1);
                • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred),1,0);
                • add controlsLayout->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred),1,6+2);
                Q Offline
                Q Offline
                Qt2019
                wrote on last edited by
                #7

                @VRonin good,it works for my code.

                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