如何得到Ubuntu手機上的IP地址

來源:互聯網
上載者:User

標籤:

在這篇文章中,我們介紹如何在Ubuntu QML應用中得到手機上的IP地址。


我們在我們的main.cpp中加入一些代碼來得到IP地址:


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"    /*     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("ipaddress")        Column {            spacing: units.gu(2)            anchors {                margins: units.gu(2)                fill: parent            }            Label {                id: label                objectName: "label"                fontSize: "large"                text: i18n.tr("IP addresses: ")            }            ListView {                width: parent.width                height: parent.height - label.height                model: myModel                delegate: Text {                    text: modelData                }            }        }    }}


運行我們的應用:


這是我們在利用wifi時顯示的IP地址。
我們的源碼在: git clone https://gitcafe.com/ubuntu/ipaddress.git


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

如何得到Ubuntu手機上的IP地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.