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. fopen 參数具体解释

    fopen fopen(打开文件) 相关函数 open,fclose 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path, ...

  2. 机器学习实战笔记5(logistic回归)

    1:简单概念描写叙述 如果如今有一些数据点,我们用一条直线对这些点进行拟合(改线称为最佳拟合直线),这个拟合过程就称为回归.训练分类器就是为了寻找最佳拟合參数,使用的是最优化算法. 基于sigmoid ...

  3. LabVIEW新手5大错误

    虽然NI LabVIEW软件长期以来一直帮助工程师和科学家们快速开发功能测量和控制应用,但不是所有的新用户都会遵循LabVIEW编程的最佳方法. LabVIEW图形化编程比较独特,因为只需看一眼用户的 ...

  4. Xcode5和6上新建工程如何本地化启动页面

    建议阅读本篇文章前先具备iOS本地化的基本知识,Google中搜索“iOS本地化”,有成片的教程~~ 最近有个app需要支持英语.简体中文.繁体中文,由于启动页面上有文字,所以也不得不做下本地化处理. ...

  5. sql语句中查询出的数据添加一列,并且添加默认值

    查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...

  6. 33c3-pwn350-tea

    TEA 感觉这个题目出得很不错.先运行程序了解基本功能,程序可以读取对系统上存在的文件的内容,如果文件不存在的话,直接退出. 使用IDA打开后,发现父进程通过clone api克隆出一个子进程,主要的 ...

  7. Makefile 工程管理

    Makefile 工程管理 Makefile 规则 --变量 在Makefile中,用户除了可以自己定义变量外,还可以使用存在系统已经定义好的默认变量 $^:代表所有的依赖文件 $@:代表目标 $&l ...

  8. c# 调用 友盟api

    今天要使用友盟的推送API来给我的app进行推送信息,调试了好久,老是返回500错误,最终在友盟的技术人员支持下完成了此操作,在此多谢友盟技术和客服人员. 把发方法和注意事项贴出来供大家参考. pub ...

  9. 不能修改“System Roots”钥匙串 即下载的.cer 文件添加不到钥匙串

    双击提示  :不能修改“System Roots”钥匙串要更改根证书是否会被信任,请在“钥匙串访问”中打开它,然后修改它的信任设置. 解决办法:添加到   登录或显示LOGIN的 keychain(记 ...

  10. (原)测试intel的并行计算pafor

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/4988264.html 参考网址: 关于mt19937:http://www.cnblogs.com/e ...