Settings and default ApplicationWindow position
-
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
andheight
are restored properly butx
andy
still have screen center position even if in INI file I see changed values. Tried also withonCompleted
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 } }
-
Have you added logging to the slots? What does it say?
@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 SettingsonCompleted
. 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 }
-
Why are
width
andheight
property aliases, but notx
andy
?
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 aconsole.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 } }
-
I'm wondering why this official Qt example work. My case is the same no?
https://doc.qt.io/qt-6/qml-qtcore-settings.htmlimport 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 } }
-
Have you added logging to the slots? What does it say?
-
Have you added logging to the slots? What does it say?
@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 SettingsonCompleted
. 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-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 SettingsonCompleted
. 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 }
@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 ofmainWindow
assign the application name tocategory
. That should solve your problem.