QT制作可访问IP地址的程序

1,主界面

2,代码 

#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
#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(u8"协 议:"+protocolName(aHost.protocol()));
        ui->plainTextEdit->appendPlainText(aHost.toString());
        }
    }
}

void Dialog::on_btnGetHostInfo_clicked()
{//QHostInfo获取主机信息
//    ui->plainTextEdit->clear();

    QString hostName=QHostInfo::localHostName();//本地主机名
    ui->plainTextEdit->appendPlainText(u8"本机主机名:"+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(u8"协 议:"+protocolName(aHost.protocol()));//协议类型
        ui->plainTextEdit->appendPlainText(u8"本机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(u8"协  议:"+protocolName(aHost.protocol()));
        ui->plainTextEdit->appendPlainText(u8"IP地址:"+aHost.toString());
        ui->plainTextEdit->appendPlainText("");
        }
    }
}

void Dialog::on_btnLookup_clicked()
{//查找主机信息
    QString hostname=ui->editHost->text(); //主机名
    ui->plainTextEdit->appendPlainText(u8"正在查找查找主机信息:"+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(u8"设备名称:"+aInterface.humanReadableName());
        ui->plainTextEdit->appendPlainText(u8"硬件地址:"+aInterface.hardwareAddress());
        QList<QNetworkAddressEntry> entryList=aInterface.addressEntries();
        for(int j=0;j<entryList.count();j++)
        {
            QNetworkAddressEntry aEntry=entryList.at(j);
            ui->plainTextEdit->appendPlainText(u8"   IP 地址:"+aEntry.ip().toString());
            ui->plainTextEdit->appendPlainText(u8"   子网掩码:"+aEntry.netmask().toString());
            ui->plainTextEdit->appendPlainText(u8"   广播地址:"+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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁养浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值