Question

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

Was it helpful?

Solution

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top