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 ...
随机推荐
- 从percona server 5.7换到mariadb 10.2
过去两年半一直推荐使用percona server,今天开始,因为一些mysql迟迟不不愿意支持的特性,打算换回mariadb 10.2了,具体哪些不说了,总之非常关键,mariadb都支持一两年了, ...
- windows下vc编译和debug nginx
总体来说,各个步骤以及版本参考官方文档http://nginx.org/en/docs/howto_build_on_win32.html一点没错,有些细节没说清楚. To build nginx: ...
- python基础--概念性问题
面试其他篇 目录: 头条面试题:https://blog.csdn.net/m0_37947204/article/details/80103151 1.1 python常见基础题 1.遍历文件夹 # ...
- kali安装搜狗输入法
安装步骤 依赖1: apt-get install fcitx 依赖2: apt-get install fcitx-libs-qt 重建: apt-get --fix-broken install ...
- WIN10安装和使用MySql5.6中遇到的一些问题与解决
WIN10安装和使用MySql5.6中遇到的一些问题与解决 提示一下,安装前需要安装python环境. MySql安装缺少组件MySQL for Excel 如图(转载别人的图,自己的安装时没有截图) ...
- bzoj 3560 DZY Loves Math V - 线性筛 - 扩展欧几里得算法
给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). Input 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. Output 仅一行答案. Sampl ...
- 【Python30--文件系统】
一.模块 定义:模块是一个包含所有定义的函数和变量的文件,后缀名是.py.模块可以被别的程序引用,以使用该模块中的函数等功能 >>> secret = random.randint( ...
- python --- 17. 面向对象成员
面向对象的成员 能写的所有成员 一.变量 1.实例变量 访问时 对象.变量 2.类变量 访问时 类名.变量 注意: 二.方法 1.实例方法 必须有一个参数(self) 调用时 ...
- linux内核中的IPIs是什么?
答: 处理器间中断(Interprocessor Interrupts)
- codevs1048石子归并
codevs1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 http://codevs.cn/problem/1048/ 题目描述 ...