zoukankan      html  css  js  c++  java
  • 使用Qt.labs.settings来存储应用的设置

    我在曾经的文章中,讲述了怎样使用U1dbSQLite offline storage API来存储应用的一些状态。在这篇文章中,我将介绍怎样使用Qt.labs.settings来存储应用的状态。更加具体的介绍,请參阅链接


    首先。我们创建一个最简单的“App with Simple UI”模版应用。并改动文件“main.qml”例如以下:

    import QtQuick 2.0
    import Ubuntu.Components 1.1
    import Qt.labs.settings 1.0
    
    /*!
        rief 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
    
         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
            }
        }
    }
    

    记得这里我们一定要增加Qt.labs.settings。

    我们首先绑定myTextField的值为settings中的input。在程序退出的时候,我们通过例如以下的方式进行存储:


            Component.onDestruction: {
                settings.input = myTextField.text
            }

    在我们的应用中。我们使用例如以下的方法。每当myTextField变化时,我们就存一下。这依赖于我们终于程序的需求是什么样的。

                TextField {
                    id: myTextField
                    text: settings.input
                    placeholderText: "please input a string"
    
                    onTextChanged: {
                        settings.input = text
                    }
                }

    执行我们的应用。我们会发现,当我们改动myTextField中的值,并退出后。下次启动时。能够看到。上次输入的值被读取,并存放于myTextField中。



    整个測试的源代码在 bzr branch lp:~liu-xiao-guo/debiantrial/settingsqml

  • 相关阅读:
    【转】Android系统中Fastboot和Recovery所扮演的角色。
    【转】Android ROM分析(1):刷机原理及方法
    【转】ANDROIDROM制作(一)——ROM结构介绍、精简和内置、一般刷机过程
    【转】使用fastboot命令刷机流程详解
    检测是否安装或者开启flash
    CentOS中/英文环境切换教程(CentOS6.8)
    id: cannot find name for user ID xxx处理办法
    linux重命名所有find查找到的文件/文件夹
    linux过滤旧文件中的空行和注释行剩余内容组成新文件
    CentOS和AIX查看系统序列号
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6932022.html
Copyright © 2011-2022 走看看