Reputation: 25413
I wanna have a QTextEdit
and QPushButton
in a QBoxLayout
, where the button takes as little size as needed and the textedit all the rest.
So far I came up with this.
QPushButton* button = new QPushButton();
button->setText("Button");
QTextEdit* textedit = new QTextEdit();
QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
boxLayout->addWidget(textedit, 0, Qt::AlignTop);
boxLayout->addWidget(button, 0, Qt::AlignLeading);
mUI->centralWidget->setLayout(boxLayout);
There is still a padding between the textedit and the button. How can I remove it?
Upvotes: 1
Views: 985
Reputation: 1689
Try to remove Qt::AlignTop
:
QPushButton* button = new QPushButton();
button->setText("Button");
QTextEdit* textedit = new QTextEdit();
QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
boxLayout->addWidget(textedit, 0);
boxLayout->addWidget(button, 0, Qt::AlignLeading);
mUI->centralWidget->setLayout(boxLayout);
That worked for me fine
Upvotes: 3
Reputation: 12321
Use the setStretch
function.
boxLayout->setStretch(0, 1);
boxLayout->setStretch(1, 0);
EDIT
Use a QVBoxLayout
instead:
QPushButton* button = new QPushButton();
button->setText("Button");
QTextEdit* textedit = new QTextEdit();
QVBoxLayout* boxLayout = new QVBoxLayout();
boxLayout->addWidget(textedit);
boxLayout->addWidget(button);
boxLayout->setStretch(0, 1);
boxLayout->setStretch(1, 0);
mUI->centralWidget->setLayout(boxLayout);
Upvotes: 0