zoukankan      html  css  js  c++  java
  • Qt-网络与通信-获取本机网络信息

    在网络应用中,经常需要获取本机主机名和IP地址和硬件地址等信息。运用QHostInfo、QNetworkInterface、QNetworkAddressEntry可以获得本机的网络信息。

    上运行截图

    这里需要注意的,在Qt5.80 VS的版本中,有的字符“:”中文版本的,会导致编译错误。

    第一步,需要再pro文件中加入 QT+= network

    .h文件

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QLabel>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QGridLayout>
    #include <QMessageBox>
    #include <QHostInfo>
    #include <QNetworkInterface>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
        void getHostInformation();
    public slots:
        void slotDetail();
    private:
        QLabel *hostLabel;
        QLineEdit *LineEditLocalHostName;
        QLabel *ipLabel;
        QLineEdit *LineEditAddress;
        QPushButton *detailBtn;
        QGridLayout *mainLayout;
    };
    
    #endif // WIDGET_H

    .cpp文件

    #include "widget.h"
    #include <QDebug>
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        hostLabel = new QLabel(tr("主机名:"));
        LineEditLocalHostName = new QLineEdit;
        ipLabel = new QLabel(tr("IP 地址:"));
        LineEditAddress = new QLineEdit;
        detailBtn = new QPushButton(tr("详细"));
        mainLayout = new QGridLayout(this);
        mainLayout->addWidget(hostLabel,0,0);
        mainLayout->addWidget(LineEditLocalHostName,0,1);
        mainLayout->addWidget(ipLabel,1,0);
        mainLayout->addWidget(LineEditAddress,1,1);
        mainLayout->addWidget(detailBtn,2,0,1,2);
        getHostInformation();
        connect(detailBtn,SIGNAL(clicked()),this,SLOT(slotDetail()));
    }
    
    Widget::~Widget()
    {
    
    }
    
    void Widget::getHostInformation()
    {
        QString localHostName = QHostInfo::localHostName();			
        LineEditLocalHostName->setText(localHostName);
        QHostInfo hostInfo = QHostInfo::fromName(localHostName);	
      
        QList<QHostAddress> listAddress = hostInfo.addresses();
    
        qDebug()<<listAddress;
        if(!listAddress.isEmpty())									
        {
            LineEditAddress->setText(listAddress.at(4).toString());
        }
    }
    
    void Widget::slotDetail()
    {
        QString detail="";
        QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
        //(a)
        for(int i=0;i<list.count();i++)
        {
            QNetworkInterface interface=list.at(i);
            detail=detail+tr("设备:")+interface.name()+"
    ";
            //(b)
            detail=detail+tr("硬件地址:")+interface.hardwareAddress()+"
    ";
            //(c)
            QList<QNetworkAddressEntry> entryList=interface.addressEntries();
            //(d)
            for(int j=1;j<entryList.count();j++)
            {
                QNetworkAddressEntry entry=entryList.at(j);
                detail=detail+"	"+tr("IP 地址:")+entry.ip().toString()+"
    ";
                detail=detail+"	"+tr("子网掩码:")+entry.netmask().toString() +"
    ";
                detail=detail+"	"+tr("广播地址:")+entry.broadcast().toString() +"
    ";
            }
        }
        QMessageBox::information(this,tr("Detail"),detail);
    }

    工程地址:https://gitee.com/DreamLife-Technology_DreamLife/NetworkInformation

  • 相关阅读:
    shell脚本编写实例
    正则表达式文本过滤
    bootstrap实现嵌套列
    当鼠标悬停在链接上,或者点击过的链接,颜色会被设置为 #2a6496。同时,会呈现一条下划线。点击过的链接,会呈现一个颜色码为 #333 的细的虚线轮廓。另一条规则是设置轮廓为 5 像素宽,且对于基于 webkit 浏览器有一个 -webkit-focus-ring-color 的浏览器扩展。轮廓偏移设置为 -2 像素
    bootstrap页面效果图
    bootstrap实现菜单定位
    bootstrap三列布局
    bootstrap图片切换效果
    bootstrap导航菜单
    首字下沉
  • 原文地址:https://www.cnblogs.com/DreamDog/p/9159986.html
Copyright © 2011-2022 走看看