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. Android——SQLite实现面向对象CRUD

    android中SQLite的使用,事实上倒也不难.可是与JDBC操作数据库相比,这个还是有点不顺手,并且我好久没写底层的封装了,使用SSM框架这些都不须要考虑......好了,废话不多说.以下直接建 ...

  2. 【SVN Working copy is too old (format 10, created by Subversion 1.6)】解决方式

    SVN同步或者提交的时候出现类似错误信息: The working copy needs to be upgraded svn: Working copy 'D:\adt-bundle-windows ...

  3. Laravel 安装指南

    http://www.golaravel.com/article/laravel-installation-guide/ http://laravel.com/api/4.1/ http://ding ...

  4. Python网络爬虫(6)--爬取淘宝模特图片

    经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...

  5. EffectiveC#8--确保0对于值类型数据是有效的(初始化问题)

    1.决不要创建一个不包括0在内的枚举类型 2.举例如下: public enum Planet { Mercury = 1, Venus = 2, Earth = 3, Mars = 4, Jupit ...

  6. PIL库 (Pillow)

    PIL基础 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...

  7. 2个或多个datable类似于sql inner join 合并查询

    public static DataTable DatableInnerJoin(DataTable FirstTB, DataTable SecondTB, DataColumn[] FJC, Da ...

  8. WordPress主题制作全过程

    前言: 我想大多数使用WordPress的朋友都喜欢去尝试新的主题,但是换来换去,总是找不到那么一款适合自己的,让人很郁闷.于是很多人萌生了修改现有主题或自己动手从头制作一个主题的想法,但是问题又来了 ...

  9. 在virtualenv中安装libxml2和libxslt

    在使用python的工作中,需要使用到libxml2和libxslt库.原来在实际环境中已经安装完成了,但是在virtualenv中还没有,现在正在整理virtualenv的环境.下面把在virtua ...

  10. @Scheduled(cron="") spring定时任务时间设置

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...