In QTVerticalLayout How to remove spaces from widgets?
-
Hi,
How did you remove the space ?
-
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??
-
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);
-
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.
-
You can use an image sharing site like imgur.