QBoxLayout add QTextEdit full size and QPushButton

793 views Asked by At

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?

Screenshot of Layout

2

There are 2 answers

0
dvvrd On BEST ANSWER

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

0
pnezis On

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