QtObject singleton and Settings
-
I have some application wide settings stored in a
QtObject
singleton object. I wanted to expose one of these as a persistent setting by extending an existingSettings
object that I already use for main window size and position.GlobalSettings.qml in root qml directory:
pragma Singleton QtObject { property bool mySetting: false ... }
main.qml in same directory:
... import "." ApplicationWindow { ... Settings { ... property alias mySetting: GlobalSettings.mySetting } }
The application fails at startup with an error message:
Invalid alias reference. Unable to find id "GlobalSettings"
Is the issue simply that it is not possible to alias a singleton property like this?
Is there an obvious way around this? I could introduce a boolean property in the
ApplicationWindow
perhaps and alias that in theSettings
, but the issue is that the boolean in question can be adjusted from a few places (menus and suchlike) and this is currently handled quite cleanly via the singleton. It would be awkward to route everything via a newApplicationWindow
property. -
I have some application wide settings stored in a
QtObject
singleton object. I wanted to expose one of these as a persistent setting by extending an existingSettings
object that I already use for main window size and position.GlobalSettings.qml in root qml directory:
pragma Singleton QtObject { property bool mySetting: false ... }
main.qml in same directory:
... import "." ApplicationWindow { ... Settings { ... property alias mySetting: GlobalSettings.mySetting } }
The application fails at startup with an error message:
Invalid alias reference. Unable to find id "GlobalSettings"
Is the issue simply that it is not possible to alias a singleton property like this?
Is there an obvious way around this? I could introduce a boolean property in the
ApplicationWindow
perhaps and alias that in theSettings
, but the issue is that the boolean in question can be adjusted from a few places (menus and suchlike) and this is currently handled quite cleanly via the singleton. It would be awkward to route everything via a newApplicationWindow
property. -
@Bob64 did you put your GlobalSettings.qml in your CMakeLists.txt file?
set_source_files_properties( GlobalSettings.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
@mzimmers I am using qmake. Note that there is no problem in general in using my
GlobalSettings
object - it has been in use for a couple of years. The problem is trying to persist one particular property in theGlobalSettings
QtObject
via a property alias in mySettings
object.Sorry I realise I should have chosen better names here. Just to be totally clear,
Settings
is a built in QML type that can be used to implement persistence of various application properties such as window sizes and so on.GlobalSettings
is the singletonQtObject
that is specific to my application and contains various attributes that are queried throughout the application.The issue is that I have one specific property in
GlobalSettings
that I also want to persist viaSettings
. The standard way to do useSettings
is to add a property to it which is aproperty alias
to the actual value you want to persist. The reason it is not working for me is that theproperty alias
to theQtObject
property causes a run time error that aborts the application.