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 ...
随机推荐
- Redis配置文件(2)SNAPSHOTTING快照/APPEND ONLY MODE追加
redis.conf文件 1.Save a. save 秒钟 写操作次数 RDB是整个内存的压缩过的Snapshot,RDB的数据结构,可以配置复合的快照触发条件, 默认 是1分钟内改了1万次, 或5 ...
- ASP.NET Web API编程——构建api帮助文档
1 概要 创建ASP.NET Web Api 时模板自带Help Pages框架. 2 问题 1)使用VS创建Web Api项目时,模板将Help Pages框架自动集成到其中,使得Web Api项目 ...
- 解决 git pull 报错 fatal: refusing to merge unrelated histories
我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传. 先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法p ...
- 为什么 window.location.search 为空?
1,什么是window.location?示例 URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性 含义 值 protoc ...
- IDEA+MAVEN构建一个webapp骨架项目(解决一直卡在downloading plugins for问题)
下载:链接:https://pan.baidu.com/s/1jJx73H8 密码:nud0 第一步 我在上面链接下载了这个骨架xml,放进本地(你的目录默认.m2)\repository\org ...
- 微信小程序获取手机信息
wx.getSystemInfo({ success: function (res) { console.log(res.model)//手机型号 console.log(res.pixelRatio ...
- Redis服务端和客户端的命令
服务器端 服务器端的命令为redis-server 可以使⽤help查看帮助⽂档 redis-server --help 个人习惯 ps aux | grep redis 查看redis服务器进程su ...
- linux搭建的LNMP环境下的mysql授权远程连接
用phpstudy搭建的lnmp环境下mysql授权远程连接 简单高效 这是因为mysql 里的优先级不是所有人(提前检查防火墙是关闭状态)1.使用phpstudy安装的mysql没有放置到可以直接调 ...
- VSCode 配置 Python
假设 Anaconda 和 VSCode 都安装好了. 安装插件 VSCode 自带的 python 高亮不是很好,这里我们用 One Dark Pro 插件, 以及安装 Python 插件. 设定 ...
- kubernetes资源清单定义
apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 . kind: Pod #必选,Pod metadata: #必选,元数据 ...