QT下QThread学习(二)
学习QThread主要是为了仿照VC下的FTP服务器写个QT版本。不多说,上图。

FTP服务器的软件结构在上面的分析中就已经解释了,今天要解决的就是让每一个客户端的处理过程都可以按一个线程来单独跑。先给出上面3个类的cpp文件,再给出现象。
1、QListenSocket类
#include "qlistensocket.h"
#include <QTcpSocket>
#include <QDebug> QListenSocket::QListenSocket(QObject *parent,int port):QTcpServer(parent)
{
listen(QHostAddress::LocalHost,port);
} void QListenSocket::incomingConnection(int socketDescriptor)
{
qDebug()<<"A new Client Connnected!"; QControlThread *tmp =new QControlThread(socketDescriptor,this);
ClientList.append(tmp); tmp->start();
}
2、QControlThread类
//线程构造函数
QControlThread::QControlThread(qintptr socketDescriptor,QObject *parent):QThread(parent)
{
qDebug()<<"QControlThread Construct Function threadid:"<<QThread::currentThreadId(); m_ControlSocketObj = new QControlSocketObj(socketDescriptor,); m_ControlSocketObj->moveToThread(this); //将m_ControlSocketObj和它的槽函数都移到本线程中来处理
} QControlThread::~QControlThread()
{
delete m_ControlSocketObj; quit();
wait();
deleteLater();
}
3、QControlSocketObj类
#include "qcontrolsocketobj.h"
#include <QDebug>
#include <QThread> QControlSocketObj::QControlSocketObj(qintptr socketDescriptor,QObject *parent) : QObject(parent)
{
m_ControlSocket = new QTcpSocket;
m_ControlSocket->setSocketDescriptor(socketDescriptor); connect(m_ControlSocket,SIGNAL(readyRead()),this,SLOT(dataReceived())); qDebug()<<"QControlSocketObj Construct Function threadid:"<<QThread::currentThreadId();
} void QControlSocketObj::dataReceived()
{
qDebug()<<"QControlSocketObj dataReceived Function threadid:"<<QThread::currentThreadId();
}
显示结果如下:
这是连接的客户端,连接了2个tcp客户端,连接上了以后,给服务器随便发送了些文字。

2、应用程序输出结果。

基本想法达到了,还是有几个地方值得注意。QControlThread的构造函数执行在主线程中,QControlSocketObj的构造函数也执行在主线程中。
QObject::MoveToThread的官方说明是:
Changes the thread affinity for this object and its children. The object cannot be moved if it has a parent. Event processing will continue in the targetThread.
QControlThread对象和QControlSocketObj对象都是在QListenSocket类中被构造的,所以他们的构造函数会在主线程中被执行。
QThread所表示的线程是其成员函数Run执行的代码。run的默认处理就是做消息的接收与处理。
QT下QThread学习(二)的更多相关文章
- Qt Model/View学习(二)
Model和View的搭配使用 DEMO pro文件 #------------------------------------------------- # # Project created by ...
- Docker下kafka学习三部曲之二:本地环境搭建
在上一章< Docker下kafka学习,三部曲之一:极速体验kafka>中我们快速体验了kafka的消息分发和订阅功能,但是对环境搭建的印象仅仅是执行了几个命令和脚本,本章我们通过实战来 ...
- Qt下libusb-win32的使用(转)
源:Qt下libusb-win32的使用(一)打印设备描述符 主要是在前一篇的基础上,学习libusb-win32的API使用.程序很简单,就是打印指定USB设备的设备描述符(当然其他描述符也是可以的 ...
- Qt DLL总结【二】-创建及调用QT的 DLL(三篇)good
目录 Qt DLL总结[一]-链接库预备知识 Qt DLL总结[二]-创建及调用QT的 DLL Qt DLL总结[三]-VS2008+Qt 使用QPluginLoader访问DLL 开发环境:VS20 ...
- 【转】Qt下使用glut库
ps:这个说的很明白,尤其是win10环境下用mingw环境时编程时碰到的问题, 1.加 windows.h 2.在.pro 添加libs 博文地址:Qt下使用glut库 本人使用的环境 ...
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- ReactJS入门学习二
ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...
- Qt之QThread(深入理解)
简述 为了让程序尽快响应用户操作,在开发应用程序时经常会使用到线程.对于耗时操作如果不使用线程,UI界面将会长时间处于停滞状态,这种情况是用户非常不愿意看到的,我们可以用线程来解决这个问题. 前面,已 ...
- 解析Qt中QThread使用方法
本文讲述的是在Qt中QThread使用方法,QThread似乎是很难的一个东西,特别是信号和槽,有非常多的人(尽管使用者本人往往不知道)在用不恰当(甚至错误)的方式在使用QThread,随便用goog ...
随机推荐
- 【Openjudge】岛屿(并查集)
题目链接 此题是并查集.考虑到水位不断上涨,所以将时间倒转.先统计最后一天的联通块个数,每一天浮出水面的块进行计算.复杂度O(玄学). 代码如下 #include<cstdio> #inc ...
- 北京集训TEST16——图片加密(fft+kmp)
题目: Description CJB天天要跟妹子聊天,可是他对微信的加密算法表示担心:“微信这种加密算法,早就过时了,我发明的加密算法早已风靡全球,安全性天下第一!” CJB是这样加密的:设CJB想 ...
- 【二分+扫描线乱搞】B. Producing Snow
注意二分写法... http://codeforces.com/problemset/problem/923/B #include<cstdio> #include<string.h ...
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
实现一.在内部建立内联类实现RowMapper接口 package hysteria.contact.dao.impl; import java.sql.ResultSet; import java. ...
- mongodb的入门学习
mongodb的入门学习 简介: MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库 ...
- codevs——1507 酒厂选址
1507 酒厂选址 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Abstinence(戒酒) ...
- Angular 路由⑦要素
cnzt http://www.cnblogs.com/zt-blog/p/7919185.html http://www.cnblogs.com/zt-blog/p/7919185.ht ...
- 关于ios异步加载图片的几个开源项目
一.HjCache 原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...
- 【spring data jpa】报错如下:Caused by: javax.persistence.EntityNotFoundException: Unable to find com.rollong.chinatower.server.persistence.entity.staff.Department with id 0
报错如下: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.rollong.chi ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...