Qt之获取机器对应的MAC及IP地址

一、相关函数介绍

1.QHostInfo::localHostName()函数

QHostInfo::localHostName()函数是 Qt 网络模块中的函数,用于获取本地主机(计算机)的主机名。主机名是计算机在网络上的标识,通常是一个字符串,用于识别计算机。这个函数返回一个字符串,表示本地主机的主机名。

函数原型如下:

1
QString QHostInfo::localHostName();

以下是一些使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <QCoreApplication>
#include <QHostInfo>
#include <QDebug>
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    // 获取本地主机名
    QString localHostName = QHostInfo::localHostName();
 
    qDebug() << "本地主机名:" << localHostName;
 
    return a.exec();
}

2.QHostInfo::fromName函数

QHostInfo::fromName 函数用于执行域名解析,将主机名解析为 IP 地址信息。它可以用于查找主机名对应的 IP 地址,或者查找一个 IP 地址对应的主机名。这个函数返回一个 QHostInfo 对象,其中包含了有关解析结果的信息,如 IP 地址列表和主机名。

函数原型如下:

1
static QHostInfo QHostInfo::fromName(const QString &name);

其中 name 参数是要解析的主机名或 IP 地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QCoreApplication>
#include <QHostInfo>
#include <QDebug>
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    // 解析主机名到 IP 地址
    QHostInfo hostInfo = QHostInfo::fromName("www.example.com");
 
    if (hostInfo.error() == QHostInfo::NoError) {
        qDebug() << "主机名:" << hostInfo.hostName();
        qDebug() << "IP 地址列表:";
        foreach (const QHostAddress &address, hostInfo.addresses()) {
            qDebug() << address.toString();
        }
    } else {
        qDebug() << "解析错误:" << hostInfo.errorString();
    }
 
    return a.exec();
}

3.QNetworkInterface::allInterfaces函数  

QNetworkInterface::allInterfaces()用于获取计算机上所有网络接口的信息。它返回一个包含 QNetworkInterface 对象的列表,每个对象代表一个网络接口,提供有关接口的信息,如接口名称、MAC 地址、IP 地址等。  

函数的原型:

1
QList<QNetworkInterface> QNetworkInterface::allInterfaces();

以下是一个示例,演示如何使用 QNetworkInterface::allInterfaces() 函数来获取计算机上的网络接口信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <QCoreApplication>
#include <QNetworkInterface>
#include <QDebug>
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    QList<QNetworkInterface> networkInterfaces = QNetworkInterface::allInterfaces();
 
    qDebug() << "所有网络接口信息:";
 
    foreach (const QNetworkInterface &iface, networkInterfaces) {
        qDebug() << "接口名称:" << iface.name();
        qDebug() << "MAC 地址:" << iface.hardwareAddress();
 
        QList<QNetworkAddressEntry> addresses = iface.addressEntries();
        foreach (const QNetworkAddressEntry &entry, addresses) {
            qDebug() << "IP 地址:" << entry.ip().toString();
            qDebug() << "子网掩码:" << entry.netmask().toString();
        }
 
        qDebug() << "---------------------";
    }
 
    return a.exec();
}

二、完整代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowTitle("获取Windows IP的配置信息");
    this->setFixedSize(250, 100);
 
    m_pHostLab = new QLabel("主机名:", this);
    m_pLineEditHost = new QLineEdit(this);
 
    m_pIpLab = new QLabel("MAC地址:", this);
    m_pLineEditIp = new QLineEdit(this);
 
    m_pButDisply = new QPushButton("获取IP配置信息", this);
    connect(m_pButDisply, &QPushButton::clicked, this, &MainWindow::winHostInfo);
 
    m_pGlayout = new QGridLayout(this);
    m_pGlayout->addWidget(m_pHostLab, 0, 0);
    m_pGlayout->addWidget(m_pLineEditHost, 0, 1);
    m_pGlayout->addWidget(m_pIpLab, 1, 0);
    m_pGlayout->addWidget(m_pLineEditIp, 1, 1);
    m_pGlayout->addWidget(m_pButDisply, 2, 0, 1, 2);
 
    this->getHostInfo();
}
 
MainWindow::~MainWindow()
{
 
}
 
void MainWindow::getHostInfo()
{
    QString strHostName = QHostInfo::localHostName();
    m_pLineEditHost->setText(strHostName);
 
    QHostInfo hostInfo = QHostInfo::fromName(strHostName);
    QList<QHostAddress> listAddress = hostInfo.addresses();
    m_pLineEditIp->setText(listAddress.first().toString());
}
 
void MainWindow::winHostInfo()
{
    QString strTemp;
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    for (int i = 0; i < list.count(); i++) {
        QNetworkInterface interface = list.at(i);
        strTemp = strTemp + ("设备:") + interface.name() + "\n";
        strTemp = strTemp + ("硬件地址:") + interface.hardwareAddress() + "\n";
        QList<QNetworkAddressEntry> elist = interface.addressEntries();
        for (int j = 0; j < elist.count(); j++) {
            QNetworkAddressEntry es = elist.at(j);
            strTemp = strTemp + "\t" + ("IP地址:") + es.ip().toString() + "\n";
            strTemp = strTemp + "\t" + ("子网掩码:") + es.netmask().toString() + "\n";
            strTemp = strTemp + "\t" + ("广播地址:") + es.broadcast().toString() + "\n";
        }
    }
 
    QMessageBox::information(this, "本机网络信息", strTemp);
}

 

posted @   TechNomad  阅读(1454)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2022-10-25 Qt之进程间通信(共享内存)
点击右上角即可分享
微信分享提示