title: Why is a dynamically by C++ generated QML Item not aware of other pre-existing Items?
body: <p>I have a <code>QQuickView</code> that displays a QML file which itself consists of several QML Items (in separate files). I'd like to add Items dynamically using C++ code. The dynamically added item should resize with the parent one, i.e. <code>width</code> and <code>height</code> properties reference parent.</p>
  
  <p>For example, my target Item in QML looks like this:</p>
  
  <pre><code>// TargetContainer.qml
  
  Grid {
      id: fillMeWithItemsContainer
      objectName: "fillMeWithItemsContainer"
  }
  </code></pre>
  
  <p>The Item I want to add dynamically (maybe multiple times) looks like this:</p>
  
  <pre><code>// DynamicItem.qml
  
  Rectangle {
      color: "white"
  
      height: fillMeWithItemsContainer.height
      width: height * 4/3
  }
  </code></pre>
  
  <p><strong>Note that the rectangle references the container it is intended to reside in regarding height.</strong></p>
  
  <p><code>quickView</code> is populated with <code>TargetContainer</code>:</p>
  
  <pre><code>QQuickView *quickView = new QQuickView();
  quickView-&gt;setSource(QUrl("qrc:/foo/bar/TargetContainer.qml"));
  </code></pre>
  
  <p>So I load a component</p>
  
  <pre><code>QQmlComponent dynamicallyLoadedComponent(
                  quickView-&gt;engine(),
                  QUrl("qrc:/foo/bar/DynamicItem.qml")
                  );
  </code></pre>
  
  <p>And I create an Object out of it.</p>
  
  <pre><code>QObject *dynamicallyLoadedObject = dynamicallyLoadedComponent.create();
  </code></pre>
  
  <p><strong>Here I get an error</strong> (in application output view):</p>
  
  <pre><code>DynamicItem.qml:4: ReferenceError: fillMeWithItemsContainer is not defined
  </code></pre>
  
  <p><code>quickView</code> should be aware of the existence of <code>fillMeWithItemsContainer</code>, because it has been created before. However, <code>fillMeWithItemsContainer</code> is not a parent of <code>dynamicallyLoadedObject</code> (yet) and this could be the problem.</p>
  
  <p>So I find the target Item by </p>
  
  <pre><code>QQuickItem *targetItem = quickView-&gt;rootObject()-&gt;findChild&lt;QQuickItem*&gt;("fillMeWithItemsContainer");
  </code></pre>
  
  <p>And reparent the previously created object</p>
  
  <pre><code>dynamicallyLoadedObject-&gt;setProperty("parent", QVariant::fromValue&lt;QObject*&gt;(targetItem ));
  </code></pre>
  
  <p>Note: I tried <code>dynamicallyLoadedObject-&gt;setParent()</code> before, but this seems to be a different kind of parent (<code>QObject</code> vs. parent property).</p>
  
  <p>However, the width and height properties of <code>dynamicallyLoadedObject</code> are set to 0 (because of the reference error, I assume) and won't change. Even if I set them again programatically</p>
  
  <pre><code>dynamicallyLoadedObject-&gt;setProperty("height", "fillMeWithItemsContainer.height;");
  dynamicallyLoadedObject-&gt;setProperty("width", "height * 4/3");
  </code></pre>
  
  <p>nothing changes.</p>
  
  <p><strong>If I define DynamicItem directly in QML it works:</strong></p>
  
  <pre><code>Grid {
      id: fillMeWithItemsContainer
      objectName: "fillMeWithItemsContainer"
  
      DynamicItem {}
  }
  </code></pre>
  
  <p>How do I make sure that dynamically added items can access Items that have been in the QML view before? Alternatively: What am I doing wrong?</p>