目的:

  获取本机的主机名、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 - 获取本机网络信息的更多相关文章

  1. qt获取本机网络信息

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

  2. Qt5获取本机网络信息

    获取本机网络信息 在pro文件中加入如下代码 QT += network widget.h中的代码如下 #ifndef WIDGET_H #define WIDGET_H #include <Q ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. C#获取本机磁盘信息

    照着书敲的.留作笔记吧. using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

随机推荐

  1. Python 异常处理Ⅳ

    异常的参数 一个异常可以带上参数,可作为输出的异常信息参数. 你可以通过except语句来捕获异常的参数,如下所示: 变量接收的异常值通常包含在异常的语句中.在元组的表单中变量可以接收一个或者多个值. ...

  2. HTML中的超链接(Hyperlink)

    超链接 ★超链接简单介绍 超链接可以说是网页中最常见的元素.超链接的英文名是hyperlink.每个网站都是由众多的网页组成,网页之间通常都是通过链接的方式相互关联的. 超链接能够让浏览者在各个独立的 ...

  3. Pygame模块实现功能超赞的贪吃蛇

    import pygame import random import sys import pygame.freetype import re import datetime   pygame.ini ...

  4. JavaScript事件兼容性写法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. XSS这段时间的学习总结

    0X01利用平台payload获取COOKIE 本机IP 192.168.1.100 靶机win7 192.168.1.102 我们先创建一个cookie的项目 然后在可以执行xss的地方插入我们的恶 ...

  6. Android学习_7/22

    一.Android项目目录结构 1.         AndroidManifest.xml:整个Android项目的配置,注册各组件 <activity android:name=" ...

  7. LeetCode----两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  8. 为EasyUI的dataGrid单元格增加鼠标移入移出事件

    onLoadSuccess: function (data) { $(".datagrid-row").mouseover(function (e) { var text = $( ...

  9. MQ常问的问题

    目录 1:什么场景使用了mq?直接掉接口不行吗? 2:用消息队列都有什么优点和缺点? 3:Kafka.ActiveMQ.RabbitMQ.RocketMQ 都有什么区别? 4:那你们是如何保证消息队列 ...

  10. What exactly is the parameter e (event) and why pass it to JavaScript functions?

    What exactly is the parameter e (event) and why pass it to JavaScript functions? 问题 Well, when I lea ...