Here you have an example:
main.cpp
interesting part of main function:
QObject *QMLmainWindow = engine.rootObjects()[0];
QQuickWindow *QMLsecondWindow = QMLmainWindow->findChild<QQuickWindow*>("secondWindow");
QQmlComponent *qml = new QQmlComponent(&engine, QUrl(QStringLiteral("qrc:/ThirdWindow.qml")));
QQuickWindow *QMLthirdWindow = qobject_cast<QQuickWindow*>(qml->create());
QList<QScreen*> screens = app.screens();
if (screens.count() > 1) {
QRect secScreenGeometry = screens.at(1)->geometry();
QMLsecondWindow->setProperty("visible", true);
QMLsecondWindow->setX(secScreenGeometry.x());
QMLsecondWindow->setY(secScreenGeometry.y());
QMLsecondWindow->resize(secScreenGeometry.width(), secScreenGeometry.height());
QMLthirdWindow->setScreen(screens.at(1));
QMLthirdWindow->setProperty("visible", true);
}
return app.exec();
and QML files with definitions of windows:
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
SecondWindow {
id: secondWindow
objectName: "secondWindow"
}
}
SecondWindow.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: false
width: 640
height: 480
title: qsTr("Second Window")
}
ThirdWindow.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: false
width: 640
height: 480
title: qsTr("Third Window")
}
QQuickWindow has an inherited method setScreen, but this method works only for top level windows. As you can see, QMLthirdWindow is created as indpendent window and we're allowed to use setScreen method.
But if we want to move QMLsecondWindow, we need to do this "manually". So we need to read geometry of proper screen and set X, Y parameters of window for those placed on destination screen. In the result of above example "Main Window" will be displayed centered at main screen (0), "Second Window" will be displayed at second screen (1) with maximum size, and "Third Window" will be displayed centered at second screen (1).