In my previous articles, I described how to use u1db and SQLite offline storage APIs to store some application statuses. In this article, I will introduce how to use QT. Labs. settings to store the application status. For more details, see the link.
First, create a simple "app with simple UI" template application and modify the file "Main. qml" as follows:
import QtQuick 2.0import Ubuntu.Components 1.1import Qt.labs.settings 1.0/*! \brief MainView with a Label and Button elements.*/MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "com.ubuntu.developer.liu-xiao-guo.settings" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false width: units.gu(50) height: units.gu(75) Page { title: i18n.tr("Simple") Column { anchors.fill: parent anchors.centerIn: parent anchors.horizontalCenter: parent.center Label { text: "Please input a string below:" fontSize: "large" } TextField { id: myTextField text: settings.input placeholderText: "please input a string" onTextChanged: { settings.input = text } } Button { text: "Get category" onClicked: { console.log("settings category:" + settings.category); } } } Settings { id: settings property string input: "unknown" } Component.onDestruction: { settings.input = myTextField.text } }}
<Span style = "font-family: Arial, Helvetica, sans-serif; Background-color: RGB (255,255,255 ); "> remember to add" </span> <span style = "font-family: Arial, Helvetica, sans-serif; Background-color: RGB (255,255,255 ); "> QT. labs. settings </span> <span style = "font-family: Arial, Helvetica, sans-serif; Background-color: RGB (255,255,255);"> ". We first bind the value of mytextfield to the input in settings. When the program exits, we store it as follows: </span>
Component.onDestruction: { settings.input = myTextField.text }
In our application, we use the following method. Every time mytextfield changes, we save it. This depends on what our final program needs.
TextField { id: myTextField text: settings.input placeholderText: "please input a string" onTextChanged: { settings.input = text } }
Run our application and we will find that when we modify the value in mytextfield and exit. At the next startup, we can see that the value entered last time is read and stored in mytextfield.
The source code for the entire test is in bzr branch LP :~ Liu-Xiao-guo/debiantrial/settingsqml
Use QT. Labs. settings to store application settings