Qt5获取本机网络信息
获取本机网络信息
在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获取本机网络信息的更多相关文章
- Qt网络获取本机网络信息
下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...
- Qt-网络与通信-获取本机网络信息
在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息.运用QHostInfo.QNetworkInterface.QNetworkAddressEntry可以获得本机的网络信息. 上运行截图 ...
- Qt - 获取本机网络信息
目的: 获取本机的主机名.IP地址.硬件地址等网络信息. 工具: 使用Qt提供的网络模块QtNetwork(pro文件里面加network): 使用Qt提供的类QHostInfo.QNetworkIn ...
- Linux中获取本机网络信息的几个函数及应用
一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...
- Qt之获取本机网络信息(MAC, IP等等,很全)
经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...
- Qt之获取本机网络信息(超详细)
经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...
- qt获取本机网络信息
networkinformation.h #include<QtGui/QWidget> #include<QLabel> #include<QPushButton> ...
- Qt5获取网卡/IP等信息
参考网址:http://blog.csdn.net/wjs1033/article/details/22697063 1.环境 Win7x64.Qt5.5.1(x86).vs2013_ultimate ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
随机推荐
- python的反射函数(hasattr()、getattr()、setattr()与delattr())和类的内置属性attr(__getattr()__、__setattr()__与__delattr()__)
主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省),有四个可以实现自省函数. hasattr(object,name) 判断object中是否有name字符串对应的属性或方法,返回Tr ...
- centos6二进制安装mysql5.5
centos 6.5,安装mysql 5.5.60 所需安装包mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz.ncurses-devel-5.7-4.200902 ...
- spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法
因为从Spring Boot 1.4开始,spring boot就不支持log4j了,必须是log4j2或者logback,具体两者如何配置以及NDC的支持可以参考spring boot精华版. 这里 ...
- 1、pandas使用sort_values排序
用Numpy库的randn函数生成一个完整的DataFrame: DataFrame有多个参数: data就是要转换成DataFrame的内容,很多数据类型都可以转换成DataFrame,比如:Ser ...
- MyBatis 与 Hibernate 到底哪个更快?
前言 由于编程思想与数据库的设计模式不同,生出了一些ORM框架. 核心都是将关系型数据库和数据转成对象型.当前流行的方案有Hibernate与myBatis. 两者各有优劣.竞争激烈,其中一个比较重要 ...
- body-parser 用法
1.下载 body-parser 模块 : npm install body-parser 2.require body-parser 模块(引入),并用一个变量接收(此处栗子变量为 bodyp ...
- python --- 19 判断对象所属,区分函数和对象, 反射
一.判断对象所属 isinstance, type , issubclass 1.issubclass(x,y) 判断x是否是y 的子类 2.type(x) 精准返回x 的数据类型 3.isi ...
- topcoder srm 485 div1
problem1 link 枚举第一个数和第二个数即可确定公差. problem2 link 设高度为$n$,宽度为$m$,且$n \ge m$ 如果$m \ge 5$,那么答案为0.这个可以通过抽屉 ...
- Python3 tkinter基础 Canvas create_text 在画布上添加文字
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- bitset,2018蓝桥杯-明码(二进制转换)
bitset可以存储二进制数位 bitset<8> x(2); cout<<x<<endl; //输出:00000010 #include <iostream ...