• Qt5开发从入门到精通——第十二篇三节(Qt5 事件处理及实例——多线程应用、服务器端编程、客户端编程)


    提示:欢迎小伙伴的点评✨✨,相互学习c/c++应用开发。🍳🍳🍳
    博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩‍🚀


    前言

    本章节会给大家带来Qt5 事件处理及实例——多线程应用、服务器端编程、客户端编程详解。通过实现一个多线程的网络时间服务器,介绍如何综合运用多线程技术编程。每当有客户请求到达时,服务器将启动一个新线程为它返回当前的时间,服务完毕后,这个线程将自动退出。同时,用户界面会显示当前已接收请求的次数。


    一、服务器端编程

    1.1、效果实例

    图一
    在这里插入图片描述

    1.2、原码解析

    首先在TimeServer.pro中加入 QT +=network

    QT       += network
    
    • 1

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include 
    #include 
    #include 
    class TimeServer;
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        Dialog(QWidget *parent = 0);
        ~Dialog();
    private:
        QLabel *Label1;      //此标签用于显示监听端口
        QLabel *Label2;      //此标签用于显示请求次数
        QPushButton *quitBtn; //退出
    
        TimeServer *timeServer; //TCP 服务器端 timeserver
        int count; //请求次数计数器 coun
    public slots:
        void slotShow ();
    
    } ;
    
    
    
    #endif // DIALOG_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    timeserver.h

    #ifndef TIMESERVER_H
    #define TIMESERVER_H
    
    #include 
    class Dialog;
    class TimeServer : public QTcpServer
    {
       Q_OBJECT
    public:
        TimeServer(QObject *parent=0);
    protected:
        /*重写此虚函数。这个函数在 TCP服务器端有新的连接时被调用,其参数为所接收新连接的套接字描述符。*/
        void incomingConnection(qintptr socketDescriptor); 
    private:
        /*用于记录创建这个 TCP 服务器端对象的父类,这里是界面指针,通过这个指针将线程发出的消息关联到界面的槽函数中。*/
        Dialog *dig; 
    
    };
    
    #endif // TIMESERVER_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    timethread.h

    #ifndef TIMETHREAD_H
    #define TIMETHREAD_H
    
    #include 
    #include 
    #include 
    class TimeThread : public QThread
    {
        Q_OBJECT
    public:
        TimeThread(qintptr socketDescriptor,QObject *parent=0);
        void run();              //重写此虚函数
    signals:
         void error(QTcpSocket::SocketError socketError); //出错信号
    private:
         qintptr socketDescriptor;  //套接字描述符
    };
    
    #endif // TIMETHREAD_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    dialog.cpp

    #include "dialog.h"
    #include 
    #include 
    #include 
    #include "timeserver.h"
    Dialog::Dialog(QWidget *parent)
        : QDialog(parent)
    {
        setWindowTitle(tr("多线程时间服务器"));
        Label1 =new QLabel(tr(" 服务器端口:"));
        Label2 = new QLabel;
        quitBtn = new QPushButton(tr(" 退出"));
        QHBoxLayout *BtnLayout = new QHBoxLayout;  /*画出界面的类*/
        BtnLayout->addStretch(1); /*addStretch平均分配*/
        BtnLayout->addWidget(quitBtn);
        BtnLayout->addStretch(1);
        QVBoxLayout *mainLayout = new QVBoxLayout(this); /*画出界面的类*/
        mainLayout->addWidget(Label1);
        mainLayout->addWidget(Label2);
        mainLayout->addLayout(BtnLayout);
        connect(quitBtn, SIGNAL (clicked()), this, SLOT (close()));
        count=0;
        timeServer= new TimeServer(this);
        if (!timeServer->listen ())
        {
            QMessageBox::critical(this, tr(" 多线程时间服务器 "),
            tr(" 无法启动服务器: %1.") .arg(timeServer->errorString()));
            close();
            return;
        }
        Label1->setText(tr(" 服务器端口: %1. ") .arg (timeServer->serverPort ()));
    }
    
    Dialog::~Dialog()
    {
    
    }
    /*此槽函数用于界面上显示的请求次数*/
    void Dialog::slotShow()
    {
        Label2->setText(tr(" 第 %1 次请求完毕。") .arg(++count));
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    main.cpp

    #include "dialog.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    timeserver.cpp

    #include "timeserver.h"
    #include "timethread.h"
    #include "dialog.h"
    TimeServer::TimeServer(QObject *parent):QTcpServer(parent)
    {
        dig =(Dialog *)parent;
    }
    void TimeServer::incomingConnection(qintptr socketDescriptor)
    {
        /*以返回的套接字描述符socketDescriptor 创建一个工作线程 TimeThread 。*/
        TimeThread *thread= new TimeThread(socketDescriptor,0); 
        
        /*将上述创建的线程结束消息函数 finished()关联到槽函数 slotShow()用于显示请求计数 。 
         * 此操作中,因为信号是跨线程的,所以使用了排队连接方式 。
         */
        connect (thread, SIGNAL (finished()), dig, SLOT (slotShow ())); 
        
        /*将上述创建的线程结束消息函数 finished()关联到线程自身的槽函数 deleteLater()用于结束线程 。
         * 在此操作中,因为信号是在同 一个线程中的,使用了直接连接方式,故最后一个参数可以省略而使用 Qt 的自动连接选择方式 。
         * 另外,由于工作线程中存在网络事件,所以不能被外界线程销毁,这里使用了延迟销毁函数 deleteLater()保证由工作线程自身销毁 。
         */
        connect (thread, SIGNAL (finished()) , thread, SLOT(deleteLater()),Qt::DirectConnection);
        /*启动上述创建的线程 。 执行此语句后,工作线程 (TimeThread) 的虚函数 run()开始执行。*/
        thread->start();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    timethread.cpp

    #include "timethread.h"
    #include 
    #include 
    #include 
    TimeThread::TimeThread(qintptr socketDescriptor,QObject *parent)
        :QThread(parent),socketDescriptor(socketDescriptor)
    {
    
    }
    void TimeThread::run()
    {
        QTcpSocket tcpSocket;  //创建一个 QTcpSocket 类
        /*将以上创建的 QTcpSocket 类置以从构造函数中传入的套接字描述符,用于向客户端传回服务器端的当前时间。*/
        if(!tcpSocket.setSocketDescriptor(socketDescriptor))
        {
            /*如果出错,则发出 error(tcpSocket.error())信号报告错误。*/
            emit error (tcpSocket.error ());
            return;
        }
        QByteArray block;
        QDataStream out(&block,QIODevice::WriteOnly);
        out.setVersion (QDataStream::Qt_5_11);
        /*如果不出错,则开始获取当前时间。*/
        
        /*此处需要注意的是时间数据的传输格式, Qt 虽然可以很方便地通过 QDateTime 类的静态函
    数 currentDateTime()获取一个时间对象,但类结构是无法直接在网络间传输的,此时需要将它转
    换为一个标准的数据类型后再传输 。 而 QDateTime 类提供了 uint toTime_t() const 函数,这个函
    数返回当前自 1970-01-01 00:00:00 (UNIX 纪元)经过了多少秒,返回值为一个 uint 类型,可以
    将这个值传输给客户端。在客户端方面,使用 QDateTime 类的 void setTime_t(uint seconds)将这
    个时间还原 。*/
        
        uint time2u = QDateTime::currentDateTime().toTime_t(); //(c)
        out<<time2u;
        tcpSocket.write(block);     //将获得的当前时间传回客户端
        tcpSocket.disconnectFromHost(); //断开连接
        tcpSocket.waitForDisconnected(); //等待返回
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    二、客户端编程

    首先在TimeServer.pro中加入 QT +=network

    QT       += network
    
    • 1

    2.1、效果实例

    图二

    在这里插入图片描述

    图三
    在这里插入图片描述

    2.2、原码实例

    timeclient.h

    #ifndef TIMECLIENT_H
    #define TIMECLIENT_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    class TimeClient : public QDialog
    {
        Q_OBJECT
    
    public:
        TimeClient(QWidget *parent = 0);
        ~TimeClient();
    public slots:
        void enableGetBtn();
        void getTime();
        void readTime();
        void showError(QAbstractSocket::SocketError socketError);
    private:
        QLabel *serverNameLabel;
        QLineEdit *serverNameLineEdit;
        QLabel *portLabel;
        QLineEdit  *portLineEdit;
        QDateTimeEdit *dateTimeEdit;
        QLabel *stateLabel;
        QPushButton *getBtn;
        QPushButton *quitBtn;
        uint time2u;
        QTcpSocket *tcpSocket;
    };
    
    #endif // TIMECLIENT_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    main.cpp

    #include "timeclient.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        TimeClient w;
        w.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    timeclient.cpp

    #include "timeclient.h"
    #include 
    #include 
    #include 
    #include 
    #include 
    TimeClient::TimeClient(QWidget *parent)
        : QDialog(parent)
    {
        
        /*初始化界面*/
        setWindowTitle(tr(" 多线程时间服务客户端"));
        serverNameLabel =new QLabel(tr("服务器名:"));
        serverNameLineEdit = new QLineEdit("Localhost");
    
        portLabel =new QLabel(tr(" 端口:"));
        portLineEdit = new QLineEdit;
        QGridLayout *layout= new QGridLayout;
        layout->addWidget(serverNameLabel,0,0);
        layout->addWidget(serverNameLineEdit,0,1);
        layout->addWidget(portLabel,1,0);
        layout->addWidget(portLineEdit,1,1);
        dateTimeEdit = new QDateTimeEdit(this);
        QHBoxLayout *layout1 = new QHBoxLayout;
        layout1->addWidget(dateTimeEdit);
        stateLabel =new QLabel(tr("请首先运行时间服务器"));
        QHBoxLayout *layout2 = new QHBoxLayout;
        layout2->addWidget(stateLabel);
        getBtn = new QPushButton(tr(" 获取时间")) ;
        getBtn->setDefault(true);
        getBtn->setEnabled(false);
        quitBtn = new QPushButton(tr(" 退出")) ;
        QHBoxLayout *layout3 = new QHBoxLayout;
        layout3->addStretch ();
        layout3->addWidget(getBtn);
        layout3->addWidget(quitBtn);
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->addLayout(layout);
        mainLayout->addLayout(layout1);
        mainLayout->addLayout(layout2);
        mainLayout->addLayout(layout3);
        connect(serverNameLineEdit, SIGNAL(textChanged(QString)),this, SLOT (enableGetBtn ()));
        connect(portLineEdit ,SIGNAL(textChanged(QString)),
        this, SLOT (enableGetBtn ()));
        connect (getBtn, SIGNAL (clicked()), this, SLOT (getTime()));
        connect (quitBtn, SIGNAL (clicked()), this, SLOT (close()));
        tcpSocket = new QTcpSocket(this);
        connect (tcpSocket, SIGNAL (readyRead ()) , this, SLOT (readTime())) ;
        connect (tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(showError(QAbstractSocket::SocketError)));
        portLineEdit->setFocus () ;
    }
    
    TimeClient::~TimeClient()
    {
    
    }
    
    /*检查文本框填充的数据是否正常*/
    void TimeClient:: enableGetBtn()
    {
        getBtn->setEnabled(!serverNameLineEdit->text().isEmpty()&&!portLineEdit->text().isEmpty());
    }
    /*连接到服务端*/
    void TimeClient::getTime()
    {
        getBtn->setEnabled(false);
        time2u =0;
        /*QTcpServer释放前需要先close。QTcpSocket释放前需要先abort。*/
        tcpSocket->abort ();
        tcpSocket->connectToHost(serverNameLineEdit->text(),portLineEdit->text ().toInt());
    }
    
    /*从服务端接收到数据时刷新时间*/
    void TimeClient::readTime()
    {
        QDataStream in(tcpSocket);
        in.setVersion(QDataStream::Qt_5_11);
        if(time2u==0)
        {
            if(tcpSocket->bytesAvailable ()<(int)sizeof(uint))
                return;
            in>>time2u;
        }
        dateTimeEdit->setDateTime(QDateTime::fromTime_t(time2u));
        getBtn->setEnabled(true);
    
    }
    
    
    /*当TCP连接异常时,进行报错*/
    void TimeClient::showError(QAbstractSocket::SocketError socketError)
    {
       switch(socketError)
       {
       case QAbstractSocket::RemoteHostClosedError:
            break;
       case QAbstractSocket::HostNotFoundError:
            QMessageBox::information(this, tr(" 时间服务客户端"),tr(" 主机不可达!"));
            break;
       case QAbstractSocket::ConnectionRefusedError:
            QMessageBox::information(this, tr(" 时间服务客户端"),tr(" 连接被拒绝!"));
            break;
       default:
            QMessageBox::information(this, tr(" 时间服务客户端"),tr(" 产生如下错误:%1.") .arg(tcpSocket->errorString()));
       }
       getBtn->setEnabled(true);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    三、多线程优势与特性

    通常情况下,应用程序都是在一个线程中执行操作。但是,当调用一个耗时操作(例如,大批量 I/0 或大量矩阵变换等 CPU 密集操作)时,用户界面常常会冻结。而使用多线程可解决这一问题。
    多线程具有以下优势
    (1) 提高应用程序的响应速度。这对千开发图形界面的程序尤为重要,当一个操作耗时很
    长时,整个系统都会等待这个操作,程序就不能响应键盘、鼠标、菜单等的操作,而使用多线程技术可将耗时长的操作置于一个新的线程,从而避免出现以上问题。
    (2) 使多 CPU 系统更加有效。当线程数不大于 CPU 数目时,操作系统可以调度不同的线
    程运行千不同的 CPU 上。
    (3) 改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为独立或半独立的
    运行部分,这样有利于代码的理解和维护。
    多线程程序具有以下特点。
    (1) 多线程程序的行为无法预期,当多次执行上述程序时,每次的运行结果都可能不同。
    (2) 多线程的执行顺序无法保证,它与操作系统的调度策略和线程优先级等因素有关。
    (3) 多线程的切换可能发生在任何时刻、任何地点。
    (4) 由于多线程对代码的敏感度高,因此对代码的细微修改都可能产生意想不到的结果。
    基于以上这些特点,为了有效地使用线程,开发人员必须对其进行控制。

    四、总结

    Qt5 事件处理及实例——多线程应用、服务器端编程、客户端编程会在应用程序开发中经常用到的。

  • 相关阅读:
    [论文笔记]SiameseNet
    graphviz 绘制红黑树
    PHP 中传值与传引用的区别,什么时候传值什么时候传引用?
    智慧安防:监控防盗两不误的安防视频监控系统是什么样的?
    磷酸除杂回用去除铁、铝、氟
    线程同步之互斥量
    mybatis-plus排坑,心得
    80万奖励!武汉经开区(汉南区)2021年两化融合贯标评定奖励资金申报条件和材料
    CopyOnWriteArrayList源码分析
    java毕业设计茶叶企业管理系统Mybatis+系统+数据库+调试部署
  • 原文地址:https://blog.csdn.net/weixin_44759598/article/details/128061490