<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to set the parent item of a QDeclarativeComponent prior creation]]></title><description><![CDATA[<p dir="auto">I am trying to move the creation of qml pages from QML to C++:</p>
<p dir="auto">First, I create the parent page<br />
@<br />
mView = new QDeclarativeView();<br />
mView-&gt;setSource(QUrl("qrc:/libpages/parentPage.qml"));<br />
mView-&gt;show();<br />
@</p>
<p dir="auto">then I create the other pages with QDeclarativeComponent.:<br />
@<br />
QDeclarativeComponent *component = new QDeclarativeComponent(mView-&gt;engine(), mView-&gt;rootObject());<br />
component-&gt;loadUrl(QUrl("qrc:/libpages/myPage.qml"));<br />
...<br />
@</p>
<p dir="auto">I want to inherit the context of the parentPage, so I use QDeclarativeEngine::contextForObject(mView-&gt;rootObject()).<br />
I want also to define the parent item of myPage.qml to be parentPage.qml.<br />
@<br />
QObject * object = component-&gt;beginCreate(QDeclarativeEngine::contextForObject(mView-&gt;rootObject()));<br />
if (NULL == object)<br />
{<br />
component-&gt;completeCreate();<br />
}<br />
else<br />
{<br />
object-&gt;setProperty("parent", QVariant::fromValue(qobject_cast&lt;QDeclarativeItem*&gt;(mView-&gt;rootObject())));<br />
object-&gt;setProperty("visible", QVariant(false));<br />
component-&gt;completeCreate();<br />
}<br />
@</p>
<p dir="auto">First question: is the above code correct ?<br />
Second question: is there an easier way to set the parent element of myPage.qml, prior to the beginCreate() call? The qml code "object Component::createObject ( Item parent, object properties )" accepts a parent element. Is there an equivalent in c++ ?</p>
<p dir="auto">Thanks<br />
Anthony</p>
]]></description><link>https://forum.qt.io/topic/16023/how-to-set-the-parent-item-of-a-qdeclarativecomponent-prior-creation</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 13:21:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/16023.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 19 Apr 2012 11:43:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to set the parent item of a QDeclarativeComponent prior creation on Sun, 22 Apr 2012 23:36:25 GMT]]></title><description><![CDATA[<p dir="auto">Yikes, ok, that setParentItem / "parent" property name thing is interesting - I didn't know that ;-)  Thanks.<br />
The fact that the parent isn't set prior to the onCompleted callback being called sounds like a bug, to me.<br />
/edit: well, the fact that we don't have API to allow a parent argument, perhaps would be a better way of putting it.  What you do currently, seems ok to me too, as the beginCreate()/completeCreate() split exists to allow the finer control which you use.</p>
<p dir="auto">Regarding ownership, setting a parent object will prevent the object from being collected, but won't (necessarily) prevent the parent object from being collected (unless it explicitly has CppOwnership).  In your case, I believe that it will have explicit CppOwnership anyway, since it is the root object of the view.  Thus, I think my advice about explicitly setting the ownership semantics of the child object was unnecessary.  Sorry for the confusion!</p>
<p dir="auto">I'll look into the "parent not set until after the onCompleted handler being called" issue and then respond to this thread again.</p>
<p dir="auto">Cheers,<br />
Chris.</p>
]]></description><link>https://forum.qt.io/post/136545</link><guid isPermaLink="true">https://forum.qt.io/post/136545</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Sun, 22 Apr 2012 23:36:25 GMT</pubDate></item><item><title><![CDATA[Reply to How to set the parent item of a QDeclarativeComponent prior creation on Fri, 20 Apr 2012 08:24:05 GMT]]></title><description><![CDATA[<p dir="auto">[quote author="chriadam" date="1334877825"]Out of interest, why are you using beginCreate() instead of create()?<br />
[/quote]<br />
My code uses "Component.onCompleted" to execute some javascript code that will use the parent.width. In case I call create(), "Component.onCompleted" will be executed with a "parent" property set to "null". If I set later on the "parent" property, the "Component.onCompleted" javascript code will not be executed again. So I must set explicitly a "onParentChanged" handler to call again the init code. This causes a new execution of the javascript code plus property bindings for the "parent" property, thus creating a longer creation time.</p>
<p dir="auto">As explained in <a href="http://qt-project.org/doc/qt-4.8/qdeclarativecomponent.html#beginCreate" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-4.8/qdeclarativecomponent.html#beginCreate</a>, beginCreate and completeCreate allows me to distinguish the init steps and I can insert between boths the init of the parent item.<br />
[quote]<br />
Note that in QML you can pass a parent as a parameter (ie, component.createObject(parentArg)).<br />
[/quote]<br />
From my point of view, component.createObject(parentArg) will set the "parent" property of the QDeclarativeItem plus maybe the QObject::setParent(QObject*).<br />
[quote]<br />
In C++ you can set the ownership explicitly to be CppOwnership which will prevent the engine from cleaning up the item due to (implied) JS ownership.<br />
[/quote]<br />
I understand better. If I want to avoid my object being destroyed, I can replace<br />
@<br />
object-&gt;setParent(mView-&gt;rootObject());<br />
@</p>
<p dir="auto">with<br />
@<br />
QDeclarativeEngine::setObjectOwnership(object, QDeclarativeEngine::CppOwnership);<br />
@</p>
<p dir="auto">Thanks<br />
[quote]</p>
<p dir="auto">Using object-&gt;setProperty("parent", ...) is generally wrong.  If you have a QObject and you want to set its (ownership) parent, use QObject::setParent().<br />
[/quote]</p>
<p dir="auto">I have not confused object-&gt;setProperty("parent", ...) and QObject::setParent().<br />
In fact, the call<br />
@<br />
object-&gt;setProperty("parent", QVariant::fromValue(qobject_cast&lt;QDeclarativeItem*&gt;(mView-&gt;rootObject())));<br />
@<br />
is equivalent to<br />
@<br />
qobject_cast&lt;QDeclarativeItem*&gt;(object)-&gt;setParentItem(qobject_cast&lt;QDeclarativeItem*&gt;(mView-&gt;rootObject()));<br />
@<br />
This call is necessary so that the QML item has a parent item and can be displayed.</p>
<p dir="auto">So, my initial question came again, is there a way to simplify the following code with keeping in mind that "Component.onCompleted" must be executed with a correct parent item set (like createObject in javascript ?).<br />
@<br />
QObject * object = component-&gt;beginCreate(QDeclarativeEngine::contextForObject(mView-&gt;rootObject()));<br />
if (NULL == object)<br />
{<br />
component-&gt;completeCreate();<br />
}<br />
else<br />
{<br />
object-&gt;setProperty("parent", QVariant::fromValue(qobject_cast&lt;QDeclarativeItem*&gt;(mView-&gt;rootObject())));<br />
object-&gt;setParent(mView-&gt;rootObject()); //OR QDeclarativeEngine::setObjectOwnership(object, QDeclarativeEngine::CppOwnership);<br />
object-&gt;setProperty("visible", QVariant(false));<br />
component-&gt;completeCreate();<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/136362</link><guid isPermaLink="true">https://forum.qt.io/post/136362</guid><dc:creator><![CDATA[agroyer]]></dc:creator><pubDate>Fri, 20 Apr 2012 08:24:05 GMT</pubDate></item><item><title><![CDATA[Reply to How to set the parent item of a QDeclarativeComponent prior creation on Thu, 19 Apr 2012 23:23:45 GMT]]></title><description><![CDATA[<p dir="auto">Out of interest, why are you using beginCreate() instead of create()?<br />
Note that in QML you can pass a parent as a parameter (ie, component.createObject(parentArg)).<br />
In C++ you can set the ownership explicitly to be CppOwnership which will prevent the engine from cleaning up the item due to (implied) JS ownership.</p>
<p dir="auto">Using object-&gt;setProperty("parent", ...) is generally wrong.  If you have a QObject and you want to set its (ownership) parent, use QObject::setParent().</p>
]]></description><link>https://forum.qt.io/post/136343</link><guid isPermaLink="true">https://forum.qt.io/post/136343</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Thu, 19 Apr 2012 23:23:45 GMT</pubDate></item><item><title><![CDATA[Reply to How to set the parent item of a QDeclarativeComponent prior creation on Thu, 19 Apr 2012 16:23:38 GMT]]></title><description><![CDATA[<p dir="auto">I found today that this way of initializing the object make my application crash.<br />
This is caused by a DeferredDelete event because my object had no parent.</p>
<p dir="auto">I understood that I need also to call "QObject::setParent(QObject*)".<br />
Initializing the "parent" property of the QDeclarativeItem is necessary to place the item in the scene but it is not enough. This is 2 different things.</p>
<p dir="auto">The new code is<br />
@<br />
QObject * object = component-&gt;beginCreate(QDeclarativeEngine::contextForObject(mView-&gt;rootObject()));<br />
if (NULL == object)<br />
{<br />
component-&gt;completeCreate();<br />
}<br />
else<br />
{<br />
object-&gt;setProperty("parent", QVariant::fromValue(qobject_cast&lt;QDeclarativeItem*&gt;(mView-&gt;rootObject())));<br />
object-&gt;setParent(mView-&gt;rootObject());<br />
object-&gt;setProperty("visible", QVariant(false));<br />
component-&gt;completeCreate();<br />
}<br />
@</p>
<p dir="auto">Is there a way to achieve the init of both "parents" within one call ?</p>
]]></description><link>https://forum.qt.io/post/136325</link><guid isPermaLink="true">https://forum.qt.io/post/136325</guid><dc:creator><![CDATA[agroyer]]></dc:creator><pubDate>Thu, 19 Apr 2012 16:23:38 GMT</pubDate></item></channel></rss>