2013-12-10 30 views
1

我想有一個QTextEditQPushButtonQBoxLayout,其中按鈕需要的尺寸儘可能小,文本編輯其餘所有。QBoxLayout添加QTextEdit全尺寸和QPushButton

到目前爲止,我想出了這個。

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

回答

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

,對我罰款

工作
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); 
+0

不起作用。我到底要把它放在哪裏? – Niklas

+0

檢查我編輯的答案 – pnezis