Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

In QTVerticalLayout How to remove spaces from widgets?



  • I have a vertical layout with 7 pushbuttons in it. The spacing setting is set to zero but the buttons still have spaces between them. How can I get rid of those spaces between the buttons?

    Thanks a bunch

    -emp1953


  • Lifetime Qt Champion

    Hi,

    How did you remove the space ?



  • @SGaist No, I cannot remove it.


  • Lifetime Qt Champion

    That was not my question. In any cas take a look at setContentsMargins



  • @SGaist I implemented the following:

    ui->verticalLayout->setSpacing(0);
    ui->verticalLayout->setMargin(0);
    ui->verticalLayout->setContentsMargins(0,0,0,0);

    This had no effect on the appearance of the buttons within the verticalLayout. The buttons were still evenly spaced.

    I am using Qt4.8 QtCreator 2.8.1 RedHat Enterprise 6.8

    Could this be a bug in 4.8??


  • Lifetime Qt Champion

    That's normal. If you want to have them all pushed up against each other on the top, you have to add a stretch after the last bottom with a value higher that the one used for the buttons. If you didn't set the stretch for the buttons (which is usually the case) then a stretch of one is enough. You can also use a QSpacerItem.



  • @SGaist I tried a stretch(1) shown commented out in my code below. I then implemented the spacerItem and put it into the layout via addItem()........
    This seems to pile all the buttons on top of one another in the upper left corner of the mainWindow:

    QPushButton *pushButton_1 = new QPushButton(this);
    QPushButton *pushButton_2 = new QPushButton(this);
    QPushButton *pushButton_3 = new QPushButton(this);
    QPushButton *pushButton_4 = new QPushButton(this);
    QPushButton *pushButton_5 = new QPushButton(this);
    QPushButton *pushButton_6 = new QPushButton(this);
    QSpacerItem *spc = new QSpacerItem(20,20,QSizePolicy::Expanding,QSizePolicy::Expanding);

    pushButton_1->setText("One");
    pushButton_2->setText("Two");
    pushButton_3->setText("Three");
    pushButton_4->setText("Four");
    pushButton_5->setText("Five");
    pushButton_6->setText("Six");
    
    QVBoxLayout *pLayout = new QVBoxLayout(this);
    
    
    pLayout->addWidget(pushButton_1, 0, Qt::AlignTop);
    pLayout->addWidget(pushButton_2, 0, Qt::AlignTop);
    pLayout->addWidget(pushButton_3, 0, Qt::AlignTop);
    pLayout->addWidget(pushButton_4, 0, Qt::AlignTop);
    pLayout->addWidget(pushButton_5, 0, Qt::AlignTop);
    pLayout->addWidget(pushButton_6, 0, Qt::AlignTop);
    //pLayout->addStretch(1);
    pLayout->addItem(spc);
    setLayout(pLayout);
    


  • Lifetime Qt Champion

    Can you show the result with your original code and the spacer item ?

    By the way, are you locked to Qt 4 ? It has reached end of life a long time ago.



  • @SGaist For the foreseeable future, Yes I am stuck with 4.8. Later version has not been cleared through security protocols.



  • @SGaist I tried on my original post to include a .png of my gui but was unable to post it.


  • Lifetime Qt Champion

    You can use an image sharing site like imgur.


Log in to reply