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. QtObject singleton and Settings
QtWS25 Last Chance

QtObject singleton and Settings

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 276 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.
  • B Offline
    B Offline
    Bob64
    wrote on last edited by Bob64
    #1

    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 existing Settings 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 the Settings, 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 new ApplicationWindow property.

    mzimmersM 1 Reply Last reply
    0
    • B Bob64

      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 existing Settings 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 the Settings, 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 new ApplicationWindow property.

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      @Bob64 did you put your GlobalSettings.qml in your CMakeLists.txt file?

      set_source_files_properties(
          GlobalSettings.qml
          PROPERTIES QT_QML_SINGLETON_TYPE TRUE
      
      B 1 Reply Last reply
      0
      • mzimmersM mzimmers

        @Bob64 did you put your GlobalSettings.qml in your CMakeLists.txt file?

        set_source_files_properties(
            GlobalSettings.qml
            PROPERTIES QT_QML_SINGLETON_TYPE TRUE
        
        B Offline
        B Offline
        Bob64
        wrote on last edited by Bob64
        #3

        @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 the GlobalSettings QtObject via a property alias in my Settings 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 singleton QtObject 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 via Settings. The standard way to do use Settings is to add a property to it which is a property alias to the actual value you want to persist. The reason it is not working for me is that the property alias to the QtObject property causes a run time error that aborts the application.

        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