Qt4.7中,线程,信号,事件的一点理解
来源:互联网 发布:网络专线费用 编辑:程序博客网 时间:2024/04/27 17:43
这几天在学线程,觉得不错就转载一下。
首先,写个线程类,继承自QThread,该线程做的事情很简单:每两秒打印一次自己的线程id,由于我对Qt的console打印函数不太了解,这里还是使用c++的cout!
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
- #include <QThread>
- #include <iostream>
- using namespace std;
- class Mythread : public QThread
- {
- Q_OBJECT
- public:
- explicit Mythread(QObject *parent = 0);
- signals:
- public slots:
- protected:
- void run();
- };
- #endif // MYTHREAD_H
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>#include <iostream>using namespace std;class Mythread : public QThread{ Q_OBJECTpublic: explicit Mythread(QObject *parent = 0);signals:public slots:protected:void run();};#endif // MYTHREAD_H
- #include "mythread.h"
- Mythread::Mythread(QObject *parent) :
- QThread(parent)
- {
- }
- void Mythread::run()
- {
- while(1)
- {
- cout << "thread id: " << QThread::currentThreadId() << endl;
- sleep(2);
- }
- }
#include "mythread.h"Mythread::Mythread(QObject *parent) : QThread(parent){}void Mythread::run(){while(1){cout << "thread id: " << QThread::currentThreadId() << endl;sleep(2);}}
- #include <QtCore/QCoreApplication>
- #include "mythread.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- cout << "main thread id:" << QThread::currentThreadId() << endl;
- Mythread thread;
- thread.start();
- return a.exec();
- }
#include <QtCore/QCoreApplication>#include "mythread.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); cout << "main thread id:" << QThread::currentThreadId() << endl; Mythread thread; thread.start(); return a.exec();}
这是一个Qt console进程,好了,运行一下,每隔两秒打印一次子线程id。
接下来,我们给线程设置定时器,来替换sleep
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
- #include <QThread>
- #include <QTimer>
- #include <iostream>
- using namespace std;
- class Mythread : public QThread
- {
- Q_OBJECT
- public:
- explicit Mythread(QObject *parent = 0);
- signals:
- public slots:
- void mytimedout();
- protected:
- void run();
- QTimer _timer;
- };
- #endif // MYTHREAD_H
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>#include <QTimer>#include <iostream>using namespace std;class Mythread : public QThread{ Q_OBJECTpublic: explicit Mythread(QObject *parent = 0);signals:public slots:void mytimedout();protected:void run();QTimer _timer;};#endif // MYTHREAD_H
- #include "mythread.h"
- Mythread::Mythread(QObject *parent) :
- QThread(parent)
- {
- }
- void Mythread::run()
- {
- connect(&_timer, SIGNAL(timeout()), this, SLOT(mytimedout()));
- cout << "child thread id: " << QThread::currentThreadId() << endl;
- _timer.start(2000);
- exec();
- }
- void Mythread::mytimedout()
- {
- cout << "thread id: " << QThread::currentThreadId() << endl;
- }
#include "mythread.h"Mythread::Mythread(QObject *parent) : QThread(parent){}void Mythread::run(){connect(&_timer, SIGNAL(timeout()), this, SLOT(mytimedout()));cout << "child thread id: " << QThread::currentThreadId() << endl;_timer.start(2000);exec();}void Mythread::mytimedout(){cout << "thread id: " << QThread::currentThreadId() << endl;}
main与以前相同
发现并不能正常隔两秒打印一次。根据我查的资料,大概原因是:QTimer是在主线程里创建的,它发的信号只能与主线程相关的QObject来处理。详细信息还需要细看文档。
修改代码,我们在子线程里创建一个QTimer,并用它来发超时信号,子线程会阻塞在exec()这里,所以不用担心mytimer的生存期
将run改为如下:
- void Mythread::run()
- {
- QTimer mytimer;
- connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));
- cout << "child thread id: " << QThread::currentThreadId() << endl;
- mytimer.start(2000);
- exec();
- }
void Mythread::run(){QTimer mytimer;connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));cout << "child thread id: " << QThread::currentThreadId() << endl;mytimer.start(2000);exec();}
此时可以打印了,不过,我们发现打印的时候,显示的是主线程id,也就是说,mytimedout这个函数是在主线程里执行的。根据所查资料,这里大概是因为:工作线程在创建的时候,线程的事件处理,是依附在创建者身上的,我们需要在创建之后,调用QObject::moveToThread()来改变依附性,
main修改如下:
- #include <QtCore/QCoreApplication>
- #include "mythread.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- cout << "main thread id:" << QThread::currentThreadId() << endl;
- Mythread thread;
- thread.start();
- thread.moveToThread(&thread);
- return a.exec();
- }
#include <QtCore/QCoreApplication>#include "mythread.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv);cout << "main thread id:" << QThread::currentThreadId() << endl;Mythread thread;thread.start();thread.moveToThread(&thread); return a.exec();}
这时候,便由子线程自己来打印了
接下来看下事件,QEvent!我们让子线程往主线程发送QEvent,主线程接受到后,打印信息表示接收到了。
要让主线程可以处理自定义事件,需要从QCoreApplication继承一个类,并改写虚函数event(QEvent *eve)
- #ifndef MYAPP_H
- #define MYAPP_H
- #include <QCoreApplication>
- #include <mythread.h>
- class Myapp : public QCoreApplication
- {
- Q_OBJECT
- public:
- explicit Myapp(int argc, char* argv[], QObject *parent = 0);
- bool event( QEvent * e );
- signals:
- public slots:
- };
- #endif // MYAPP_H
#ifndef MYAPP_H#define MYAPP_H#include <QCoreApplication>#include <mythread.h>class Myapp : public QCoreApplication{ Q_OBJECTpublic:explicit Myapp(int argc, char* argv[], QObject *parent = 0);bool event( QEvent * e );signals:public slots:};#endif // MYAPP_H
- #include "myapp.h"
- Myapp::Myapp(int argc, char* argv[], QObject *parent) :
- QCoreApplication(argc, argv)
- {
- }
- bool Myapp::event( QEvent * e )
- {
- if(e->type() == (mytype1)){
- cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;
- }
- return QCoreApplication::event(e);
- }
#include "myapp.h"Myapp::Myapp(int argc, char* argv[], QObject *parent) :QCoreApplication(argc, argv){}bool Myapp::event( QEvent * e ){if(e->type() == (mytype1)){cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;}return QCoreApplication::event(e);}
子线程里,发送事件
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
- #include <QThread>
- #include <QTimer>
- #include <QEvent>
- #include <iostream>
- using namespace std;
- enum Type{ mytype1 = QEvent::User+1, mytype2 }; // 这是我自定义的事件类型。这里有个问题,可能是qt的bug,如果我编译成功了,然后改mytype1=QEvent::User,再编译运行,就会收不到事件
- class Mythread : public QThread
- {
- Q_OBJECT
- public:
- explicit Mythread(QObject *parent = 0);
- signals:
- public slots:
- void mytimedout();
- protected:
- void run();
- QTimer _timer;
- };
- #endif // MYTHREAD_H
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>#include <QTimer>#include <QEvent>#include <iostream>using namespace std;enum Type{ mytype1 = QEvent::User+1, mytype2 }; // 这是我自定义的事件类型。这里有个问题,可能是qt的bug,如果我编译成功了,然后改mytype1=QEvent::User,再编译运行,就会收不到事件class Mythread : public QThread{ Q_OBJECTpublic: explicit Mythread(QObject *parent = 0);signals:public slots:void mytimedout();protected:void run();QTimer _timer;};#endif // MYTHREAD_H
- #include <QCoreApplication>
- #include "mythread.h"
- Mythread::Mythread(QObject *parent) :
- QThread(parent)
- {
- }
- void Mythread::run()
- {
- QTimer mytimer;
- connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));
- cout << "child thread id: " << QThread::currentThreadId() << endl;
- mytimer.start(2000);
- exec();
- }
- void Mythread::mytimedout()
- {
- cout << "thread id: " << QThread::currentThreadId() << endl;
- QEvent *eve = new QEvent((QEvent::Type)(mytype1));
- QCoreApplication::postEvent(QCoreApplication::instance(), eve);
- }
#include <QCoreApplication>#include "mythread.h"Mythread::Mythread(QObject *parent) : QThread(parent){}void Mythread::run(){QTimer mytimer;connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));cout << "child thread id: " << QThread::currentThreadId() << endl;mytimer.start(2000);exec();}void Mythread::mytimedout(){cout << "thread id: " << QThread::currentThreadId() << endl;QEvent *eve = new QEvent((QEvent::Type)(mytype1));QCoreApplication::postEvent(QCoreApplication::instance(), eve);}
- #include <QtCore/QCoreApplication>
- #include "mythread.h"
- #include "myapp.h"
- int main(int argc, char *argv[])
- {
- Myapp a(argc, argv);
- cout << "reg value:" << QEvent::registerEventType(mytype1) << endl; // 这里注册一下,看该类型的事件是否被别人注册过了,避免混淆
- cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;
- cout << "main thread id:" << QThread::currentThreadId() << endl;
- Mythread thread;
- thread.start();
- thread.moveToThread(&thread);
- return a.exec();
- }
#include <QtCore/QCoreApplication>#include "mythread.h"#include "myapp.h"int main(int argc, char *argv[]){Myapp a(argc, argv);cout << "reg value:" << QEvent::registerEventType(mytype1) << endl; // 这里注册一下,看该类型的事件是否被别人注册过了,避免混淆cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;cout << "main thread id:" << QThread::currentThreadId() << endl;Mythread thread;thread.start();thread.moveToThread(&thread); return a.exec();}
我们在Mythread::mytimedout()里new 了一个QEvnet对象,却没有去delete它,那么它会内存泄漏吗?其实不会,因为这个事件被发出去后,处理该事件的对象在处理完该事件后,会delete它,比如这里是
- bool Myapp::event( QEvent * e )
- {
- if(e->type() == (mytype1)){
- cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;
- }
- return QCoreApplication::event(e);
- }
bool Myapp::event( QEvent * e ){if(e->type() == (mytype1)){cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;}return QCoreApplication::event(e);}return QCoreApplication::event(e); 这里是转给父类去处理,估计最终会由QObject的event函数来delete掉。
我们可以自己写个类继承自QEvent,QEvent的析构函数为虚。我们的类继承它,别人delete这个类时,除了会执行自己的析构函数以外,也会调用QEvent的析构函数。
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
- #include <QThread>
- #include <QTimer>
- #include <QEvent>
- #include <iostream>
- using namespace std;
- enum Type{ mytype1 = QEvent::User+1, mytype2 };
- class Myevent:public QEvent
- {
- public:
- Myevent(QEvent::Type type):QEvent(type){}
- ~Myevent(){
- cout << "thread id: " << QThread::currentThreadId() << "destroy event, type is:" << type() << endl;
- }
- };
- class Mythread : public QThread
- {
- Q_OBJECT
- public:
- explicit Mythread(QObject *parent = 0);
- signals:
- public slots:
- void mytimedout();
- protected:
- void run();
- QTimer _timer;
- };
- #endif // MYTHREAD_H
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>#include <QTimer>#include <QEvent>#include <iostream>using namespace std;enum Type{ mytype1 = QEvent::User+1, mytype2 };class Myevent:public QEvent{public:Myevent(QEvent::Type type):QEvent(type){}~Myevent(){cout << "thread id: " << QThread::currentThreadId() << "destroy event, type is:" << type() << endl;}};class Mythread : public QThread{ Q_OBJECTpublic: explicit Mythread(QObject *parent = 0);signals:public slots:void mytimedout();protected:void run();QTimer _timer;};#endif // MYTHREAD_H
- void Mythread::mytimedout()
- {
- cout << "thread id: " << QThread::currentThreadId() << endl;
- QEvent *eve = new Myevent((QEvent::Type)(mytype1));
- QCoreApplication::postEvent(QCoreApplication::instance(), eve);
- }
void Mythread::mytimedout(){cout << "thread id: " << QThread::currentThreadId() << endl;QEvent *eve = new Myevent((QEvent::Type)(mytype1));QCoreApplication::postEvent(QCoreApplication::instance(), eve);}
可以看到Myapp的线程在处理完事件后,delete掉了事件,因为调用了Myevent的析构函数。
最后,我们让父类接收到mytype1事件后,往子线程发一个mytype2事件。
类Mythread需要重载event方法
- bool Mythread::event( QEvent * e )
- {
- switch(e->type())
- {
- case mytype2:
- cout << "child thread:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;
- break;
- default:
- break;
- }
- return QThread::event(e);
- }
bool Mythread::event( QEvent * e ){switch(e->type()){case mytype2:cout << "child thread:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;break;default:break;}return QThread::event(e);}
Myapp的event变为
- bool Myapp::event( QEvent * e )
- {
- if(e->type() == (mytype1)){
- cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;
- QEvent *eve = new Myevent((QEvent::Type)mytype2);
- postEvent(_recv, eve);
- }
- return QCoreApplication::event(e);
- }
bool Myapp::event( QEvent * e ){if(e->type() == (mytype1)){cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;QEvent *eve = new Myevent((QEvent::Type)mytype2);postEvent(_recv, eve);}return QCoreApplication::event(e);}
这里_recv是子线程的QObject指针
- void setReceive(QObject *obj){
- _recv = obj;
- }
- protected:
- QObject *_recv;
void setReceive(QObject *obj){_recv = obj;}protected:QObject *_recv;
main函数
- #include <QtCore/QCoreApplication>
- #include "mythread.h"
- #include "myapp.h"
- int main(int argc, char *argv[])
- {
- Myapp a(argc, argv);
- cout << "reg value:" << QEvent::registerEventType(mytype1) << endl;
- cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;
- cout << "main thread id:" << QThread::currentThreadId() << endl;
- Mythread thread;
- a.setReceive(&thread);
- thread.start();
- thread.moveToThread(&thread);
- return a.exec();
- }
#include <QtCore/QCoreApplication>#include "mythread.h"#include "myapp.h"int main(int argc, char *argv[]){Myapp a(argc, argv);cout << "reg value:" << QEvent::registerEventType(mytype1) << endl;cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;cout << "main thread id:" << QThread::currentThreadId() << endl;Mythread thread;a.setReceive(&thread);thread.start();thread.moveToThread(&thread); return a.exec();}
这样,子线程每2秒处理一个超时信号,打印相关信息和往主线程发事件mytype1,主线程接收到该事件后,往子线程发一个mytype2的事件。子线程处理mytype2事件,打印信息
注意:
- thread.moveToThread(&thread) // 这不是一个好的方法,这里作为例子这么用可以更快速让例子运行起来
thread.moveToThread(&thread) // 这不是一个好的方法,这里作为例子这么用可以更快速让例子运行起来
参考:
http://www.qtcn.org/bbs/simple/?t32303.html
http://www.cnblogs.com/tankery/archive/2011/03/09/2004561.html
http://www.cnblogs.com/andreitang/archive/2011/08/03/2125815.html
- Qt4.7中,线程,信号,事件的一点理解
- Qt4.7中,线程,信号,事件的一点理解
- C#中线程锁的一点理解
- Qt4线程间的信号与槽通信
- 线程池的一点理解
- 关于进程和线程的一点理解
- QT4的键盘事件
- 关于C#.Net中事件(event)、委托(delegate)和方法的一点理解
- Qt中信号与事件的小结
- QT中信号函数的理解
- qt4典型的信号槽的实现
- linuxc线程信号-pthread_cond_wait理解
- nodejs单线程、异步事件的理解
- 对java中synchronized的一点理解
- 关于openlayers中namespace的一点理解
- ios中MVC的一点理解
- C#中foreach的一点理解
- mpq中hash表的一点理解
- 表达式求值(char)
- android layout_weight讲解
- s3c2440 MPLL & UPLL
- Deploy Puppet on CentOS 5
- 常见typedef 用法
- Qt4.7中,线程,信号,事件的一点理解
- Linux下压缩解压缩命令
- Java 模板技术
- 可能你不知道的TClientDataset功能
- usaco Party Lamps
- android中的weight
- slab slub和内存管理
- 我喜欢大海
- 【网站制作视频教程】-第01讲 html标记语法基础!-传智播客