User7723337
User7723337

Reputation: 12018

QT Quick C++ Application with multiple screens

I am new to QT Quick and QML and I am trying to build a Linux Desktop multi screen application using C++ and QT. I have seen the examples provided by QT but most of the examples are single screen and not explaining how to have multiple screens and how the screen stack is maintained and how screens communicate with each other and what are the ways available.

An example for multi screen QT Quick C++ application will help.

Upvotes: 1

Views: 3602

Answers (2)

DaszuOne
DaszuOne

Reputation: 809

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).

Upvotes: 1

folibis
folibis

Reputation: 12854

Since we have Qt.application.screens I would use pure QML for that, for example:

Item {
    id: root
    visible: true

    Component.onCompleted: {
        for(var i = 0;i < Qt.application.screens.length;i ++)
        {
            wnd.createObject(root, {wndIndex: i});
        }
    }

    Component {
        id: wnd
        Window {
            property int wndIndex: 0
            visible: true
            width: 640
            height: 480
            title: qsTr("Screen " + wndIndex)
            screen: Qt.application.screens[wndIndex]
        }
    }
}

The only thing in this case that we need to use QQuickView instead of QQmlApplicationEngine:

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQuickView *view = new QQuickView;
    view->setSource(QUrl("qrc:/main.qml"));
    view->show();    
    return app.exec();
}

Upvotes: 0

Related Questions