如何在Ubuntu手机应用中得到所有的环境变量值

185 篇文章 6 订阅
137 篇文章 1 订阅

我们在先前的例程中已经通过一些方法得到我们应用的一些环境变量值。这些值有的非常有用,比如我们可以得到我们应用所只能访问的目录。在今天的例程中,我们来展示一种方法可以得到应用所有的环境变量。在我们的实际应用中,我们可以通过这些环境变量来做一些事情。另外,在这个例程中,我们也展示了如何在Qt C++的代码中构造我们的ListView中的model。我们在先前的例程“Ubuntu OS应用Runtime Enviroment”已经展示了和我们平台安全相关的一些环境变量。


首先,我们来展示我们所使用的model dataobject:


dataobject.h


#ifndef DATAOBJECT_H
#define DATAOBJECT_H

#include <QObject>

class DataObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString key READ key WRITE setKey NOTIFY keyChanged)
    Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)

public:
    DataObject(QObject *parent=0);
    DataObject(const QString &key, const QString &value, QObject *parent=0);

    QString key() const;
    void setKey(const QString &key);

    QString value() const;
    void setValue(const QString &value);

signals:
    void keyChanged();
    void valueChanged();

private:
    QString m_key;
    QString m_value;
};

#endif // DATAOBJECT_H


dataobject.cpp


#include <QDebug>
#include "dataobject.h"

DataObject::DataObject(QObject *parent)
    : QObject(parent)
{
}

DataObject::DataObject(const QString &key, const QString &value, QObject *parent)
    : QObject(parent), m_key(key), m_value(value)
{
}

QString DataObject::key() const
{
    return m_key;
}

void DataObject::setKey(const QString &key)
{
    if (key != m_key) {
        m_key = key;
        emit keyChanged();
    }
}

QString DataObject::value() const
{
    return m_value;
}

void DataObject::setValue(const QString &value)
{
    if (value != m_value) {
        m_value = value;
        emit valueChanged();
    }
}


这个model dataobject是为了我们能够在ListView中显示我们的key及value。


main.cpp


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QProcessEnvironment>
#include <QQmlContext>
#include <QDebug>

#include "dataobject.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // Try to get all of the env variables here
    QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();

    QStringList keys = environment.keys();

    QList<QObject*> dataList;

    foreach (QString key, keys) {
        qDebug() << "key: " << key;

        QString value = environment.value(key);
        qDebug() << "value: " << value;

        dataList.append(new DataObject(key, value));
    }

    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:///Main.qml")));
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("varModel", QVariant::fromValue(dataList));

    view.show();
    return app.exec();
}


在上面,我们通过QProcessEnvironment来得到我们所有的环境变量,并把它们装在我们的datalist变量中。这个datalist将在我们的QML中以varModel的形式访问。

Main.qml


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

/*!
    \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: "runtime.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)

    Page {
        title: i18n.tr("Run time variables")

        Column {
            anchors.fill: parent

            ListItem.Base {
                id: header
                height: ubuntuLabel.height + runtime.height + units.gu(6)

                Column {
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.centerIn: parent
                    spacing: units.gu(2)
                    Label {
                        id: ubuntuLabel
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: ""
                        fontSize: "x-large"
                    }
                    Label {
                        id: runtime
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: "Runtime Environment"
                    }
                }
            }

            ListView {
                height: parent.height - header.height
                width: parent.width
                model: varModel
                clip: true

                delegate:  ListItem.Subtitled {
                    text: key
                    subText: value
                }
            }

        }
    }
}


这里的显示非常简单。我们直接使用一个ListView来显示我们的数据。

运行我们的应用:

     



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值