Dibo
Dibo

Reputation: 1189

Settings and default ApplicationWindow position

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

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

Upvotes: 0

Views: 45

Answers (0)

Related Questions