qt - Setting parent of a created qml object from C++ is not working -
i have c++ class creates qml object, takes 5 parameters:
//prototype // q_invokable qquickitem *createqmlobject(qobject *parent, qstring path, qstring id="", int x=0, int y=0); qquickitem * qmlitemcreator::createqmlobject(qobject* parent,qstring path,qstring id,int x,int y) { qqmlengine engine; qqmlcomponent component(&engine, qurl::fromlocalfile(path)); qquickitem * mainobject_; if(component.iserror()) { qwarning() << "qmlitemcreator::createqmlobject qmlcomponent " << path << " has errors: " << component.errorstring(); } else if (component.isready()) { mainobject_ = qobject_cast<qquickitem*>(component.create()); qqmlengine::setobjectownership(mainobject_, qqmlengine::javascriptownership); mainobject_->setproperty("id",id); mainobject_->setproperty("x",x); mainobject_->setproperty("y",y); mainobject_->setparent(parent); // qdebug()<<mainobject_<<" "<<mainobject_->parent()<<" "<<mainobject_->property("x"); } else { componentcomplete(); } return mainobject_; }
i use in qml follows:
item{ id: idroot component.oncompleted: { var obj = qmlcreator.createqmlobject(idroot,"path/test.qml") console.log(obj.parent)// print null !! } }
while in c++ context, can correctly print value of properties set, parent of created object. however, when print them qml, the
parenthas a
nulland properties are
undefined`.
i printed adress of created object c++ , qml , i've got same address. don't understand what's causing behaviour.
thanks help.
i've solved issue adding following line of code c++ code:
mainobject_->setparentitem(qobject_cast<qquickitem*>(parent));
Comments
Post a Comment