adusting the spacing in qgridlayout



  • 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


  • Moderators

    @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 );
    


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



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



  • @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..



  • 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);

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.