In this article, we show you how to get the IP address on your phone in the Ubuntu qml app.
We add some code to our main.cpp to get the IP address:
Main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> #include < Qnetworkinterface> #include <qqmlcontext>int main (int argc, char *argv[]) { qguiapplication app (argc, argv ); qlist<qhostaddress> list = Qnetworkinterface::alladdresses (); Qstringlist DataList; for (int niter = 0; Niter < List.count (); niter++) { qdebug () << list[niter].tostring (); Datalist.append (list[niter].tostring ()); } Qquickview view; View.setsource (Qurl (qstringliteral ("qrc:///main.qml")); View.setresizemode (Qquickview::sizerootobjecttoview); Qqmlcontext *ctxt = View.rootcontext (); Ctxt->setcontextproperty ("MyModel", Qvariant::fromvalue (DataList)); View.show (); return app.exec ();}
Main.qml
Import QtQuick 2.0import ubuntu.components 1.1/*! \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: "Ipaddress.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 ("IPAddress") Column {spacing:units.gu (2) Anchors {margins:units.gu (2) Fill : parent} label {Id:label objectName: "Label" Fontsi Ze: "Large" text:i18n.tr ("IP addresses:") } ListView {Width:parent.width height:parent.height-label.height Model:mymodel Delegate:text {Text:modeldata} } } }}
To run our application:
This is the IP address that we display when using WiFi.
Our source code is: Git clone https://gitcafe.com/ubuntu/ipaddress.git
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to get the IP address on your Ubuntu phone