Qt-网络与通信-获取本机网络信息
在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息。运用QHostInfo、QNetworkInterface、QNetworkAddressEntry可以获得本机的网络信息。
上运行截图
这里需要注意的,在Qt5.80 VS的版本中,有的字符“:”中文版本的,会导致编译错误。
第一步,需要再pro文件中加入 QT+= network
.h文件
#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QHostInfo>
#include <QNetworkInterface> class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = 0);
~Widget();
void getHostInformation();
public slots:
void slotDetail();
private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostName;
QLabel *ipLabel;
QLineEdit *LineEditAddress;
QPushButton *detailBtn;
QGridLayout *mainLayout;
}; #endif // WIDGET_H
.cpp文件
#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
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()),this,SLOT(slotDetail()));
} Widget::~Widget()
{ } void Widget::getHostInformation()
{
QString localHostName = QHostInfo::localHostName();
LineEditLocalHostName->setText(localHostName);
QHostInfo hostInfo = QHostInfo::fromName(localHostName); QList<QHostAddress> listAddress = hostInfo.addresses(); qDebug()<<listAddress;
if(!listAddress.isEmpty())
{
LineEditAddress->setText(listAddress.at(4).toString());
}
} void Widget::slotDetail()
{
QString detail="";
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
//(a)
for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
detail=detail+tr("设备:")+interface.name()+"\n";
//(b)
detail=detail+tr("硬件地址:")+interface.hardwareAddress()+"\n";
//(c)
QList<QNetworkAddressEntry> entryList=interface.addressEntries();
//(d)
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";
}
}
QMessageBox::information(this,tr("Detail"),detail);
}
工程地址:https://gitee.com/DreamLife-Technology_DreamLife/NetworkInformation
Qt-网络与通信-获取本机网络信息的更多相关文章
- Qt网络获取本机网络信息
下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...
- Qt5获取本机网络信息
获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- C#获取本机磁盘信息
照着书敲的.留作笔记吧. using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- python学习之最简单的获取本机ip信息的小程序
文章是从我的个人博客粘贴过来的,大家可以直接访问我的个人博客哦 http://www.iwangzheng.com 获取本机ip信息的命令ifconfig总是在用,这次拿到pyhton代码里,感觉py ...
- 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 ...
随机推荐
- 20145238-荆玉茗《网络对抗技术》MSF基础应用
20145238荆玉茗-<网络攻防>-MSF基础应用 实践过程 MS08_067漏洞渗透攻击实践(主动攻击) 实验工具: kali&windows xp 将xp的网络设为NAT模式 ...
- flex布局兼容性写法
CSS样式 flex:定义布局为盒模型 flex-v:盒模型垂直布局 flex-1:子元素占据剩余的空间 flex-align-center:子元素垂直居中 flex-pack-center:子元素水 ...
- C#string byte[] base64位互相转换
byte表示字节,byte[]则表示存放一系列字节的数组 1个字符=2个字节(byte) 1个字节=8个比特(bit) 网速上所说的1M其实是指1兆的小b,1M= 1024b/8 = 128kb 下面 ...
- Servlet是线程安全的吗
Servlet默认是单例模式,在web容器中只创建一个实例,所以多个线程同时访问Servlet是不安全的. 解决此类问题的办法是: 只要Servlet实现 SingleThreadModel 接口
- STM32之系统时钟
转载:http://www.openedv.com/posts/list/302.htm 时钟系统是处理器的核心,所以在学习STM32所有外设之前,认真学习时钟系统是必要的,有助于深入理解STM32. ...
- 2017年终总结&展望2018年
转眼就要挥别2017年了,也看到好多人都在叹时间过得好快.对啊,在我的印象中时间过得慢的时期恐怕只有中小学期间了,转眼研究生阶段已经过了一半.如今这个阶段,很多时候忙任务和学东西好不容易觉得自己摸到点 ...
- 竞赛题解 - NOIP2018 保卫王国
\(\mathcal{NOIP2018}\) 保卫王国 - 竞赛题解 按某一个炒鸡dalao名曰 taotao 的话说: \(\ \ \ \ \ \ \ \ \ "一道sb倍增题" ...
- redhat6 快速部署percona
1.首先得能访问外网 2.yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-relea ...
- PHP中计算字符串相似度的函数代码
similar_text — 计算两个字符串的相似度 int similar_text ( string $first , string $second [, float &$percent ...
- 初学者:__init__.py文件的作用
__init__.py 文件的作用及意义 __init__.py文件是一个包必须的文件,即使它是空的,但也是必须的,如果没有这个文件,python将不会把该文件夹当做一个package,而仅仅是一个d ...