获取本机网络信息

在pro文件中加入如下代码

QT       += network

widget.h中的代码如下

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QHostInfo>
#include <QNetworkInterface> namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = 0);
~Widget();
void getHostInformation();
public slots:
void slotDetail();
private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostName;
QLabel *ipLabel;
QLineEdit *LineEditAddress;
QPushButton *detailBtn;
QGridLayout *mainLayout;
Ui::Widget *ui;
}; #endif // WIDGET_H

widget.cpp中的代码如下

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QNetworkAddressEntry> Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
hostLabel=new QLabel(tr("主机名:"));
LineEditLocalHostName=new QLineEdit();
ipLabel=new QLabel(tr("IP地址:"));
LineEditAddress=new QLineEdit();
detailBtn=new QPushButton(tr("详细"));
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(bool)),
this,SLOT(slotDetail()));
} //获取详细信息函数
void Widget::getHostInformation()
{
QString localHostName=QHostInfo::localHostName(); //获取本机主机名
LineEditLocalHostName->setText(localHostName); //根据主机名获取相关的IP地址
QHostInfo hostInfo=QHostInfo::fromName(localHostName);
//获取主机的IP地址列表
QList<QHostAddress> listAddress=hostInfo.addresses();
if(!listAddress.isEmpty())
{
LineEditAddress->setText(listAddress.at(2).toString());
} }
//“详细”按键按下时的槽函数
void Widget::slotDetail()
{
QString detail="";
//QNetWorkInterface类提供了一个主机IP地址和网络接口的列表
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces(); for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
//获得网络接口的名称
detail=detail+tr("设备")+interface.name()+"\n";
//获得网络接口的硬件地址
detail=detail+tr("硬件")+interface.hardwareAddress()+"\n";
QList<QNetworkAddressEntry> entryList=interface.addressEntries(); for(int j=1;j<entryList.count();j++)
{
QNetworkAddressEntry entry=entryList.at(j);
detail=detail+"\t"+tr("IP地址:")+entry.ip().toString()+"\n";
detail=detail+"\t"+tr("子网掩码:")+entry.netmask().toString()+"\n";
detail=detail+"\t"+tr("广播地址:")+entry.broadcast().toString()+"\n";
} } //end for(int i=0;i<list.count();i++)
QMessageBox::information(this,tr("Detail"),detail);
}
Widget::~Widget()
{
delete ui;
}

main.cpp中的代码如下

#include "widget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show(); return a.exec();
}

运行效果如下



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

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

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

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

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

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

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

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

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

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

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

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

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

  7. qt获取本机网络信息

    networkinformation.h #include<QtGui/QWidget> #include<QLabel> #include<QPushButton> ...

  8. Qt5获取网卡/IP等信息

    参考网址:http://blog.csdn.net/wjs1033/article/details/22697063 1.环境 Win7x64.Qt5.5.1(x86).vs2013_ultimate ...

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

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

随机推荐

  1. Linux进程内存分析和内存泄漏定位

    在Linux产品开发过程中,通常需要注意系统内存使用量,和评估单一进程的内存使用情况,便于我们选取合适的机器配置,来部署我们的产品. Linux本身提供了一些工具方便我们达成这些需求,查看进程实时资源 ...

  2. JVM内存结构之堆、栈、方法区以及直接内存、堆和栈区别

    JVM内存结构之堆.栈.方法区以及直接内存.堆和栈区别 一.  理解JVM中堆与栈以及方法区 堆(heap):FIFO(队列优先,先进先出):二级缓存:*JVM中只有一个堆区被所有线程所共享:对象和数 ...

  3. css-过渡

    css过渡:元素从一种样式逐渐改变为另一种的效果.过渡所需的条件:1.所过渡的元素必须有css样式.2.必须有过渡时间.以下是过渡元素的属性:transition:简写属性,用于在一个属性中设置四个过 ...

  4. css3-动画(animation)

    css3-动画(animation): 具有以下属性: 1.animation-name 自定义动画名称 2.animation-duration 动画指定需要多少秒或毫秒完成,默认值是0; 3.an ...

  5. 使用Jenkins构建、部署spring boot项目

    一.环境搭建 本次实验的环境为Ubuntu 16.04,Jenkins 2.8.3 1.安装ssh sudo apt-get update # 更新软件源 sudo apt-get install o ...

  6. CocoaPods创建自己的公开库、私有库

    http://www.cocoachina.com/ios/20180308/22509.html

  7. python --- 07 补充( join 删除和添加 fromkeys ) 深浅拷贝

    一.基本数据类型补充 1.join() "*".join("马虎疼") # 马*虎*疼 把传递进去的参数进行迭代.  获取到的每个元素和前面的*进行拼接. 得到 ...

  8. Selenium Webdriver wait for JavaScript JQuery and Angular

    Hi all, during the last two weeks I was dealing with the best solution to wait for both JQuery, Angu ...

  9. 彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server

    目录的权限都 应该设置 为 drwxr_xr_x, 即755, 而html下的文件的权限设置为; 644 即可! -x 只有目标文件对某些用户是可执行的或该目标文件是目录时才追加x 属性. -w权限, ...

  10. CodeForces 430A Points and Segments (easy)(构造)题解

    题意:之前愣是没看懂题意...就是给你n个点的坐标xi,然后还规定了Li,Ri,要求给每个点染色,每一组L,R内的点红色和黑色的个数不能相差大于1个,问你能不能染成功,不能输出-1,能就按照输入的顺序 ...