networkinformation.h

#include<QtGui/QWidget>
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
#include<QGridLayout>
#include<QHostInfo> class NetworkInformation:public QWidget
{
Q_OBJECT public:
NetworkInformation(QWidget *parent=0);
void getHostInformation();
//~NetworkInformation(); public slots:
void slotDetail(); private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostName;
QLabel *ipLabel;
QLineEdit *LineEditAddress;
QPushButton *detailBtn;
QGridLayout *mainLayout;
};

networkinformation.cpp:

#include<QNetworkInterface>
#include<QMessageBox>
#include"networkinformation.h" NetworkInformation::NetworkInformation(QWidget *parent)
:QWidget(parent)
{
hostLabel=new QLabel(tr("host:"));
LineEditLocalHostName=new QLineEdit;
ipLabel=new QLabel(tr("IP:"));
LineEditAddress=new QLineEdit; detailBtn=new QPushButton(tr("details")); mainLayout=new QGridLayout(this);
mainLayout->addWidget(hostLabel,0,0);
mainLayout->addWidget(LineEditLocalHostName,0,1);
mainLayout->addWidget(ipLabel,1,0);
mainLayout->addWidget(LineEditAddress,1,1);
mainLayout->addWidget(detailBtn,2,0,1,2); getHostInformation(); connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
} void NetworkInformation::getHostInformation()
{
QString localHostName=QHostInfo::localHostName();
LineEditLocalHostName->setText(localHostName); QHostInfo hostInfo=QHostInfo::fromName(localHostName);
QList<QHostAddress> listAddress=hostInfo.addresses(); if(!listAddress.isEmpty())
{
LineEditAddress->setText(listAddress.first().toString());
}
} void NetworkInformation::slotDetail()
{
QString detail="";
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces(); for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
detail=detail+tr("shebei:")+interface.name()+"\n";
QString hardwareAddress=interface.hardwareAddress();
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("netmask:")+entry.netmask().toString()+"\n";
detail=detail+"\t"+tr("broadcast:")+entry.broadcast().toString()+"\n";
}
}
QMessageBox::information(this,tr("Detail"),detail);
}

main.cpp:

#include<QtGui/QApplication>
#include"networkinformation.h"
#include<QTextCodec> int main(int argc, char *argv[])
{
QApplication a(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
NetworkInformation w;
w.show();
return a.exec();
}

qt获取本机网络信息的更多相关文章

  1. Qt - 获取本机网络信息

    目的: 获取本机的主机名.IP地址.硬件地址等网络信息. 工具: 使用Qt提供的网络模块QtNetwork(pro文件里面加network): 使用Qt提供的类QHostInfo.QNetworkIn ...

  2. Qt5获取本机网络信息

    获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...

  3. Qt网络获取本机网络信息

    下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...

  4. Qt-网络与通信-获取本机网络信息

    在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息.运用QHostInfo.QNetworkInterface.QNetworkAddressEntry可以获得本机的网络信息. 上运行截图 ...

  5. Linux中获取本机网络信息的几个函数及应用

    一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...

  6. Qt之获取本机网络信息(MAC, IP等等,很全)

    经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...

  7. Qt之获取本机网络信息(超详细)

    经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...

  8. linux编程获取本机网络相关参数

    getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程   ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...

  9. C#获取本机磁盘信息

    照着书敲的.留作笔记吧. using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

随机推荐

  1. freemarker书写select组件错误摘要(七)

    1.错误叙述性说明 六月 26, 2014 11:26:27 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...

  2. 制作可独立分发的Android模拟器

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6586759 如果我们编写了一个Android应 ...

  3. 又一道软通动力7K月薪面试题——银行业务调度系统

    后期补充:网友对我诟病最多的就是我帮学生做面试题,说这是小偷和骗子行为,在此,我对自己给学员做面试题做出例如以下解释:  (1)学员拿着面试题来找老师,学生也事先思考和尝试后实在没有办法,又求职心切才 ...

  4. 虚拟化之docker

    1,什么是docker dokcer是应用容器,它是基于系统的虚拟化,他不是基于硬件的虚拟化(KVM),可能这样说有点抽象,我在知乎看到过一个比喻, docker思想是来源于集装箱的,在一艘大船上,可 ...

  5. MFC让控件随窗口大小而改变

    转载自http://blog.csdn.net/chw1989/article/details/7488711 大小和位置都改变(亲测可行) 1.首先为窗体类添加CRect m_rect,该成员变量用 ...

  6. 三大主流ETL工具选型

    ETL(extract, transform and load)产品乍看起来似乎并不起眼,单就此项技术本身而言,几乎也没什么特别深奥之处,但是在实际项目中,却常常在这个环节耗费太多的人力,而在后续的维 ...

  7. 淘管 ERP项目与淘宝对接中产生的若干问题及处理办法

    现象:ERP绑定淘宝后,下载商品数据时如果成功,ajax不断尝试重发. 原因:  /app/taoapi/lib/top/TopClient.php 中的curl()方法成功后,返回空值,而前端收到空 ...

  8. HTTP之I/O模型图MPM详细解析

    高度模块化:DSO MPM:多路处理模块      prefork-->一个主进程+多个工作进程,每个工作进程处理多个请求      worker-->一个主进程+多个工作进程,每个工作进 ...

  9. python 核心编程第六章课后题自己做的答案

    6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了) 'Take a str ...

  10. position属性absolute与relative(转)

    每次要用到Position属性时,总要去搜索下,这两个属性值的区别:今天就直接复制网上的结果,以便以后多看看. Absolute,CSS中的写法是:position:absolute; 他的意思是绝对 ...