[英]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.