Qt5开发从入门到精通——第十二篇三节(Qt5 事件处理及实例——多线程应用
QPushButton \*quitBtn; //退出TimeServer \*timeServer; //TCP 服务器端 timeserver
int 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; /*画出界面的类*/
BtnL