Settings
Qt comes with a Settings element for loading and storing settings. The is still in the lab’s module, which means the API may break in the future. So be aware.
Here is a small example which applies a color value to a base rectangle. Every time the user clicks on the window a new random color is generated. When the application is closed and relaunched again you should see your last color. The default color should be the color initially set on the root rectangle.
import QtQuick
import Qt.labs.settings 1.0
Rectangle {
id: root
width: 320
height: 240
color: '#fff' // default color
Settings {
property alias color: root.color
}
MouseArea {
anchors.fill: parent
// random color
onClicked: root.color = Qt.hsla(Math.random(), 0.5, 0.5, 1.0);
}
}The settings value are stored every time the value changes. This might be not always what you want. To store the settings only when required you can use standard properties combined with a function that alters the setting when called.
It is also possible to group settings into different categories using the category property.
The settings are stored according to your application name, organization, and domain. This information is normally set in the main function of your C++ code.
If you are writing a pure QML application, you can set the same attributed using the global properties Qt.application.name, Qt.application.organization, and Qt.application.domain.
Last updated