目录
- 1. Qt网络模块
- 2. QHostInfo
- 2.1 公共函数
- 2.2 静态函数
- 3. QNetworkInterface
- 3.1 公共函数
- 3.2 静态函数
- 4. 代码示例
- Dialog.h
- Dialog.cpp
- 界面展示
1. Qt网络模块
使用Qt网络模块,需要在配置文件.pro中添加:
Qt += network
2. QHostInfo
2.1 公共函数
QList<QHostAddress> addresses()
返回与hostName()主机关联的Ip地址列表
QString hostName()
返回已查询其IP地址的主机的名称
int lookupId()
返回本次查找的ID
QHostInfoError error()
若主机查找失败,返回失败类型
QString errorString()
若主机查找失败,返回错误描述字符串
2.2 静态函数
QString localHostName()
返回本机的主机名
QHostInfo fromName(QString& name)
通过主机名获取IP地址
QString localDomainName()
返回本机的DNS域名
int lookupHost(QString& name, QObject* receiver, char* member)
以异步方式根据主机名查找主机IP地址,返回本次查找的ID,用于abortHostLookup()
void abortHostLookup(int id)
用lookupHost()返回的ID ID中止主机查找
3. QNetworkInterface
3.1 公共函数
QList<QNetworkAddressEntry> addressEntries()
返回该网络接口(包括子网掩码和广播地址)的IP地址列表
QString hardwareAddress()
返回该接口的低级硬件地址,以太网里就是MAC地址
QString name()
返回网络接口名称
QString humanReadableName()
返回可以读懂的接口名称,如果名称不确定,得到的就是name()函数的返回值
bool isValid()
若接口信息有效就返回true
3.2 静态函数
QList<QHostAddress> allAddresses()
返回主机上所有IP地址的列表
QList<QNetworkInterface> allInterfaces()
返回主机上所有接口的网络列表
一个网络接口可能包含多个IP地址,每个IP地址与掩码或广播地址关联。如果无需知道子网掩码和广播的Ip地址,可以使用allAdresses()获得主机的所有IP地址列表
4. 代码示例
Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QCheckBox>
#include <QDialog>
#include <QGridLayout>
#include <QHostInfo>
#include <QLineEdit>
#include <QNetworkInterface>
#include <QPlainTextEdit>
#include <QPushButton>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog {
Q_OBJECT
public:
explicit Dialog(QWidget* parent = 0);
~Dialog();
private:
//根据QAbstractSocket::NetworkLayerProtocol的枚举类型,返回协议名称的字符串
QString protocolType(QAbstractSocket::NetworkLayerProtocol protocol);
private slots:
void slotBtnGetHostInfoClicked();
void slotBtnLookupHostInfoClicked();
void slotLookupHostInfo(const QHostInfo& host);
void slotBtnAllInterfaceClicked();
void slotBtnAllAddressClicked();
void slotClearTextClicked();
private:
Ui::Dialog* ui;
QPushButton* m_pBtnGetHostInfo;
QCheckBox* m_pCheckIPv4;
QPushButton* m_pBtnLookupHostInfo;
QLineEdit* m_pEditHost;
QPushButton* m_pBtnAllInterface;
QPushButton* m_pBtnAllAddress;
QPlainTextEdit* m_pTextShowInfo;
QPushButton* m_pClearText;
};
#endif // DIALOG_H
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget* parent) : QDialog(parent), ui(new Ui::Dialog) {
ui->setupUi(this);
this->setWindowTitle(QStringLiteral("网络主机信息"));
// m_pBtnGetHostInfo = new QPushButton(this);
// m_pBtnGetHostInfo->setText(QStringLiteral("获取本机主机名和IP地址"));
m_pBtnGetHostInfo = new QPushButton(QStringLiteral("获取本机主机名和IP地址"), this);
m_pCheckIPv4 = new QCheckBox(QStringLiteral("只显示IPv4协议地址"), this);
m_pCheckIPv4->setCheckState(Qt::Checked);
m_pCheckIPv4->setLayoutDirection(Qt::RightToLeft); //更改复选框的位置在右边
m_pBtnLookupHostInfo = new QPushButton(QStringLiteral("查找域名的IP地址"), this);
m_pEditHost = new QLineEdit(QStringLiteral("www.baidu.com"), this);
m_pBtnAllInterface = new QPushButton(QStringLiteral("QNetworkInterface::allInterface"), this);
m_pBtnAllAddress = new QPushButton(QStringLiteral("QNetworkInterface::allAddress"), this);
QGridLayout* GLay1 = new QGridLayout();
GLay1->addWidget(m_pBtnGetHostInfo, 0, 0);
GLay1->addWidget(m_pCheckIPv4, 0, 1, Qt::AlignHCenter);
GLay1->addWidget(m_pBtnLookupHostInfo, 1, 0);
GLay1->addWidget(m_pEditHost, 1, 1);
GLay1->addWidget(m_pBtnAllInterface, 2, 0);
GLay1->addWidget(m_pBtnAllAddress, 2, 1);
m_pTextShowInfo = new QPlainTextEdit(this);
m_pClearText = new QPushButton(QStringLiteral("清空文本框"), this);
QGridLayout* GLay = new QGridLayout(this);
GLay->addLayout(GLay1, 0, 0);
GLay->addWidget(m_pTextShowInfo);
GLay->addWidget(m_pClearText);
connect(m_pBtnGetHostInfo, &QPushButton::clicked, this, &Dialog::slotBtnGetHostInfoClicked);
connect(m_pBtnLookupHostInfo, &QPushButton::clicked, this, &Dialog::slotBtnLookupHostInfoClicked);
connect(m_pBtnAllInterface, &QPushButton::clicked, this, &Dialog::slotBtnAllInterfaceClicked);
connect(m_pBtnAllAddress, &QPushButton::clicked, this, &Dialog::slotBtnAllAddressClicked);
connect(m_pClearText, &QPushButton::clicked, this, &Dialog::slotClearTextClicked);
}
Dialog::~Dialog() { delete ui; }
QString Dialog::protocolType(QAbstractSocket::NetworkLayerProtocol protocol) {
//通过协议类型返回协议名称
switch (protocol) {
case QAbstractSocket::IPv4Protocol: return "IPv4 Protocol";
case QAbstractSocket::IPv6Protocol: return "IPv6 Protocol";
case QAbstractSocket::AnyIPProtocol: return "Any IP Protocol";
default: return "Unknow Network Layer Protocol";
}
}
void Dialog::slotBtnGetHostInfoClicked() {
QString hostName = QHostInfo::localHostName(); //本地主机名
m_pTextShowInfo->appendPlainText("本地主机名:" + hostName + "\n");
QHostInfo hostIP = QHostInfo::fromName(hostName); //本机IP
QList<QHostAddress> addList = hostIP.addresses(); // IP地址列表
if (!addList.isEmpty()) {
for (int i = 0; i < addList.count(); i++) {
QHostAddress ipAddr = addList.at(i); //每一项是一个QHostAddress
bool isCheck = m_pCheckIPv4->isChecked();
if (isCheck)
isCheck = (QAbstractSocket::IPv4Protocol == ipAddr.protocol()); //只显示IPv4
else
isCheck = true;
if (isCheck) {
m_pTextShowInfo->appendPlainText("协议:" + protocolType(ipAddr.protocol()));
m_pTextShowInfo->appendPlainText("本机IP地址:" + ipAddr.toString());
}
}
m_pTextShowInfo->appendPlainText("-----------------------------");
}
}
void Dialog::slotBtnLookupHostInfoClicked() {
QString hostName = m_pEditHost->text();
m_pTextShowInfo->appendPlainText("正在查找主机信息:" + hostName);
QHostInfo::lookupHost(hostName, this, SLOT(slotLookupHostInfo(QHostInfo))); // SLOT方式的槽函数需注明参数类型
}
void Dialog::slotLookupHostInfo(const QHostInfo& host) {
QList<QHostAddress> addList = host.addresses();
if (!addList.isEmpty()) {
for (int i = 0; i < addList.count(); i++) {
QHostAddress hostAddr = addList.at(i);
bool isCheck = m_pCheckIPv4->isChecked();
if (isCheck)
isCheck = (QAbstractSocket::IPv4Protocol == hostAddr.protocol());
else
isCheck = true;
if (isCheck) {
m_pTextShowInfo->appendPlainText("协议:" + protocolType(hostAddr.protocol()));
m_pTextShowInfo->appendPlainText("IP地址:" + hostAddr.toString());
}
}
m_pTextShowInfo->appendPlainText("-----------------------------");
}
}
void Dialog::slotBtnAllInterfaceClicked() {
QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();
if (interfaceList.isEmpty())
return;
for (int i = 0; i < interfaceList.count(); i++) {
QNetworkInterface aInterface = interfaceList.at(i);
if (!aInterface.isValid())
continue;
m_pTextShowInfo->appendPlainText("设备名称:" + aInterface.humanReadableName());
m_pTextShowInfo->appendPlainText("硬件地址:" + aInterface.hardwareAddress() + "\n");
QList<QNetworkAddressEntry> entryList = aInterface.addressEntries();
if (entryList.isEmpty())
return;
for (int i = 0; i < entryList.count(); i++) {
QNetworkAddressEntry aEntry = entryList.at(i);
m_pTextShowInfo->appendPlainText("IP地址:" + aEntry.ip().toString());
m_pTextShowInfo->appendPlainText("子网掩码:" + aEntry.netmask().toString());
m_pTextShowInfo->appendPlainText("广播地址:" + aEntry.broadcast().toString() + "\n");
}
}
m_pTextShowInfo->appendPlainText("-----------------------------");
}
void Dialog::slotBtnAllAddressClicked() {
QList<QHostAddress> addList = QNetworkInterface::allAddresses();
if (addList.isEmpty())
return;
for (int i = 0; i < addList.count(); i++) {
QHostAddress aHost = addList.at(i);
bool show = m_pCheckIPv4->isChecked();
if (show)
show = QAbstractSocket::IPv4Protocol == aHost.protocol();
else
show = true;
if (show) {
m_pTextShowInfo->appendPlainText("协议:" + protocolType(aHost.protocol()));
m_pTextShowInfo->appendPlainText("IP地址:" + aHost.toString());
}
}
m_pTextShowInfo->appendPlainText("-----------------------------");
}
void Dialog::slotClearTextClicked() { m_pTextShowInfo->clear(); }