Search code examples
c++qtqtexteditboxlayoutqpushbutton

QBoxLayout add QTextEdit full size and QPushButton


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


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