Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Save window position in Settings - handle multiple monitors
QtWS25 Last Chance

Save window position in Settings - handle multiple monitors

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 4.1k Views
  • 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 Offline
    D Offline
    dagheyman
    wrote on 8 May 2017, 07:08 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 May 2017, 21:30 last edited by
      #2

      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
      0
      • D Offline
        D Offline
        dagheyman
        wrote on 11 May 2017, 08:18 last edited by
        #3

        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
        0
        • D Offline
          D Offline
          dagheyman
          wrote on 11 May 2017, 08:46 last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 11 May 2017, 09:23 last edited by
            #5

            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
            0
            • D Offline
              D Offline
              dagheyman
              wrote on 11 May 2017, 13:39 last edited by dagheyman 5 Dec 2017, 07:41
              #6

              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
              1

              4/6

              11 May 2017, 08:46

              • Login

              • Login or register to search.
              4 out of 6
              • First post
                4/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved