Use QT. Labs. settings to store application settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.