title: How to get a QMainWindow from ApplicationWindow QML file to allow use of QDockWidget with QML files
body: <p>I am using Qt 5.5.0
  I am trying to use QDockWidget with QML files.
  My main.qml is currently set up to be an Application window with a set of buttons that are intended to select the various QML ‘page’ files, which I want to open in dockable windows.</p>
  
  <p>I want these page selection buttons to be in a QML file, located in a fixed area of the screen.
  Currently these buttons are in the ToolBar section, to achieve this and main.qml is set up to be an ApplicationWindow.</p>
  
  <p>In order to use QDockWidget, I need a QMainWindow.
  I have tried to follow the example given in
  <a href="https://stackoverflow.com/questions/27167439/how-to-embed-qml-toolbar-and-menubar-into-qmainwindow">How to embed QML toolbar and menubar into QMainWindow</a></p>
  
  <p>So in main.cpp, I have tried to get a QMainWindow associated with my main.qml, as follows:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QQmlApplicationEngine&gt;
  #include &lt;QDebug&gt;
  #include &lt;QQmlContext&gt;
  #include &lt;QQmlEngine&gt;
  #include &lt;QtQml&gt;
  #include &lt;QtQuick&gt;
  #include &lt;QMainWindow&gt;
  #include &lt;QGridLayout&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QQuickWidget&gt;
  
  #include "pageset.h"
  
  QMainWindow* GlobalMainWindow;
  
  int main(int argc, char *argv[])
  {
  
      QApplication app(argc, argv);
  
      qmlRegisterType&lt;PageSet&gt;("pagesetter", 1, 0, "MyPageSet");
  
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("main.qml")));
  
  //I’ve added the following just to try and get a QMainWindow....
  
      QObject *rootObject = engine.rootObjects().first();
      QQuickWindow *qmlWindow = qobject_cast&lt;QQuickWindow *&gt;(rootObject);
  
      /* From https://stackoverflow.com/questions/27167439/how-to-embed-qml-toolbar-and-menubar-into-qmainwindow */
      QWidget *container = QWidget::createWindowContainer(qmlWindow);
      container-&gt;setMinimumSize(qmlWindow-&gt;size());   //This IS required!
  
      //Place container to the top of your widget
  
      QMainWindow *mainwindow = new QMainWindow();
      GlobalMainWindow = mainwindow;
      //NB. Crashes without setMenuWidget!!
      mainwindow-&gt;setMenuWidget(container);
  
      //At first, get a small white border around the qml window - and no buttons are responsive
      //then if you somehow adjust size so it 'snaps', all looks ok and buttons work!
      mainwindow-&gt;setMinimumSize(qmlWindow-&gt;size());   //??
      mainwindow-&gt;show();
  
      return app.exec();
  }
  </code></pre>
  
  <p>I’m not sure if the above use of QMainWindow is valid, since the QML file displays fine without any of the code between engine.load and return.</p>
  
  <p>Anyway, my main.qml is something like:</p>
  
  <pre><code>import QtQuick 2.4
  import QtQuick.Controls 1.4
  import QtQuick.Window 2.2
  import QtQuick.Layouts 1.2
  import pagesetter 1.0
  
  ApplicationWindow
  {
      title: qsTr("Test")
      visible: true
      width:1080
      height:720
  
      MyPageSet{
          id: pageSet
      }
  
      toolBar: ToolBar{
          GridLayout{
          x: 4; anchors.top: parent.top; anchors.bottomMargin: 4; anchors.fill:parent
          rows: 2;
          columns: 5;
  
          Button 
          {
              Layout.fillWidth: true; Layout.fillHeight: true;
              text: "page1"; checkable: true; 
              onClicked:
              {
                 pageSet.display("Page1.qml")
              }
          }
          }   //End of Grid
  
      }   //End of toolbar
  }
  </code></pre>
  
  <p>Where MyPageSet is registered as a C++ class ‘PageSet’, with ‘display’ method as follows:</p>
  
  <pre><code>void PageSet::display(QString file)
  {
     QDockWidget *dock = new QDockWidget(tr("DockWidget name TBD"), GlobalMainWindow);
  
     QQuickView *view = new QQuickView();
  
     QWidget *container = QWidget::createWindowContainer(view, dock);
     container-&gt;setMinimumSize(200, 200);
     container-&gt;setMaximumSize(200, 200);
     container-&gt;setFocusPolicy(Qt::TabFocus);
  
     view-&gt;setSource(QUrl(file));
  
  
     dock-&gt;setWidget(container);
  
     GlobalMainWindow-&gt;addDockWidget(Qt::LeftDockWidgetArea, dock);
  }
  </code></pre>
  
  <p>Page1.qml is</p>
  
  <pre><code>Rectangle
  {
   ...various buttons
  }
  </code></pre>
  
  <p>The code runs but although I can see the Page1.qml file is loaded (I have debug in various custom types when they are created), I do not see the page displayed.</p>
  
  <p>I suspect this is due to my QMainWindow not really being what I need it be to.</p>
  
  <p>I know that Page1.qml can be displayed correctly if I just add it directly to main.qml, so I do not think there is anything wrong with that.</p>
  
  <p>Can anyone advise how I should go about getting a QMainWindow so that I can attach dock widgets to it, or any other suggestions as to what I am doing wrong and why nothing is displayed.</p>