1

how to embed qquickwindow into qwidget ? my code is like this one:

mainwindow.cpp

#include "mainwindow.h"
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
engine = new QQmlApplicationEngine(":/drawer.qml", this);
auto wins = engine->rootObjects();
if (wins.size() > 1) {
QQuickWindow *win=0;
win=wins.at (0)->findChild<QQuickWindow*>("win1");
if (win) {
  win->setFlags (Qt::FramelessWindowHint);
  this->setMinimumSize (win->size ());
  this->resize (win->size ());      

 ui->gridLayout_qml->addWidget(QWidget::createWindowContainer(QWindow::fromWinId (win->winId ()),this));
   }
  }
 }

 MainWindow::~MainWindow() {
  delete engine;
  delete ui;
 }

drawer.qml

 import QtQuick 2.6
 import QtQuick.Layouts 1.0
 import Qt.labs.controls 1.0

 ApplicationWindow {
  objectName: qsTr("win1")
  visible: true
 width: 640
 height: 480
  title: qsTr("Hello World")

  SwipeView {
    id: swipeView
    anchors.fill: parent
    currentIndex: tabBar.currentIndex

    Page {
        Label {
            text: qsTr("FIRST page")
            anchors.centerIn: parent
        }
    }

    Page {
        Label {
            text: qsTr("Second page")
            anchors.centerIn: parent
        }
    }
  }

  footer: TabBar {
    id: tabBar
    currentIndex: swipeView.currentIndex
    TabButton {
        text: qsTr("First")
    }
    TabButton {
        text: qsTr("Second")
     }
    }
 }

Above code is creating new seperate window rather than embedding it into gridLayout_qml. How to insert this qquickwindow into gridLayout_qml, any pointers? thanks Qt 5.6.2 windows 7 msvc2015.

user3453753
  • 357
  • 4
  • 15

1 Answers1

1

ApplicationWindow is a top-level window. Change it to something that is not a window, for example Page.

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • thanks for quick answer. I have changed ApplicationWindow to Page and it displayed nothing. Anymore something to change ? Appereantly this line is failing to cast `win=wins.at (0)->findChild("win1"); ` so that `ui->gridlayout_qml->addwidget` is not called. What is supposed to be qml Page is derived from? – user3453753 May 20 '17 at 13:36
  • Ok I have solved this using qquickwidget instead of qquickwindow rather than using qwidget::createwindowcontainer manually. thanks – user3453753 May 20 '17 at 14:00
  • Hmm, my bad. I didn't read the code carefully enough. I somehow expected the "standard" window container procedure loading QML content with QQuickView, which itself is a window, and then embedding that to a window container. I realize now that you were actually loading QML with QQmlApplicationEngine, so there was in fact only one window. I suspect that the actual problem was the use if QWindow::fromWinId() instead of passing the application window directly. However, you're probably better off with QQuickWidget anyway since it's supposed to be the better alternative to window containers. – jpnurmi May 21 '17 at 09:56
  • Yes using simple QQuickWidget::setSource is better – user3453753 May 21 '17 at 13:27