利用Qt Global Object来获取一些关于应用的信息

185 篇文章 6 订阅
159 篇文章 2 订阅

在Qt中,我们可以利用Qt全局变量来获取一些对我们应用有用的信息。在下面的应用中,我们可以获取如下的信息:

  


在上面,我们可以看到应用的状态,运行的输入参数,应用的名称及操作系统等。


我们的应用设计非常简单:


import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 1.0 as ListItems

/*!
    \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: "application.liu-xiao-guo"

    /*
     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(60)
    height: units.gu(85)

    property string locale: ""

    Page {
        title: i18n.tr("application")

        Flickable {
            anchors.fill: parent
            contentHeight: content.childrenRect.height

            Column {
                id: content
                anchors.fill: parent
                spacing: units.gu(0.5)

                ListItems.SingleValue {
                    text: "Active"
                    value: Qt.application.state == Qt.ApplicationActive
                }

                ListItems.SingleValue {
                    text: "State"
                    value: {
                        switch(Qt.application.state) {
                        case Qt.ApplicationActive:
                            return "Active";
                        case Qt.ApplicationInactive:
                            return "Inactive";
                        case Qt.ApplicationSuspended:
                            return "Suspended";
                        case Qt.ApplicationHidden:
                            return "Hidden";
                        default:
                            return "Unknown";
                        }
                    }
                }

                ListItems.SingleValue {
                    text: "Layout direction"
                    value: {
                        switch(Qt.application.layoutDirection) {
                        case Qt.LeftToRight:
                            return "Left to right";
                        case Qt.RightToLeft:
                            return "Right to left";
                        default:
                            return "Unknown";
                        }
                    }
                }

                ListItems.Subtitled {
                    text: "aruguments"
                    subText: {
                        console.log("arguments: " + Qt.application.arguments);
                        var arguments = Qt.application.arguments;
                        var content = arguments.join(" ");
                        return content;
                    }
                }

                ListItems.SingleValue {
                    text: "name"
                    value: Qt.application.name
                }

                ListItems.SingleValue {
                    text: "domain"
                    value: {
                        console.log("version: " + Qt.application.version);
                        return Qt.application.domain;
                    }
                }

                ListItems.SingleValue {
                    text: "support multiple windows"
                    value: Qt.application.supportsMultipleWindows
                }

                ListItems.SingleValue {
                    text: "OS"
                    value: Qt.platform.os
                }

                ListItems.SingleValue {
                    text: "Locale"
                    value: locale
                }
            }
        }

        Component.onCompleted: {
            var keys = Object.keys(Qt.application);
            for(var i = 0; i < keys.length; i++) {
                var key = keys[i];
                // prints all properties, signals, functions from object
                console.log(key + ' : ' + Qt.application[key]);
            }

            locale = Qt.inputMethod["locale"].nativeLanguageName;
            console.log("locale: " + locale);
        }
    }
}


整个应用的源码在:https://github.com/liu-xiao-guo/application

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值