title: Create qml object dynamically from c++ object (by using setContextProperty)
body: <p>I am trying to create a qml object dynamically in c++ using the object of c++ class. Below is the minimal code for my approach. Upon execution of this code and after clicking, the application is crashing(see the comment in main.qml).</p>
  
  <p>I have pasted the code below and It can be downloaded <a href="https://files.fm/u/b6pw53ja" rel="nofollow noreferrer">here</a>.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QQmlContext&gt;
  #include "scene.h"
  int main(int argc, char *argv[])
  {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
  
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      scene sc(engine);
  
      QQmlContext* context = engine.rootContext();
      context-&gt;setContextProperty("sc", &amp;sc);
  
      if (engine.rootObjects().isEmpty())
          return -1;
  
      return app.exec();
  }
  </code></pre>
  
  <p><strong>scene.h</strong></p>
  
  <pre><code>#ifndef SCENE_H
  #define SCENE_H
  
  #include &lt;QObject&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QQmlComponent&gt;
  class scene : public QObject
  {
      Q_OBJECT
  public:
      explicit scene(QQmlApplicationEngine&amp; engine, QObject *parent = nullptr);
      QQmlApplicationEngine&amp; engine;
  
  public slots:
      void create_rect_object();
  };
  
  #endif // SCENE_H
  </code></pre>
  
  <p>scene.cpp</p>
  
  <pre><code>#include "scene.h"
  
  scene::scene(QQmlApplicationEngine &amp;engine, QObject *parent) : engine(this-&gt;engine),QObject(parent)
  {
  
  }    
  void scene::create_rect_object()
  {
      QQmlComponent component(&amp;engine, QUrl::fromLocalFile("myrect.qml"));
      QObject *object = component.create();
      object-&gt;setProperty("width", 200);
      object-&gt;setProperty("height", 150);
      object-&gt;setProperty("color", "blue");
  }
  </code></pre>
  
  <p>main.qml</p>
  
  <pre><code>import QtQuick 2.11
  import QtQuick.Window 2.11
  
  Window {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
      Rectangle{
          anchors.fill: parent
          color: "red"
          MouseArea{
              anchors.fill: parent
              onClicked: {
                  console.log("Before click");
                  sc.create_rect_object(); // application is crashing here
                  console.log("after click");
              }
          }
      }
  }
  </code></pre>
  
  <p>myrect.qml</p>
  
  <pre><code>import QtQuick 2.0
  
  Rectangle {
      id:id_rec
      width: 100
      height: 100
      color: "green"
      x:0
      y:0
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>The object to be created is not the child of the root of main window but child of one of the item inside the chain of children items of root of mainwindow. The pseudo structure looks like below.</p>
  
  <p><strong>main.qml</strong></p>
  
  <pre><code>Window {       
  
      customitem1{
          id:id_ci1
  
      }    
      customitem2{
          id:id_ci1        
      }       
  }
  </code></pre>
  
  <p><strong>customitem1.qml</strong></p>
  
  <pre><code>Item {       
  
      customitem3{
          id:id_ci3
  
      }    
      customitem3{
          id:id_ci4        
      }       
  }
  </code></pre>