1

我想要一个QTextEditand QPushButtonin a QBoxLayout,其中按钮需要的大小尽可能小,其余的都是 textedit 。

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

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 和按钮之间仍然有一个填充。我怎样才能删除它?

布局截图

4

2 回答 2

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

这对我来说很好

于 2013-12-10T09:22:55.873 回答
0

使用该setStretch功能。

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

编辑

使用 aQVBoxLayout代替:

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);
于 2013-12-10T08:42:06.717 回答