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

  • 相关阅读:
    计算机硕士工资一览表 08年最新各大IT公司薪水行
    VS2010单元测试
    windows操作系统的快捷键
    关于
    [美国代购] Nexus 6 与 Moto X 询价聊天记录整理
    nginxのerror_logはformat指定できない.
    Give $20/month and provide 480 hours of free education
    如何才可能将一件事情做到最高的效率
    Palindrome Number
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/emouse/p/3032580.html
Copyright © 2011-2022 走看看