我能够找到QRadioButton这种方式:
for(int i = 0; i < ui->verticalLayout->count(); i++)
{
QRadioButton* r = qobject_cast<QRadioButton*>(ui->verticalLayout->itemAt(i)->widget());
if(r->isChecked())
//found it!
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这种迭代元素的方式,并希望使用该foreach构造.我的第一次尝试失败了
foreach(QRadioButton* child, ui->verticalLayout->findChildren<QRadioButton*>())
{
if(child->isChecked())
//found it!
}
Run Code Online (Sandbox Code Playgroud)
问题是ui->verticalLayout->findChildren<QRadioButton*>()返回零元素.它也不返回任何元素findChildren<QObject*>().有人可以解释一下这种行为吗?
注意:这个问题的标题几乎与我的相同,但它与python Qt有关,并且不包含任何有用的信息.
实验上,我认为ui->verticalLayout->children().count()返回零,其中ui->verticalLayout->count()返回我在中的元素数量verticalLayout.这意味着itemAt(i)并且findChild<QRadioButton*>()不访问相同的列表.查看Qt文档对children()我没有帮助.
有人能指出我关于Qt孩子父母概念的好材料吗?我假设这与访问嵌套对象无关,这正是我想要完成的.
正如Kuba Ober所建议的那样,这个问题的答案包含了关于另一个主题的有价值的信息,而他的答案则澄清了我关于布局儿童的问题.因此,这不是一个重复的问题.