| 1 | // Copyright (C) 2020 The Qt Company. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QScreen> |
| 5 | #include <QApplication> |
| 6 | #include <QMainWindow> |
| 7 | #include <QMdiArea> |
| 8 | #include <QMdiSubWindow> |
| 9 | #include <QQuickWidget> |
| 10 | #include <QVBoxLayout> |
| 11 | #include <QPushButton> |
| 12 | |
| 13 | void configureMainWindow(QMainWindow *w, QMdiArea *mdiArea, QPushButton *button) |
| 14 | { |
| 15 | auto widget = new QWidget; |
| 16 | auto layout = new QVBoxLayout; |
| 17 | layout->addWidget(mdiArea); |
| 18 | layout->addWidget(button); |
| 19 | widget->setLayout(layout); |
| 20 | w->setCentralWidget(widget); |
| 21 | } |
| 22 | |
| 23 | int main(int argc, char* argv[]) |
| 24 | { |
| 25 | QApplication app(argc, argv); |
| 26 | |
| 27 | QMainWindow w1; |
| 28 | w1.winId(); |
| 29 | w1.windowHandle()->setScreen(QGuiApplication::screens().at(0)); |
| 30 | auto mdiArea1 = new QMdiArea; |
| 31 | auto button1 = new QPushButton("Switch to this window"); |
| 32 | configureMainWindow(&w1, mdiArea1, button1); |
| 33 | w1.setGeometry(0, 0, 800, 800); |
| 34 | w1.show(); |
| 35 | |
| 36 | QMainWindow w2; |
| 37 | w2.winId(); |
| 38 | const int secondScreenIdx = QGuiApplication::screens().size() > 1 ? 1 : 0; |
| 39 | w2.windowHandle()->setScreen(QGuiApplication::screens().at(secondScreenIdx)); |
| 40 | auto mdiArea2 = new QMdiArea; |
| 41 | auto button2 = new QPushButton("Switch to this window"); |
| 42 | configureMainWindow(&w2, mdiArea2, button2); |
| 43 | w2.setGeometry(0, 0, 800, 800); |
| 44 | w2.show(); |
| 45 | |
| 46 | QMdiSubWindow* subWindow = new QMdiSubWindow(); |
| 47 | |
| 48 | QQuickWidget *quickWidget = new QQuickWidget(); |
| 49 | quickWidget->resize(QSize(400, 400)); |
| 50 | quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView); |
| 51 | quickWidget->setSource(QUrl("qrc:/main.qml")); |
| 52 | |
| 53 | subWindow->setWidget(quickWidget); |
| 54 | |
| 55 | QObject::connect(button1, &QPushButton::clicked, |
| 56 | &app, [mdiArea1, mdiArea2, subWindow, button1, button2]() { |
| 57 | mdiArea2->removeSubWindow(subWindow); |
| 58 | mdiArea1->addSubWindow(subWindow); |
| 59 | subWindow->show(); |
| 60 | button1->setEnabled(false); |
| 61 | button2->setEnabled(true); |
| 62 | }); |
| 63 | |
| 64 | QObject::connect(button2, &QPushButton::clicked, |
| 65 | &app, [mdiArea1, mdiArea2, subWindow, button1, button2]() { |
| 66 | mdiArea1->removeSubWindow(subWindow); |
| 67 | mdiArea2->addSubWindow(subWindow); |
| 68 | subWindow->show(); |
| 69 | button1->setEnabled(true); |
| 70 | button2->setEnabled(false); |
| 71 | }); |
| 72 | |
| 73 | mdiArea2->addSubWindow(subWindow); |
| 74 | button2->setEnabled(false); |
| 75 | subWindow->show(); |
| 76 | |
| 77 | return app.exec(); |
| 78 | } |