[英]Qt layouts, difference between passing and not passing QWidget as parent
我創建了一個簡單的QHBoxLayout
(水平),將其推到QVBoxLayout
(垂直)的底部,它包含兩個按鈕。 看到代碼:
QWidget* create_ver_and_horizontal_box() {
QWidget* temp = new QWidget();
// Add buttons to the horizontal box
QHBoxLayout* hbox = new QHBoxLayout();
QPushButton *ok = new QPushButton("OK");
QPushButton *cancel = new QPushButton("Cancel");
hbox->addWidget(ok);
hbox->addWidget(cancel);
// Create a vertical box and add the horizontal box to
// the end of it
QVBoxLayout* vbox = new QVBoxLayout();
vbox->addStretch(1);
vbox->addLayout(hbox);
// set the layout and return
temp->setLayout(vbox);
return temp;
}
但是,當我將QWidget temp
添加為QHBoxLayout的父級時,如下所示:
// Add buttons to the horizontal box
QHBoxLayout* hbox = new QHBoxLayout(temp);
我想了解這里發生了什么。 在某些情況下,我希望QWidget成為布局或任何其他QWidget的父項,在那種情況下,我不讓包含QWidget的人成為包含QWidget的父項。 例如,我可以將temp添加為兩個“按鈕”的父級,但我沒有。 不添加與添加的含義是什么。
謝謝,
QHBoxLayout* hbox = new QHBoxLayout(temp);
相當於
QHBoxLayout* hbox = new QHBoxLayout();
temp->setLayout(hbox);
也就是說,您正在制作負責temp
的水平布局。
對setLayout(vbox)
的調用應該已經生成了運行時警告消息,該temp
消息已經具有布局,以進行提示。
由於您希望垂直布局負責該小部件,因此請保留temp->setLayout(vbox)
或將temp
傳遞給QVBoxLayout
的構造QVBoxLayout
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.