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. Settings and default ApplicationWindow position
Forum Updated to NodeBB v4.3 + New Features

Settings and default ApplicationWindow position

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 614 Views 2 Watching
  • 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.
  • K Offline
    K Offline
    Kobid
    wrote on last edited by
    #1

    Hi,
    When user first time run my app, I would like to center main window on the screen by default. When he run it again, I want to restore his last position. I have this code:

    ApplicationWindow {
        id: mainWindow
        width: 640
        height: 480
        x: settings.x
        y: settings.y
        Settings {
            id: settings
            category: "MainWindow"
            property int x: Screen.width / 2 - mainWindow.width / 2
            property int y: Screen.height / 2 - mainWindow.height / 2
            property alias width: mainWindow.width
            property alias height: mainWindow.height
        }
    
        Component.onDestruction: {
            settings.x = mainWindow.x
            settings.y = mainWindow.y
        }
    }
    

    width and height are restored properly but x and y still have screen center position even if in INI file I see changed values. Tried also with onCompleted but the same issue:

    ApplicationWindow {
        id: mainWindow
        width: 640
        height: 480
        Component.onCompleted: {
            x = settings.x
            y = settings.y
        }
        Settings {
            id: settings
            category: "MainWindow"
            property int x: Screen.width / 2 - mainWindow.width / 2
            property int y: Screen.height / 2 - mainWindow.height / 2
            property alias width: mainWindow.width
            property alias height: mainWindow.height
        }
    
        Component.onDestruction: {
            settings.x = mainWindow.x
            settings.y = mainWindow.y
        }
    }
    
    1 Reply Last reply
    0
    • Axel SpoerlA Axel Spoerl

      Have you added logging to the slots? What does it say?

      K Offline
      K Offline
      Kobid
      wrote on last edited by Kobid
      #6

      @Axel-Spoerl said in Settings and default ApplicationWindow position:

      Have you added logging to the slots? What does it say?

      MainWindow's onCompleted is called before Settings onCompleted. I temporary solved this issue by this:

      Settings {
              id: settings
              category: "MainWindow"
              property int x
              property int y
              property alias width: mainWindow.width
              property alias height: mainWindow.height
              Component.onCompleted: {
                  console.log("Settings " + x + " " + y)
                  mainWindow.x = settings.value("x", Screen.width / 2 - mainWindow.width / 2)
                  mainWindow.y = settings.value("y", Screen.height / 2 - mainWindow.height / 2)
              }
          }
      
          Component.onDestruction: {
              settings.x = mainWindow.x
              settings.y = mainWindow.y
          }
      
      Axel SpoerlA 1 Reply Last reply
      0
      • Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        Why are width and height property aliases, but not x and y?
        It looks a bit complicated to detour default positioning via a settings object.

        My guess is that it doesn't work because of signal timing.
        You can find that out by adding a console.log(x, y) in the destruction and completed slots.

        Does this simplification work?

        ApplicationWindow {
            id: mainWindow
            width: 640
            height: 480
            x: Screen.width / 2 - width / 2
            y: Screen.height / 2 - height / 2
            
            Settings {
                id: settings
                category: "MainWindow"
                property alias x: mainWindow.x 
                property alias y: mainWindow.y
                property alias width: mainWindow.width
                property alias height: mainWindow.height
            }
        }
        

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kobid
          wrote on last edited by
          #3

          This doesn't work neither :(

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kobid
            wrote on last edited by
            #4

            I'm wondering why this official Qt example work. My case is the same no?
            https://doc.qt.io/qt-6/qml-qtcore-settings.html

            import QtCore
            import QtQuick
            
            Item {
                id: page
            
                state: settings.state
            
                states: [
                    State {
                        name: "active"
                        // ...
                    },
                    State {
                        name: "inactive"
                        // ...
                    }
                ]
            
                Settings {
                    id: settings
                    property string state: "active"
                }
            
                Component.onDestruction: {
                    settings.state = page.state
                }
            }
            
            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #5

              Have you added logging to the slots? What does it say?

              Software Engineer
              The Qt Company, Oslo

              K 1 Reply Last reply
              0
              • Axel SpoerlA Axel Spoerl

                Have you added logging to the slots? What does it say?

                K Offline
                K Offline
                Kobid
                wrote on last edited by Kobid
                #6

                @Axel-Spoerl said in Settings and default ApplicationWindow position:

                Have you added logging to the slots? What does it say?

                MainWindow's onCompleted is called before Settings onCompleted. I temporary solved this issue by this:

                Settings {
                        id: settings
                        category: "MainWindow"
                        property int x
                        property int y
                        property alias width: mainWindow.width
                        property alias height: mainWindow.height
                        Component.onCompleted: {
                            console.log("Settings " + x + " " + y)
                            mainWindow.x = settings.value("x", Screen.width / 2 - mainWindow.width / 2)
                            mainWindow.y = settings.value("y", Screen.height / 2 - mainWindow.height / 2)
                        }
                    }
                
                    Component.onDestruction: {
                        settings.x = mainWindow.x
                        settings.y = mainWindow.y
                    }
                
                Axel SpoerlA 1 Reply Last reply
                0
                • K Kobid has marked this topic as solved on
                • K Kobid

                  @Axel-Spoerl said in Settings and default ApplicationWindow position:

                  Have you added logging to the slots? What does it say?

                  MainWindow's onCompleted is called before Settings onCompleted. I temporary solved this issue by this:

                  Settings {
                          id: settings
                          category: "MainWindow"
                          property int x
                          property int y
                          property alias width: mainWindow.width
                          property alias height: mainWindow.height
                          Component.onCompleted: {
                              console.log("Settings " + x + " " + y)
                              mainWindow.x = settings.value("x", Screen.width / 2 - mainWindow.width / 2)
                              mainWindow.y = settings.value("y", Screen.height / 2 - mainWindow.height / 2)
                          }
                      }
                  
                      Component.onDestruction: {
                          settings.x = mainWindow.x
                          settings.y = mainWindow.y
                      }
                  
                  Axel SpoerlA Offline
                  Axel SpoerlA Offline
                  Axel Spoerl
                  Moderators
                  wrote on last edited by
                  #7

                  @Kobid said in Settings and default ApplicationWindow position:

                  MainWindow's onCompleted i called before Settings onCompleted. I temporary solved this issue by this:

                  That makes sense, I overlooked the scope of Settings.
                  It's unusual to make a settings object a child of a screen asset. I would actually place the settings object as a sibling of mainWindow assign the application name to category. That should solve your problem.

                  Software Engineer
                  The Qt Company, Oslo

                  1 Reply Last reply
                  0

                  • Login

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