title: QML get the winId of a loaded qml window
body: <p>I would like to get the winId of a qml window. I have the following files.</p>
  
  <p>main.qml :</p>
  
  <pre><code>import QtQuick 2.5
  import QtQuick.Window 2.2
  import QtQuick.Controls 1.4
  
  Window {
      id: myMainWindow
      title: "MyMainWindow"
  
      width: 200
      height: 200;
      visible: true
  
      Component.onCompleted: {
          x = 40
          y = 40
      }
  }
  </code></pre>
  
  <p>and my main.cpp :</p>
  
  <pre><code>#include &lt;QGuiApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QWindow&gt;
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      QQmlApplicationEngine engine;
      qmlRegisterType&lt;FbItem&gt;("fbitem", 1, 0, "FbItem");
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
  
      QObject* m_rootObject = engine.rootObjects().first();
      auto rect_area = m_rootObject-&gt;findChild&lt;QWindow*&gt;("myMainWindow"); //the id of the Window in qml
  
      //both lines make the application crash
      //HWND hWnd = reinterpret_cast&lt;HWND&gt;(rect_area-&gt;winId());
      WId wid = rect_area-&gt;winId();
  
      return app.exec();
  }
  </code></pre>
  
  <p>The crash message is :</p>
  
  <pre><code>The inferior stopped because it triggered an exception.
  Stopped in thread 0 by: Exception at 0x13500da, code: 0x0000005: read access violation at: 0x0, flags=0x0 (first chance).
  </code></pre>
  
  <p>What is wrong? How can I get the winId of my window?</p>
  
  <p>EDIT : we can see that rect_area is still bad. In edited main.qml :</p>
  
  <pre><code>Window {
      id: _component
      objectName: "myMainWindow"
      ...
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6KCxo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6KCxo.jpg" alt="enter image description here"></a></p>