QWidget::layout()函数的问题
程序员文章站
2022-07-13 21:18:29
...
QLayout * QWidget::layout() const
Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed.The layout manager sets the geometry of the widget's children that have been added to the layout.
See also setLayout(), sizePolicy(), and Layout Management.
比如这样一个窗口:
对象关系:
简单的说就是MainWindow中间放置了一个按钮,然后MainWindow设置了垂直布局:
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QStringLiteral("MainWindow"));
MainWindow->resize(243, 106);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
verticalLayout = new QVBoxLayout(centralWidget);
verticalLayout->setSpacing(6);
verticalLayout->setContentsMargins(11, 11, 11, 11);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
pushButton = new QPushButton(centralWidget);
pushButton->setObjectName(QStringLiteral("pushButton"));
verticalLayout->addWidget(pushButton);
MainWindow->setCentralWidget(centralWidget);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
那么下面的代码运行结果如何?
qDebug()<<this->ui->centralWidget->layout();
qDebug()<< this->ui->pushButton->layout();
答案是:
QVBoxLayout(0xf26448, name = "verticalLayout")
QObject(0x0)
可见QWidget::layout 获取到的是他的子控件。
如果一个控件被添加进了layout中,那么通过这个函数是获取不到包含他的layout的。