想在Qt界面上顯示獲取的ip地址,查了一下Qt的幫助資料,實現也相對簡單,幫助文檔中說明如下,還有其他更多的函數也沒有細看,先用着再說。
QList<QHostAddress> QNetworkInterface::allAddresses () [static]
This convenience function returns all IP addresses found on the host machine. It is equivalent to calling addressEntries() on all the objects returned by allInterfaces() to obtain lists of QHostAddress objects then calling QHostAddress::ip() on each of these.
程序中使用一個label來顯示獲取的ip,程序如下:
#include <QNetworkInterface> ui->iplabel->setText(QNetworkInterface().allAddresses().at(0).toString());
編譯出錯,需要在工程pro文件中添加,在
QT += core gui network
后添加network,正常運行,這是顯示的ip可能不一定是你想要的網卡,如顯示的是127.0.0.1,可以更改QNetworkInterface().allAddresses().at(1).toString() 中 at 括號中的值,最開始在開發板上用at(2)調試時,出現如下錯誤:
ASSERT failure in QList<T>::at: "index out of range", file /usr/local/Trolltech/QtEmbedded-4.8.4-arm/include/QtCore/qlist.h, line 469
本來以為我移植的4.8.4的庫不全,重新弄了遍還是不對,后來想想QLinst越界,可能就是這兒值越界了,更改后問題解決。