Qt学习之网络编程(一)

一些说明

学了有一段时间的python了,小项目做了不少,最近由于项目需要,所以要回归老本行了,开始重点突击C++和qt。python的网络爬虫系列有时间就更吧。

获取本机网络信息

在网络应用中,经常需要用到本机的主机名、IP地址、MAC地址等网络信息,通常通过调出命令行窗口输入ipconfig(Windows)或者ifconfig(Linux)就可以查看相关信息了,在这里我们利用qt作出一个可以查询的界面和功能出来,为了后面的网络编程打下一个简单的基础。

创建界面

创建一个Qt Widget项目,生成相关代码如下图:
01.png
然后我们开始编写头文件,在Qt里头文件的用处是申明相关函数。

#ifndef NETWORK2_H
#define NETWORK2_H

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>

class Network2 : public QWidget
{
    Q_OBJECT

public:
    Network2(QWidget *parent = 0);
    ~Network2();   //析构函数
    
private:
//声明所有的组件
    QLabel *hostLabel;
    QPushButton *detailButton;
    QLabel *ipLabel;
    QlineEdit *LineEditLocalHostName;
    QLineEdit *LineEditAddress;
//声明组件的排序方式为GridLayout,即表格放置方式,这里只用指定坐标即可将组件按布局放置。   
    QGridLayout *mainLayout;
};

#endif // NETWORK2_H

生成的结果如下:
02.png
然后在network2.cpp里实现所有组件的布局:

#include "network2.h"

Network2::Network2(QWidget *parent)
    : QWidget(parent)
{
    hostLabel = new QLabel(tr("主机名:"));
    ipLabel = new QLabel(tr("ip地址:"));

    LineEditLocalHostName = new QLineEdit;
    LineEditAddress = new QLineEdit;

    detailButton = 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(detailButton,2,0,1,2);

}

Network2::~Network2()
{

}
注意到`Network2::Network2(QWidget *parent)
QWidget(parent),这里的parent表示在这里已经创建了一个父对象,程序后面有一个 mainLayout = new QGridLayout(this);`
this指针的意思就是这个布局依附在这个父组件主窗口上成为子组件,这样讲或许比较抽象,利用图表进行说明就很形象生动。
03.png
还有一点就是在main函数中,要用已经创建的类network2 w。

功能实现

首先在qt的.pro文件中添加QT += network,表示启用了qt的网络功能。加上后,Qt会自动将与network相关的头文件引入。
在头文件中再增加功能代码:

#include <QHostInfo>
#include <QNetworkInterface>
public:
    void getHostInformation();
public slots:
    void slotDetail();

再在network2.cpp文件中实现功能,main.app目前只是负责将窗口显现,具体的功能还是需要在network2.cpp实现。下面编写network2.cpp部分的代码:

void Network2::getHostInformation()
{
    QString localHostName = QHostInfo::localHostName();//通过QHostInfo的localHostName函数获取主机名称并以字符串的形式存入localHostName
    LineEditLocalHostName->setText(localHostName);//加到先前空的表单中

    QHostInfo hostInfo = QHostInfo::fromName(localHostName); //通过主机名查询ip地址
    QList<QHostAddress> listAddress = hostInfo.addresses(); //从localHostName中得到addresses存入listAddress中

    if(!listAddress.isEmpty())
    {
         LineEditAddress->setText(listAddress.at(2).toString());
    }
}

void Network2::slotDetail()
{
    QString detail="";
    QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();

    for(int i=0;i<list.count();i++)
    {
        QNetworkInterface interface = list.at(i);
        detail = detail+tr("设备:")+interface.name()+"
";
        detail = detail+tr("硬件地址:")+interface.hardwareAddress()+"
";
        QList<QNetworkAddressEntry> entryList=interface.addressEntries();
        for(int j=0;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);
}

其中,interface.name()为获得网络接口的名称;interface.hardwareAddress()为网络接口的硬件地址;interface.addressEntries()为每个网络接口所对应的ip地址,包括其中的相关的子网掩码和广播地址。

结果图

04.png

可以说Qt中关于网络的部分已经有了大量的封装库,实现起来还是比较简单。下一篇文章将介绍利用Qt做聊天室。

作者:YunLambert

-------------------------------------------

个性签名:一名会音乐、爱健身的不合格程序员

可以Follow博主的Github哦(っ•̀ω•́)っ✎⁾⁾

原文地址:https://www.cnblogs.com/yunlambert/p/8547127.html