aboutsummaryrefslogtreecommitdiffstats
path: root/examples/EmbeddedWindow/QmlApp/embeddedwindow.cpp
blob: 3b7b17dddebc1c86366cb1da19829ae6c43b09c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/***************************************************************************************************
 Copyright (C) 2023 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

#include "embeddedwindow.h"

#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickView>
#include <QWindow>

#include "mainwindow.h"

EmbeddedWindow::EmbeddedWindow(QQmlEngine *qmlEngine, MainWindow *mainWindow)
    : qmlEngine(qmlEngine), mainWindow(mainWindow)
{
    connect(mainWindow, &MainWindow::contentRendered, this, &EmbeddedWindow::show);
    connect(mainWindow, &MainWindow::closed, this, &EmbeddedWindow::close);
}

EmbeddedWindow::~EmbeddedWindow()
{
    delete quickView;
}

void EmbeddedWindow::show()
{
    embeddedWindow = QWindow::fromWinId((WId)mainWindow->hostHandle());
    quickView = new QQuickView(qmlEngine, embeddedWindow);
    qmlEngine->rootContext()->setContextProperty("window", quickView);
    quickView->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
    quickView->show();
}

void EmbeddedWindow::close()
{
    embeddedWindow->close();
}