diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..bba77c3 Binary files /dev/null and b/.DS_Store differ diff --git a/QtQHostInfoEx/ExQHostInfo.cpp b/QtQHostInfoEx/ExQHostInfo.cpp index 4bcf7b6..79f5de8 100755 --- a/QtQHostInfoEx/ExQHostInfo.cpp +++ b/QtQHostInfoEx/ExQHostInfo.cpp @@ -33,20 +33,78 @@ QString ExQHostInfo::protocolName(QAbstractSocket::NetworkLayerProtocol protocol } } - +//QHostInfo 获取主机信息 void ExQHostInfo::on_btnGetHostInfo_clicked() { + QString hostName = QHostInfo::localHostName(); + ui->plainTextEdit->appendPlainText("本地主机名称:" + hostName + "\n"); + QHostInfo hostInfo = QHostInfo::fromName(hostName); + QList list = hostInfo.addresses(); + + if (list.isEmpty()) + return; + + foreach (QHostAddress var, list) { + bool bIPv4 = ui->checkBox->isChecked(); + + if (bIPv4) { //只显示 IPv4 + bIPv4 = QAbstractSocket::IPv4Protocol == var.protocol(); + } else { + bIPv4 = true; //显示 IPv4 和 IPv6 + } + + if (bIPv4) { + ui->plainTextEdit->appendPlainText("协议:" + protocolName(var.protocol())); + ui->plainTextEdit->appendPlainText("本机IP地址" + var.toString() + "\n"); + } + } } void ExQHostInfo::on_btnAllAddresses_clicked() { + QList list = QNetworkInterface::allAddresses(); + if (list.isEmpty()) + return; + + foreach (QHostAddress var, list) { + bool bIPv4 = ui->checkBox->isChecked(); + + if (bIPv4) { //只显示 IPv4 + bIPv4 = QAbstractSocket::IPv4Protocol == var.protocol(); + } else { + bIPv4 = true; //显示 IPv4 和 IPv6 + } + + if (bIPv4) { + ui->plainTextEdit->appendPlainText("协议:" + protocolName(var.protocol())); + ui->plainTextEdit->appendPlainText("本机IP地址" + var.toString() + "\n"); + } + } } void ExQHostInfo::on_btnAllInterfaces_clicked() { + QList list = QNetworkInterface::allInterfaces(); + if (list.isEmpty()) + return; + + foreach (QNetworkInterface var, list) { + if (!var.isValid()) + continue; + + ui->plainTextEdit->appendPlainText("设备名称:" + var.humanReadableName()); + ui->plainTextEdit->appendPlainText("硬件地址:" + var.hardwareAddress()); + + QList entry = var.addressEntries(); + foreach (QNetworkAddressEntry ent, entry) { + ui->plainTextEdit->appendPlainText(" IP 地址:" + ent.ip().toString()); + ui->plainTextEdit->appendPlainText(" 子网掩码:" + ent.netmask().toString()); + ui->plainTextEdit->appendPlainText(" 子网广播:" + ent.broadcast().toString() + "\n"); + } + } } void ExQHostInfo::on_btnFindIP_clicked() @@ -73,17 +131,16 @@ void ExQHostInfo::onLookedUpHostInfo(const QHostInfo &host) QHostAddress host = list.at(i); bool bIpv4 = ui->checkBox->isChecked(); //只显示IPv4 - if (bIpv4) { + if (bIpv4) { //只显示 IPv4 bIpv4 = QAbstractSocket::IPv4Protocol == host.protocol(); } else { - bIpv4 = false; + bIpv4 = true; //显示 IPv4 和 IPv6 } if (bIpv4) { ui->plainTextEdit->appendPlainText("协议:" + protocolName(host.protocol())); ui->plainTextEdit->appendPlainText(host.toString()); } - } } diff --git a/QtQHostInfoEx/ExQHostInfo.h b/QtQHostInfoEx/ExQHostInfo.h index ecd4705..25b6c88 100755 --- a/QtQHostInfoEx/ExQHostInfo.h +++ b/QtQHostInfoEx/ExQHostInfo.h @@ -22,16 +22,13 @@ private: QString protocolName(QAbstractSocket::NetworkLayerProtocol protocol); //通过协议类型返回协议名称 private slots: - void on_btnGetHostInfo_clicked(); - void on_btnAllAddresses_clicked(); - void on_btnAllInterfaces_clicked(); - void on_btnFindIP_clicked(); - void on_btnClean_clicked(); - - void onLookedUpHostInfo(const QHostInfo& host); - - + void on_btnGetHostInfo_clicked(); //QHostInfo查询主机名和IP + void on_btnAllAddresses_clicked(); //QNetworkInterface::allAddresses() + void on_btnAllInterfaces_clicked(); //QNetworkInterface::allInterfaces() + void on_btnFindIP_clicked(); //QHostInfo查询左侧域名IP地址 + void on_btnClean_clicked(); //清空文本框信息 + void onLookedUpHostInfo(const QHostInfo& host); //查询主机信息的槽函数 private: Ui::ExQHostInfo *ui;