children().count() of QGroupBox returns 值超过预期
children().count() of QGroupBox returns value more than expected
我正在测试 Qt 文档的以下要点。
When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
我使用 QDesigner 创建了五个 QPushButton 和一个 QGroupbox。然后我将这些按钮添加到 QGridLayout 并将其设置为组框的布局。
然后我尝试检查groupbox的children。但它显示 6 children 而不是 5。一个是空的,其他是按钮。
这是我的代码。
QGridLayout *grd = new QGridLayout();
grd->addWidget(ui->pushButton,0,0);
grd->addWidget(ui->pushButton_2,0,1);
grd->addWidget(ui->pushButton_3,1,0,1,3);
grd->addWidget(ui->pushButton_4,2,0);
grd->addWidget(ui->pushButton_5,2,1);
ui->groupBox->setLayout(grd);
qDebug() << ui->groupBox->children().count();
foreach (QObject *button, ui->groupBox->children())
{
qDebug() << "obj name" << button->objectName();
QPushButton *push_button = qobject_cast<QPushButton *>(button) ;
if(push_button)
{
qDebug() << push_button->text();
}
}
我得到的结果。
6
obj name ""
obj name "pushButton"
"button 1"
obj name "pushButton_2"
"button 2"
obj name "pushButton_3"
"button 3"
obj name "pushButton_4"
"button 4"
obj name "pushButton_5"
"button 5"
谁能告诉我为什么 children().count() 等于 6 而不是 5?
执行以下操作:
qDebug() << "Class name:" << button->metaObject()->className();
你会看到一个 child 是 QGridLayout
或者:
grd->setObjectName("GridLayout");
并且名称应该显示在空字符串的位置
什么意思:widget的布局变成了widget的child。
我正在测试 Qt 文档的以下要点。
When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.
我使用 QDesigner 创建了五个 QPushButton 和一个 QGroupbox。然后我将这些按钮添加到 QGridLayout 并将其设置为组框的布局。
然后我尝试检查groupbox的children。但它显示 6 children 而不是 5。一个是空的,其他是按钮。
这是我的代码。
QGridLayout *grd = new QGridLayout();
grd->addWidget(ui->pushButton,0,0);
grd->addWidget(ui->pushButton_2,0,1);
grd->addWidget(ui->pushButton_3,1,0,1,3);
grd->addWidget(ui->pushButton_4,2,0);
grd->addWidget(ui->pushButton_5,2,1);
ui->groupBox->setLayout(grd);
qDebug() << ui->groupBox->children().count();
foreach (QObject *button, ui->groupBox->children())
{
qDebug() << "obj name" << button->objectName();
QPushButton *push_button = qobject_cast<QPushButton *>(button) ;
if(push_button)
{
qDebug() << push_button->text();
}
}
我得到的结果。
6
obj name ""
obj name "pushButton"
"button 1"
obj name "pushButton_2"
"button 2"
obj name "pushButton_3"
"button 3"
obj name "pushButton_4"
"button 4"
obj name "pushButton_5"
"button 5"
谁能告诉我为什么 children().count() 等于 6 而不是 5?
执行以下操作:
qDebug() << "Class name:" << button->metaObject()->className();
你会看到一个 child 是 QGridLayout
或者:
grd->setObjectName("GridLayout");
并且名称应该显示在空字符串的位置
什么意思:widget的布局变成了widget的child。