Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Save window position in Settings - handle multiple monitors

    QML and Qt Quick
    2
    6
    3274
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dagheyman last edited by

      This example suggests saving the position of the window in settings, and I have seen it being used a lot. This seems to work fine in most cases. But on Windows with multiple monitors, if the app is closed on the secondary monitor, and then restarted with only the primary monitor, the application seems to spawn outside the the visible space.

      What is the recommended way to avoid this behaviour on Windows?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        A quick idea: store also the monitor then load and apply it it first then apply the position settings.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • D
          dagheyman last edited by

          Thanks for the suggestion. Do you think this would be possible in QML or does it need to be done in the C++ code?

          1 Reply Last reply Reply Quote 0
          • D
            dagheyman last edited by

            Related question on Stack Overflow: http://stackoverflow.com/questions/41253624/how-to-properly-save-window-state-in-qml

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              You'll likely have to do that by hand. AFAIK, there's no equivalent to the QMainWindow state in the ApplicationWindow class.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • D
                dagheyman last edited by dagheyman

                I ended up saving the saving the available desktop layout on destruction using Screen and Settings, then when the application was re-spawned, moving the window to the middle if the layout wasn't the same anymore.

                Full code example:

                import QtQuick 2.5
                import QtQuick.Layouts 1.1
                import QtQuick.Controls 1.4
                import Qt.labs.settings 1.0
                import QtQuick.Window 2.2
                
                ApplicationWindow {
                    id: window
                    Component.onCompleted: ensureValidWindowPosition()
                    Component.onDestruction: saveScreenLayout()
                
                    Settings {
                        id: settings
                
                        property alias x: window.x
                        property alias y: window.y
                
                        property var desktopAvailableWidth
                        property var desktopAvailableHeight
                    }
                
                    function saveScreenLayout() {
                        settings.desktopAvailableWidth = Screen.desktopAvailableWidth
                        settings.desktopAvailableHeight = Screen.desktopAvailableHeight
                    }
                
                    function ensureValidWindowPosition() {
                        var savedScreenLayout = (settings.desktopAvailableWidth === Screen.desktopAvailableWidth)
                                && (settings.desktopAvailableHeight === Screen.desktopAvailableHeight)
                        window.x = (savedScreenLayout) ? settings.x : Screen.width / 2 - window.width / 2
                        window.y = (savedScreenLayout) ? settings.y : Screen.height / 2 - window.height / 2
                    }
                }
                
                1 Reply Last reply Reply Quote 1
                • First post
                  Last post