我正在尝试获取QObject
类的子节点并将其数据添加到父节点(请参阅下面的QML示例)。但是,当我在构造函数中调用以下代码时,它不返回任何子代。似乎尚未填充子列表。如果我在paint()
函数中放置相同的代码,它就可以工作,但我需要更早的数据。
MultiGauge::MultiGauge() :
QQuickPaintedItem()
{
QObjectList children = this->children();
for (int i = 0; i < children.length(); i++)
{
this->myQList.append(children[i]->metaObject()->className());
}
}
这是QML文件
MultiGauge {
height: 125
width: height
Limits {
min: 0
caution: 1250
max: 2000
}
Limits {
min: 100
caution: 200
max: 300
}
}
编辑:解决方案:
MultiGauge::componentComplete()
{
// must call the parent's version of the function first
QQuickPaintedItem::componentComplete();
// now we can do what we need to
QObjectList children = this->children();
for (int i = 0; i < children.length(); i++)
{
this->myQList.append(children[i]->metaObject()->className());
}
}
答案 0 :(得分:1)
您应该延迟迭代,直到QML对象树完成。你可以使用
MultiGauge {
// ...
Component.onCompleted: doSomeStuff()
}