其他分享
首页 > 其他分享> > Qt QHostInfo详解

Qt QHostInfo详解

作者:互联网

1.QHostInfo

主要用来查询主机信息,包括主机名,ip地址,DNS域名等信息。

以下常用静态方法

以下常用方法

2.示例

获取本机的主机信息和获取百度域名信息

//.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QHostInfo>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    QString protocolName(QAbstractSocket::NetworkLayerProtocol protocol);

private slots:
    void on_btnGetHostInfo_clicked();

    void on_btnGetIp_clicked();

    void on_btnClear_clicked();

    void on_lookUpHostInfo(const QHostInfo&);

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
//.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

QString Widget::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 Widget::on_btnGetHostInfo_clicked()
{
    //本地主机名
    QString hostName = QHostInfo::localHostName();
    ui->plainTextEdit->appendPlainText("本地主机名: "+ hostName + "\n");

    //本机IP地址
    QHostInfo hostInfo = QHostInfo::fromName(hostName);

    //IP地址列表
    QList<QHostAddress> addrList = hostInfo.addresses();
    for(int i=0;i<addrList.count();i++)
    {
        QHostAddress host = addrList.at(i);

        bool isShow = ui->checkBox->isChecked();
        if(isShow)
            isShow = (QAbstractSocket::IPv4Protocol == host.protocol()); //IPv4协议
        else
            isShow = true;
        if(isShow)
        {
            ui->plainTextEdit->appendPlainText("协议: "+protocolName(host.protocol()));
            ui->plainTextEdit->appendPlainText("本机IP地址: "+host.toString());
            ui->plainTextEdit->appendPlainText("");
        }
    }
}

void Widget::on_btnGetIp_clicked()
{
    //主机名
    QString hostName = ui->lineEdit->text();

    ui->plainTextEdit->appendPlainText("正在查询主机信息: "+ hostName);
    QHostInfo::lookupHost(hostName,this,SLOT(on_lookUpHostInfo(QHostInfo)));
}

void Widget::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_lookUpHostInfo(const QHostInfo &host)
{
    //IP地址列表
    QList<QHostAddress> addrList = host.addresses();
    for(int i=0;i<addrList.count();i++)
    {
        QHostAddress host = addrList.at(i);

        bool isShow = ui->checkBox->isChecked();
        if(isShow)
            isShow = (QAbstractSocket::IPv4Protocol == host.protocol()); //IPv4协议
        else
            isShow = true;
        if(isShow)
        {
            ui->plainTextEdit->appendPlainText("协议: "+protocolName(host.protocol()));
            ui->plainTextEdit->appendPlainText("本机IP地址: "+host.toString());
            ui->plainTextEdit->appendPlainText("");
        }
    }
}

 

标签:Widget,Qt,QHostInfo,appendPlainText,isShow,详解,ui,plainTextEdit
来源: https://blog.csdn.net/wzz953200463/article/details/114951013