How to control which screen displays a window from QML

My application has a main window with a button, and when I click the button, I use createComponentto create a subclass Window {}and show it (only in QML). I am running the application on my macbook with a different monitor connected.

If I am not trying to set properties .xor .ymy new window, then it appears on top of my main window, regardless of whether the main window is on my macbook screen or on a connected monitor (i.e. a new window is always displayed on the same screen as and the main window). However, if I set properties .xor a .ynew window (for any value whatsoever), then the new window always appears on the macbook screen, regardless of which screen I have the main window on.

How can I control which available screen is displayed in my new window? And related, how can I precisely control the positioning of a new window on the screen (for example, how can I open a new window in the lower right corner)?

Edit: base code. RemoteWindow.qml

Window {
    id: myWindow
    flags: Qt.Window | Qt.WindowTitleHint | Qt.WindowStaysOnTopHint 
        | Qt.WindowCloseButtonHint
    modality: Qt.NonModal
    height: 500
    width: 350

    // window contents, doesn't matter
}

(remoteControl - , ):

function showRemoteWindow() {
    remoteControl.x = Screen.width - remoteControl.width
    remoteControl.y = Screen.height - remoteControl.height
    remoteControl.show()
}

, onClicked: :

if (remoteControl) {
    showRemoteWindow()
} else {
    var component = Qt.createComponent("RemoteWindow.qml")
    if (component.status === Component.Ready) {
        remoteControl = component.createObject(parent)
        showRemoteWindow() // window appears even without this call,
            // but calling this method to also set the initial position
    }
}

.x .y showRemoteWindow, RemoteWindow , ( macbook, ). ( x y ), RemoteWindow macbook, , .

+6
1

@Blabdouze, Qt 5.9 screen Window. Qt.application.screens.

, :

import QtQuick.Window 2.3 // the 2.3 is necessary

Window {
    //...
    screen: Qt.application.screens[0]
}

. , x y screen. , :

Window {
    //...
    screen: Qt.application.screens[0] //assigning the window to the screen is not needed, but it makes the x and y binding more readable
    x: screen.virtualX
    y: screen.virtualY + screen.height - height
}

Qt 5.9, ++ :

QList<QObject*> screens;
for (QScreen* screen : QGuiApplication::screens())
    screens.append(screen);
engine.rootContext()->setContextProperty("screens", QVariant::fromValue(screens));

geometry/virtualGeometry virtualX/virtualY:

x: screens[0].geometry.x
+4

All Articles