Qt5开发从入门到精通——第十二篇三节(Qt5 事件处理及实例——多线程应用、服务器端编程、客户端编程)
创始人
2024-02-19 10:40:57
0

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

文章目录

  • 前言
  • 一、服务器端编程
    • 1.1、效果实例
    • 1.2、原码解析
      • dialog.h
      • timeserver.h
      • timethread.h
      • dialog.cpp
      • main.cpp
      • timeserver.cpp
      • timethread.cpp
  • 二、客户端编程
    • 2.1、效果实例
    • 2.2、原码实例
      • timeclient.h
      • main.cpp
      • timeclient.cpp
  • 三、多线程优势与特性
  • 四、总结


前言

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


一、服务器端编程

1.1、效果实例

图一
在这里插入图片描述

1.2、原码解析

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

QT       += network

dialog.h

#ifndef DIALOG_H
#define DIALOG_H#include 
#include 
#include 
class TimeServer;
class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = 0);~Dialog();
private:QLabel *Label1;      //此标签用于显示监听端口QLabel *Label2;      //此标签用于显示请求次数QPushButton *quitBtn; //退出TimeServer *timeServer; //TCP 服务器端 timeserverint count; //请求次数计数器 coun
public slots:void slotShow ();} ;#endif // DIALOG_H

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

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

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));
}

main.cpp

#include "dialog.h"
#include int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}

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();
}

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<

二、客户端编程

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

QT       += network

2.1、效果实例

图二

在这里插入图片描述

图三
在这里插入图片描述

2.2、原码实例

timeclient.h

#ifndef TIMECLIENT_H
#define TIMECLIENT_H#include 
#include 
#include 
#include 
#include 
#include 
#include 
class TimeClient : public QDialog
{Q_OBJECTpublic: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

main.cpp

#include "timeclient.h"
#include int main(int argc, char *argv[])
{QApplication a(argc, argv);TimeClient w;w.show();return a.exec();
}

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);
}

三、多线程优势与特性

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

四、总结

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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...