联系我们 - 广告服务 - 联系电话:
您的当前位置: > 综合 > > 正文

天天新消息丨Qt 实现主机信息查询

来源:QT教程 时间:2023-05-09 22:36:15


(相关资料图)

protocol() QAbstractSocket::NetworkLayerProtocol类型变量,表示当前IP地址的协议类型 类型如下:

QAbstractSocket::IPv4Protocol

QAbstractSocket::IPv6Protocol

QAbstractSocket::AnyIPProtocol

QAbstractSocket::UnknowNetworkLayerProtocol

toString() 返回IP地址的字符串,表示程序中显示了IP地址列表中每个IP地址的协议类型和IP地址字符串

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include #include #include #include #include namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = nullptr);    ~MainWindow();private slots:    void on_pushButton_clicked();    void on_pushButton_2_clicked();    void on_pushButton_3_clicked();    void on_pushButton_4_clicked();    void on_pushButton_5_clicked();    void lookedUpHostInfo(const QHostInfo &info);private:    Ui::MainWindow *ui;    QString protoclName(QAbstractSocket::NetworkLayerProtocol protocol);};#endif // MAINWINDOW_H#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);}MainWindow::~MainWindow(){    delete ui;}void MainWindow::on_pushButton_clicked(){    //获取主机信息    QString hostname = QHostInfo::localHostName(); //本地主机名    ui->plainTextEdit->appendPlainText("本地主机名:"+hostname+"
");    QHostInfo hostinfo = QHostInfo::fromName(hostname); //本机IP    QListaddList = hostinfo.addresses();// IP地址列表    if(!addList.isEmpty())    {        for(int i =0;icheckBox->isChecked(); //只显示IPV4            if(show)            {                show = (QAbstractSocket::IPv4Protocol == aHost.protocol());            }else {                show = true;            }            if(show)            {                ui->plainTextEdit->appendPlainText("协议:"+protoclName(aHost.protocol()));                ui->plainTextEdit->appendPlainText("本机IP地址:"+aHost.toString());                ui->plainTextEdit->appendPlainText("");            }        }    }}void MainWindow::on_pushButton_2_clicked(){    QListlist = QNetworkInterface::allAddresses();        if(!list.isEmpty())        for(int i =0;icheckBox->isChecked();            if(show)                show=QAbstractSocket::IPv4Protocol == aHost.protocol();            else                show =true;            if(show)            {                ui->plainTextEdit->appendPlainText("协议"+protoclName(aHost.protocol()));                ui->plainTextEdit->appendPlainText("IP地址"+aHost.toString());                ui->plainTextEdit->appendPlainText("");            }        }}void MainWindow::on_pushButton_3_clicked(){    QListlist = QNetworkInterface::allInterfaces();    for(int i=0;iplainTextEdit->appendPlainText("设备名称"+aInterface.humanReadableName());        ui->plainTextEdit->appendPlainText("硬件地址"+aInterface.hardwareAddress());        QListentry = aInterface.addressEntries();        for(int j = 0;jplainTextEdit->appendPlainText("IP地址"+ aEntry.ip().toString());            ui->plainTextEdit->appendPlainText("子网掩码"+ aEntry.netmask().toString());            ui->plainTextEdit->appendPlainText("广播地址"+ aEntry.broadcast().toString() + "
");        }        ui->plainTextEdit->appendPlainText("
");    }}void MainWindow::on_pushButton_4_clicked(){    QString hostname = ui->lineEdit->text(); //主机名    ui->plainTextEdit->appendPlainText("正在查找主机信息"+ hostname);    QHostInfo::lookupHost(hostname,this,SLOT(lookedUpHostInfo(QHostInfo)));}void MainWindow::on_pushButton_5_clicked(){    ui->plainTextEdit->clear();}void MainWindow::lookedUpHostInfo(const QHostInfo & info){    QListaddList = info.addresses();    if(!addList.isEmpty());    for(int i =0;i < addList.count(); i++)    {        QHostAddress aHost = addList.at(i);        bool show = ui->checkBox->isChecked();        if(show)            show = QAbstractSocket::IPv4Protocol ==aHost.protocol();        else            show = true;        if(show)        {            ui->plainTextEdit->appendPlainText("协议"+protoclName(aHost.protocol()));            ui->plainTextEdit->appendPlainText(aHost.toString());        }    }}//根据protocol()返回的QabstractSocket::NetworkLayerProtocol  枚举值显示协议名称字符串QString MainWindow::protoclName(QAbstractSocket::NetworkLayerProtocol protocol){    switch (protocol)    {    case QAbstractSocket::IPv4Protocol:        return "IPV4 Protocol";    case QAbstractSocket::IPv6Protocol:        return "IPV4 Protocol";    case QAbstractSocket::AnyIPProtocol:        return "IPV4 Protocol";    default:        return "Unknow Network Layer Protocol";    }}

【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】

点击这里:

责任编辑:

标签:

相关推荐:

精彩放送:

新闻聚焦
Top