We have used some of the methods in our previous routines to get some of the environment variable values we applied. Some of these values are useful, for example, we can get directories that our app can only access. In today's routines, let's show a way to get all the environment variables applied. In our practical application, we can do something with these environment variables. In addition, in this routine, we also show how to construct the model in our ListView in QT C + + code. Our previous routines, "Ubuntu OS Application Runtime enviroment", have shown some environment variables related to our platform security.
First, let's show the model DataObject we're using:
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 Noti FY 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 ();p rivate: QString m_key; QString m_value;}; #endif//Dataobject_h
Dataobject.cpp
#include <QDebug> #include "dataobject.h" DataObject::D Ataobject (Qobject *parent) : Qobject (parent) {} DataObject::D ataobject (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 (); }}
This model DataObject is for us to be able to display our key and value in the ListView.
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 ();}
Above, we use qprocessenvironment to get all of our environment variables and put them in our DataList variables. This DataList will be accessed in the form of Varmodel in our qml.
Main.qml
Import QtQuick 2.0import ubuntu.components 1.1import 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"/* The 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 (height:units.gu) page {title:i18n.tr ("Run Time Varia Bles ") 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.Subtitle d {text:key Subtext:value}}}}
The display here is very simple. We use a ListView directly to display our data.
To run our application:
All the source code in: Git clone https://gitcafe.com/ubuntu/runtime.git
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to get all the environment variable values in the Ubuntu mobile app