title: QML Dynamic insertions of components in loader from C++
body: <p><em>I've fully edit my question since I've made some progress, and the first one was unclear.</em></p>
  
  <p>I use Qt 4.8, with QtQuick 1.0.</p>
  
  <p>I have a page where I need to keep the top and the bottom margin. So I've defined a <strong>Main.qml</strong> like that :</p>
  
  <pre><code>Item  {
      id: salesWindow
      width: 800
      height: 600
  
  [...] //Properties def
  
  TopBar {[...]}
  
  CloseButton{[...]}
  
  Rectangle {[...]}
  
  //I want to load qml file in this loader. The QML file loaded use some of the Main.qml properties
  Loader {
      id: appPlaceHolder
      objectName: "loader"
      anchors.centerIn: parent
   }
  
  Rectangle {[...]}
  
  BotBar {[...]}
  
  }
  </code></pre>
  
  <p>If I put a qml file into the loader sourceComponent, it works.
  Now I want to do it with C++, and well designed. I've subclass QDeclarativeComponent in <strong>SalesAppDisplay.h</strong></p>
  
  <pre><code>class SalesAppDisplay : public IDisplayScreen
  {
    Q_OBJECT
  
    static const std::string QML_FILENAME;
    static const std::string QML_DIR_PATH;
  public:
    SalesAppDisplay(DisplayContext&amp; context, QDeclarativeEngine&amp; engine, QObject* parent = 0);
  
    ~SalesAppDisplay();
  
    void doScreenInit();
  
    const std::string getQmlFilename() const;    
  };
  
  class IDisplayScreen : public QDeclarativeComponent
  {
    Q_OBJECT
  [...]
  }
  </code></pre>
  
  <p>and the <strong>Ctor</strong> in charge of component instanciation :</p>
  
  <pre><code>IDisplayScreen::IDisplayScreen(DisplayContext&amp; context, QDeclarativeEngine&amp; engine, std::string     qmlFilepath, QObject* parent)
  : QDeclarativeComponent(&amp;engine, QString(qmlFilepath.c_str()), parent)
  </code></pre>
  
  <p>Instead of loading a qml file in the loader by changing the source, I want to insert my component into the QML from <strong>main.cpp</strong> :</p>
  
  <pre><code>  m_view.setSource(QUrl::fromLocalFile("../displaymanager/rsrc/qml/Main.qml"));
  
    QObject* mainObj = m_view.rootObject();
    [ .. Set file property ]
  
    //this is the component subclass instantiation (made by factory)
    m_currentScreen = displaymanager::createDisplayScreen(IDisplayScreen::SALESAPP, *(m_context), *(m_view.engine()), mainObj);
  
    QDeclarativeItem* saleAppObj = qobject_cast&lt;QDeclarativeItem*&gt;(m_currentScreen-&gt;create(m_view-&gt;rootContext()));
    saleAppObj-&gt;setParentItem(qobject_cast&lt;QDeclarativeItem*&gt;(mainObj));
    [ .. Set file property ]
  
    //I can find my loader without any problems
    QDeclarativeItem *loader = mainObj-&gt;findChild&lt;QDeclarativeItem*&gt;("loader");
    /* I don't know what to do here for making it works */
  
    m_view.show();
    m_qApp.exec();
  </code></pre>
  
  <p>I've tried <code>loader-&gt;setProperty("sourceComponent", qobject_cast&lt;QVariant&gt;(saleAppObj));</code>, and some other tricks like that without any results. </p>
  
  <p>I have errors from my saleApp.qml saying that he don't know Main.qml properties that i used in it (he is clearly load at the Components instanciation). And despite that main.qml is perfectly loaded, nothing from SaleApp.qml appears.</p>