QBoxLayout添加QTextEdit全尺寸和QPushButton

时间:2013-12-10 08:32:25

标签: c++ qt qtextedit boxlayout qpushbutton

我希望在QTextEdit中有一个QPushButtonQBoxLayout,其中按钮的大小与所需的一样短。

到目前为止,我想出了这个。

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

textedit和按钮之间仍然有一个填充。我该如何删除它?

Screenshot of Layout

2 个答案:

答案 0 :(得分:3)

尝试删除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);

这对我很有用

答案 1 :(得分:0)

使用setStretch功能。

boxLayout->setStretch(0, 1);
boxLayout->setStretch(1, 0);

修改

改为使用QVBoxLayout

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