提示:欢迎小伙伴的点评✨✨,相互学习c/c++应用开发。🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩🚀
本章节会给大家带来Qt5 事件处理及实例——多线程应用、服务器端编程、客户端编程详解。通过实现一个多线程的网络时间服务器,介绍如何综合运用多线程技术编程。每当有客户请求到达时,服务器将启动一个新线程为它返回当前的时间,服务完毕后,这个线程将自动退出。同时,用户界面会显示当前已接收请求的次数。
图一
首先在TimeServer.pro中加入 QT +=network
QT += network
#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
#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
#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
#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));
}
#include "dialog.h"
#include int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}
#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();
}
#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
图二
图三
#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
#include "timeclient.h"
#include int main(int argc, char *argv[])
{QApplication a(argc, argv);TimeClient w;w.show();return a.exec();
}
#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 事件处理及实例——多线程应用、服务器端编程、客户端编程会在应用程序开发中经常用到的。