Qt - 获取本机网络信息
目的:
获取本机的主机名、IP地址、硬件地址等网络信息。
工具:
使用Qt提供的网络模块QtNetwork(pro文件里面加network);
使用Qt提供的类QHostInfo、QNetworkInterface、QNetworkAddressEntry。
代码:
获取本机主机名和IP地址
void NetworkInformation::getHostInformation() {
//获取本机主机名
QString localHostName = QHostInfo::localHostName();
LineEditLocalHostName->setText(localHostName);
//通过本机主机名获取IP地址
QHostInfo hostInfo = QHostInfo::fromName(localHostName);
QList<QHostAddress> listAddress = hostInfo.addresses();
if(!listAddress.isEmpty()) {
LineEditAddress->setText(listAddress.first().toString());
}
}
获取本机更加详细的信息
void NetworkInformation::slotDetail() {
QString detail = "";
//获取主机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";
//获取网络接口对应的IP地址(IPv4和IPv6)、子网掩码、广播地址
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
for(int j = 0; 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);
}
Qt - 获取本机网络信息的更多相关文章
- qt获取本机网络信息
networkinformation.h #include<QtGui/QWidget> #include<QLabel> #include<QPushButton> ...
- Qt5获取本机网络信息
获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...
- Qt网络获取本机网络信息
下面我们就讲解如何获取自己电脑的IP地址以及其他网络信息.这一节中,我们会涉及到网络模块(QtNetwork Module)中的QHostInfo ,QHostAddress ,QNetworkInt ...
- Qt-网络与通信-获取本机网络信息
在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息.运用QHostInfo.QNetworkInterface.QNetworkAddressEntry可以获得本机的网络信息. 上运行截图 ...
- 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 ...
- 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 ...
随机推荐
- 后端返回图片的url,将其转成base64,再次进行上传
//将图片变成base64再上传(主要是转化来自客户端的图片) getUrlBase64=(url, ext)=> { var canvas = document.createEl ...
- TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7019 Accepted: 2387 De ...
- textstudio添加中文支持
修改编译器为XeLaTeX,方法如下: Options -> Configure -> Build -> Default Compiler -> XeLaTeX 并在文本开头处 ...
- THUWC 2019游记
坑先占着QwQ 不太喜欢写Blog, 因此我的Blog一直很冷清,但是还是想写写这种游记啦,反正是给自己看的 Day -4 颓了一整天,一道题都没写 于是无聊用GL写了一个海战游戏,让两个程序对打-- ...
- 【转载】多网卡的7种bond模式原理
多网卡的7种bond模式原理 Linux 多网卡绑定 网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6 常用的有三种 mode=0 ...
- 【Java基础】谈谈集合.List
摘自:https://www.cnblogs.com/54chensongxia/p/11722828.html 目录 1. ArrayList 1.1 ArrayList的构造 1.2 add方法 ...
- ES6指北【1】——let、const
1.如何学习ES6 1.1 js的学习顺序 ES5 -> ES6 -> ES7 -> ES8 以此类推 ES5没学好就别想学好ES6 1.2 边学边用 学了就要用 2.变量声明的方式 ...
- Python学习笔记:外部数据的输入、存储等操作
查看current工作路径: >>> import os >>> os.getcwd() 'D:\\python' 更改工作路径: >>> os. ...
- docker—tomcat 报错:Failed to get D-Bus connection: Operation not permitted
docker search centos 查系统镜像 docker pull docker.io/centos 进入容器 [root@git opt]# docker images REPOSIT ...
- RF中BDD编写
工程结构 用户关键字 测试用例 运行日志