How to control which Screen a Window is shown in from QML

Viewed 6394

My application has a main window with a button, and when I click the button I use createComponent to create a subclass of Window {} and show it (purely in QML). I am running the application on my macbook with another monitor attached.

If I do not attempt to set the .x or .y properties of my new window, then it is shown on top of my main window, whether the main window is on my macbook's screen or on the attached monitor (i.e. the new window is always shown on the same screen as the main window). However, if I do set the .x or .y properties of the new window (to any value at all), then the new window is always shown on the macbook screen, regardless of which screen I have my main window on.

How can I control which available screen my new Window is shown in? And related, how can I exactly control the positioning of the new window in the screen (for example, how can I have the new window always appear in the lower-right corner)?

Edit: basic 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
}

In my main window, I have this function (remoteControl is a property that keeps a reference to the remote window):

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

Also on my main window I have a button, and in its onClicked: event I have this code:

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
    }
}

If I comment out the setting of .x and .y in the showRemoteWindow function, then my RemoteWindow always appears in the same screen as my main window (either the macbook screen or the attached monitor). But if I leave those two lines uncommented (or make any other attempt to set the x or y position of the window), then my RemoteWindow always appears on the macbook screen, regardless of which screen my main window is in.

1 Answers
Related