Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>dynamicallyLoadedObject-&gt;setProperty("height", "fillMeWithItemsContainer.height;"); dynamicallyLoadedObject-&gt;setProperty("width", "height * 4/3"); </code></pre> <p>This will not actually set a JavaScript binding on the properties. Instead, it will try to assign e.g. the string <code>"fillMeWithItemsContainer.height;"</code> to the property, which will fail since the property is of type <code>int</code>, not of type <code>QString</code>. Assigning bindings to properties is actually not possible from within C++ (with some exceptions like <code>QQmlBinding</code>).</p> <pre><code>dynamicallyLoadedObject-&gt;setProperty("parent", QVariant::fromValue&lt;QObject*&gt;(targetItem )); </code></pre> <p>As Sergei mentioned, you need to call <code>QQuickItem::setParentItem</code> instead of setting the <code>parent</code> property. That is also a bit more typesafe than the general string-based <code>setProperty</code> API. Without a parent item, a QQuickItem will not be visible. Reparenting will only change the parent item, which will affect layouting and a few other things. It will <strong>not</strong> change the <em>context</em> of the object. A context defines which objects/IDs are in the scope. The context can not be changed after an item has been created. Even if changing the parent would change the context, it is too late - the object has been created, and IDs/object are only looked up in the creation phase.</p> <p>The solution is to pass the correct context to <code>QQmlComponent::create()</code>, which actually has an optional argument. You need to create your item in the context of <code>fillMeWithItemsContainer</code>, so you need to get a pointer to it (you did that already with <code>findChild</code>) and then retrieve its context, which is possible with <code>QQmlEngine::contextForObject()</code>. That should give you enough to figure out how to make it work.</p> <p>I agree with Sergei though, you should prefer to dynamically create objects in JavaScript instead. Changing QML from within C++ is a layering violation, you should never access QML from C++, only the other way around, to have a nicer separation between UI and program logic.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload