set parent when creating a QML component from C++
Hello,
I've a QML component that is created from C++ code:
QML code:
Code:
Rectangle {
width: parent.width
y: parent.height / 2
color: "red"
}
Code:
QQmlComponent component
(engine,
QUrl(src
));
QObject *comp
= component.
create();
QQuickItem *item = qobject_cast<QQuickItem*>(comp);
item->setParentItem(root);
When the component is created appears these errors:
TypeError: Cannot read property 'height' of null
TypeError: Cannot read property 'width' of null
On creation, the parent property is null and not is valid until the item->setParentItem is executed. How can I set the parent item when creating the component? or how to avoid these errors?
Best regards.
Re: set parent when creating a QML component from C++
Have you tried a condition on parent?
Code:
width: parent ? parent.width : 0
Cheers,
_
Re: set parent when creating a QML component from C++
It works! Thank you very much.