Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-54552

Settings (Qt.labs.settings) cannot be used inside Component

XMLWordPrintable

    • qtquickcontrols2: 05933f155e0f0e90fb609839688e01b05d8e80ae

      Unfortunately Settings (Qt.labs.settings) cannot be used inside the component.
      Real problems happen when you try to use Settings inside the qml document which is instantiated inside Component. Take a look at small example:

      main.qml
      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import Qt.labs.settings 1.0
      
      ApplicationWindow {
        visible: true
      
        StackView {
          id: stackView
          anchors.fill: parent
          initialItem: thePage
        }
      
        Component {
          id: thePage
          Page {
            Settings {
              id: settings
              property int theNumber: -1
            }
            Component.onDestruction: {
              settings.theNumber = 42
              console.log("onDestruction")
            }
          }
        }
      }
      

      Configuration file will never contain "42" as "theNumber" value, it is always "-1". The "onDestruction" handler is called, but settings are not saved by some reason. If you remove configuration file while program is running, this configuration file is not recreated when program finishes.

      Obviously, moving Settings outside the Component fixes the problem. But in real use-case the Component is far away from the main.qml files and it would be nice to declare its own settings close to the implementation.

        For Gerrit Dashboard: QTBUG-54552
        # Subject Branch Project Status CR V

            Unassigned Unassigned
            prokher Alexander A. Prokhorov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes