zoukankan      html  css  js  c++  java
  • Qt 中获取本机IP地址

    想在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越界,可能就是这儿值越界了,更改后问题解决。

  • 相关阅读:
    js生成cron表达式
    原生table控制tbody滚动而thead不滚动
    js记性
    oracle 多库表建立dblink查询
    java操作mysql数据库备份
    java 抓取网页的图片
    ZOJ 3485 Identification Number【模拟】【暴力】
    Codeforces 1037E Trips【图论】【dfs】
    Codeforces 1036C Classy Numbers 【数位dp】
    Codeforces 1036B Diagonal Walking v.2 【贪心】
  • 原文地址:https://www.cnblogs.com/emouse/p/3032580.html
Copyright © 2011-2022 走看看