samp14_1.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-17T16:47:43
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = samp14_1
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QHostInfo>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void lookedUpHostInfo(const QHostInfo &host);
//===========================
void on_btnGetHostInfo_clicked();
void on_btnDetail_clicked();
void on_btnLookup_clicked();
void on_btnALLInterface_clicked();
void on_btnClear_clicked();
private:
Ui::Dialog *ui;
QString protocolName(QAbstractSocket::NetworkLayerProtocol protocol);
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QHostInfo>
#include <QNetworkInterface>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::lookedUpHostInfo(const QHostInfo &host)
{//查找主机信息的槽函数
// ui->plainTextEdit->clear();
QList<QHostAddress> addList=host.addresses();//
if (!addList.isEmpty())
for (int i=0;i<addList.count();i++)
{
QHostAddress aHost=addList.at(i);
bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4
if (show)
show=QAbstractSocket::IPv4Protocol==aHost.protocol();
else
show=true;
if (show) {
ui->plainTextEdit->appendPlainText("协 议:"+protocolName(aHost.protocol()));
ui->plainTextEdit->appendPlainText(aHost.toString());
}
}
}
void Dialog::on_btnGetHostInfo_clicked()
{//QHostInfo获取主机信息
// ui->plainTextEdit->clear();
QString hostName=QHostInfo::localHostName();//本地主机名
ui->plainTextEdit->appendPlainText("本机主机名:"+hostName+"\n");
QHostInfo hostInfo=QHostInfo::fromName(hostName); //本机IP地址
QList<QHostAddress> addList=hostInfo.addresses();//IP地址列表
if (!addList.isEmpty())
for (int i=0;i<addList.count();i++)
{
QHostAddress aHost=addList.at(i); //每一项是一个QHostAddress
bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4
if (show)
show=(QAbstractSocket::IPv4Protocol==aHost.protocol()); //协议类型,
else
show=true;
if (show) {
ui->plainTextEdit->appendPlainText("协 议:"+protocolName(aHost.protocol()));//协议类型
ui->plainTextEdit->appendPlainText("本机IP地址:"+aHost.toString()); //IP地址
ui->plainTextEdit->appendPlainText("");
}
}
}
void Dialog::on_btnDetail_clicked()
{//QNetworkInterface::allAddresses()的使用
// ui->plainTextEdit->clear();
QList<QHostAddress> addList=QNetworkInterface::allAddresses();//
if (!addList.isEmpty())
for (int i=0;i<addList.count();i++)
{
QHostAddress aHost=addList.at(i);
bool show=ui->chkOnlyIPv4->isChecked();//只显示IPv4
if (show)
show=QAbstractSocket::IPv4Protocol==aHost.protocol();
else
show=true;
if (show)
{
ui->plainTextEdit->appendPlainText("协 议:"+protocolName(aHost.protocol()));
ui->plainTextEdit->appendPlainText("IP地址:"+aHost.toString());
ui->plainTextEdit->appendPlainText("");
}
}
}
void Dialog::on_btnLookup_clicked()
{//查找主机信息
QString hostname=ui->editHost->text(); //主机名
ui->plainTextEdit->appendPlainText("正在查找查找主机信息:"+hostname);
QHostInfo::lookupHost(hostname,this,SLOT(lookedUpHostInfo(QHostInfo)));
}
void Dialog::on_btnALLInterface_clicked()
{//QNetworkInterface::allInterfaces()函数的使用
// ui->plainTextEdit->clear();
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
for(int i=0;i<list.count();i++)
{
QNetworkInterface aInterface=list.at(i);
if (!aInterface.isValid())
continue;
ui->plainTextEdit->appendPlainText("设备名称:"+aInterface.humanReadableName());
ui->plainTextEdit->appendPlainText("硬件地址:"+aInterface.hardwareAddress());
QList<QNetworkAddressEntry> entryList=aInterface.addressEntries();
for(int j=0;j<entryList.count();j++)
{
QNetworkAddressEntry aEntry=entryList.at(j);
ui->plainTextEdit->appendPlainText(" IP 地址:"+aEntry.ip().toString());
ui->plainTextEdit->appendPlainText(" 子网掩码:"+aEntry.netmask().toString());
ui->plainTextEdit->appendPlainText(" 广播地址:"+aEntry.broadcast().toString()+"\n");
}
ui->plainTextEdit->appendPlainText("\n");
}
}
QString Dialog::protocolName(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 "Unknown Network Layer Protocol";
}
}
void Dialog::on_btnClear_clicked()
{
ui->plainTextEdit->clear();
}
dialog.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>466</width>
<height>364</height>
</rect>
</property>
<property name="windowTitle">
<string>网络主机信息</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="btnGetHostInfo">
<property name="text">
<string>QHostInfo 获取本机主机名和IP地址</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="btnDetail">
<property name="text">
<string>QNetworkInterface ::allAddresses()</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="chkOnlyIPv4">
<property name="text">
<string>只显示IPv4 协议地址</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnALLInterface">
<property name="text">
<string>QNetworkInterface::allInterfaces()</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="btnLookup">
<property name="text">
<string>QHostInfo 查找域名的IP地址</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="editHost">
<property name="text">
<string>www.163.com</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="btnClear">
<property name="text">
<string>清空文本框</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
源码:https://download.csdn.net/download/qq_30457077/85075848