码迷,mamicode.com
首页 > 其他好文 > 详细

Qt学习之路:获取本机网络信息

时间:2014-06-02 05:43:06      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

用Qt5实现的简单程序,获取本机网络信息。

 

建立工程后,在.pro里加入:

QT       += network

用Qt设计器创建界面:

bubuko.com,布布扣

在widget.h中声明getHostInformation类:

public:
    int getHostInformation();

 

声明槽函数:

public slots :
    void slotDetail();

 

并包含头文件:

#include <QHostInfo>
#include <QNetworkInterface>

 

在widget.cpp中实现:

bubuko.com,布布扣
int Widget::getHostInformation()
{
    QString localHostName = QHostInfo::localHostName();
    ui->lineEditLocalHostName->setText(localHostName);

    QHostInfo hostinfo = QHostInfo::fromName(localHostName);
    QList<QHostAddress> listAddress = hostinfo.addresses();
    if(!listAddress.isEmpty())
    {
        ui->lineEditAddress->setText(listAddress.first().toString());
    }
    return 0;
}

void Widget::slotDetail()
{
    QString detail = "";
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    for(int i=0;i<list.count();i++)
    {
        QNetworkInterface interface = list.at(i);
        detail = detail + tr("Device: ") + interface.name() + "\n";
        detail = detail + tr("Hardware address: ") + interface.hardwareAddress() + "\n";
        QList<QNetworkAddressEntry> entryList = interface.addressEntries();
        for(int j=0;j<entryList.count();j++)
        {
            QNetworkAddressEntry entry = entryList.at(j);
            detail = detail + "\t" + tr ("IP address: ") + entry.ip().toString() + "\n";
            detail = detail + "\t" +tr ("Subnet mask: ") + entry.netmask().toString() + "\n";
            detail = detail + "\t" +tr ("Broadcast address: ") + entry.broadcast().toString() + "\n";
        }
    }
    QMessageBox::information(this,tr("Detail"),detail);
}
bubuko.com,布布扣

 

编译运行:

bubuko.com,布布扣

bubuko.com,布布扣

 

Moran at 2014.6.1

Qt学习之路:获取本机网络信息,布布扣,bubuko.com

Qt学习之路:获取本机网络信息

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/moranBlogs/p/3763771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!