Qt4.7中,线程,信号,事件的一点理解

来源:互联网 发布:网络专线费用 编辑:程序博客网 时间:2024/04/27 17:43

这几天在学线程,觉得不错就转载一下。

 

首先,写个线程类,继承自QThread,该线程做的事情很简单:每两秒打印一次自己的线程id,由于我对Qt的console打印函数不太了解,这里还是使用c++的cout!

[cpp] view plaincopyprint?
  1. #ifndef MYTHREAD_H   
  2. #define MYTHREAD_H   
  3.   
  4. #include <QThread>   
  5. #include <iostream>   
  6. using namespace std;  
  7.   
  8. class Mythread : public QThread  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     explicit Mythread(QObject *parent = 0);  
  13.   
  14. signals:  
  15.   
  16. public slots:  
  17.   
  18. protected:  
  19.     void run();  
  20.   
  21. };  
  22.   
  23. #endif // MYTHREAD_H  

[cpp] view plaincopyprint?
  1. #include "mythread.h"   
  2.   
  3. Mythread::Mythread(QObject *parent) :  
  4.     QThread(parent)  
  5. {  
  6. }  
  7.   
  8. void Mythread::run()  
  9. {  
  10.     while(1)  
  11.     {  
  12.         cout << "thread id: " << QThread::currentThreadId() << endl;  
  13.         sleep(2);  
  14.     }  
  15. }  

[cpp] view plaincopyprint?
  1. #include <QtCore/QCoreApplication>  
  2. #include "mythread.h"   
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QCoreApplication a(argc, argv);  
  7.   
  8.     cout << "main thread id:" << QThread::currentThreadId() << endl;  
  9.   
  10.     Mythread thread;  
  11.     thread.start();  
  12.   
  13.     return a.exec();  
  14. }  

这是一个Qt console进程,好了,运行一下,每隔两秒打印一次子线程id。


接下来,我们给线程设置定时器,来替换sleep

[cpp] view plaincopyprint?
  1. #ifndef MYTHREAD_H   
  2. #define MYTHREAD_H   
  3.   
  4. #include <QThread>   
  5. #include <QTimer>   
  6. #include <iostream>   
  7. using namespace std;  
  8.   
  9. class Mythread : public QThread  
  10. {  
  11.     Q_OBJECT  
  12. public:  
  13.     explicit Mythread(QObject *parent = 0);  
  14.   
  15. signals:  
  16.   
  17. public slots:  
  18.     void mytimedout();  
  19.   
  20. protected:  
  21.     void run();  
  22.   
  23.     QTimer _timer;  
  24.   
  25. };  
  26.   
  27. #endif // MYTHREAD_H  

[cpp] view plaincopyprint?
  1. #include "mythread.h"   
  2.   
  3. Mythread::Mythread(QObject *parent) :  
  4.     QThread(parent)  
  5. {  
  6. }  
  7.   
  8. void Mythread::run()  
  9. {  
  10.     connect(&_timer, SIGNAL(timeout()), this, SLOT(mytimedout()));  
  11.     cout << "child thread id: " << QThread::currentThreadId() << endl;  
  12.     _timer.start(2000);  
  13.   
  14.     exec();  
  15.   
  16. }  
  17.   
  18. void Mythread::mytimedout()  
  19. {  
  20.     cout << "thread id: " << QThread::currentThreadId() << endl;  
  21. }  

main与以前相同


发现并不能正常隔两秒打印一次。根据我查的资料,大概原因是:QTimer是在主线程里创建的,它发的信号只能与主线程相关的QObject来处理。详细信息还需要细看文档。


修改代码,我们在子线程里创建一个QTimer,并用它来发超时信号,子线程会阻塞在exec()这里,所以不用担心mytimer的生存期

将run改为如下:

[cpp] view plaincopyprint?
  1. void Mythread::run()  
  2. {  
  3.     QTimer mytimer;  
  4.     connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));  
  5.     cout << "child thread id: " << QThread::currentThreadId() << endl;  
  6.     mytimer.start(2000);  
  7.   
  8.     exec();  
  9.   
  10. }  

此时可以打印了,不过,我们发现打印的时候,显示的是主线程id,也就是说,mytimedout这个函数是在主线程里执行的。根据所查资料,这里大概是因为:工作线程在创建的时候,线程的事件处理,是依附在创建者身上的,我们需要在创建之后,调用QObject::moveToThread()来改变依附性,

main修改如下:

[cpp] view plaincopyprint?
  1. #include <QtCore/QCoreApplication>  
  2. #include "mythread.h"   
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QCoreApplication a(argc, argv);  
  7.   
  8.     cout << "main thread id:" << QThread::currentThreadId() << endl;  
  9.   
  10.     Mythread thread;  
  11.     thread.start();  
  12.     thread.moveToThread(&thread);  
  13.   
  14.     return a.exec();  
  15. }  

这时候,便由子线程自己来打印了


接下来看下事件,QEvent!我们让子线程往主线程发送QEvent,主线程接受到后,打印信息表示接收到了。

要让主线程可以处理自定义事件,需要从QCoreApplication继承一个类,并改写虚函数event(QEvent *eve)

[cpp] view plaincopyprint?
  1. #ifndef MYAPP_H   
  2. #define MYAPP_H   
  3.   
  4. #include <QCoreApplication>   
  5. #include <mythread.h>   
  6.   
  7. class Myapp : public QCoreApplication  
  8. {  
  9.     Q_OBJECT  
  10. public:  
  11.     explicit Myapp(int argc, char* argv[], QObject *parent = 0);  
  12.   
  13.     bool event( QEvent * e );  
  14.   
  15. signals:  
  16.   
  17. public slots:  
  18.   
  19. };  
  20.   
  21. #endif // MYAPP_H  

[cpp] view plaincopyprint?
  1. #include "myapp.h"   
  2.   
  3. Myapp::Myapp(int argc, char* argv[], QObject *parent) :  
  4.     QCoreApplication(argc, argv)  
  5. {  
  6. }  
  7.   
  8. bool Myapp::event( QEvent * e )  
  9. {  
  10.     if(e->type() == (mytype1)){  
  11.         cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;  
  12.     }  
  13.     return QCoreApplication::event(e);  
  14. }  

子线程里,发送事件

[cpp] view plaincopyprint?
  1. #ifndef MYTHREAD_H   
  2. #define MYTHREAD_H   
  3.   
  4. #include <QThread>   
  5. #include <QTimer>   
  6. #include <QEvent>   
  7. #include <iostream>   
  8. using namespace std;  
  9.   
  10. enum Type{ mytype1 = QEvent::User+1, mytype2 }; // 这是我自定义的事件类型。这里有个问题,可能是qt的bug,如果我编译成功了,然后改mytype1=QEvent::User,再编译运行,就会收不到事件  
  11.   
  12.   
  13. class Mythread : public QThread  
  14. {  
  15.     Q_OBJECT  
  16. public:  
  17.     explicit Mythread(QObject *parent = 0);  
  18.   
  19. signals:  
  20.   
  21. public slots:  
  22.     void mytimedout();  
  23.   
  24. protected:  
  25.     void run();  
  26.   
  27.     QTimer _timer;  
  28.   
  29. };  
  30.   
  31. #endif // MYTHREAD_H  

[cpp] view plaincopyprint?
  1. #include <QCoreApplication>   
  2. #include "mythread.h"   
  3.   
  4.   
  5. Mythread::Mythread(QObject *parent) :  
  6.     QThread(parent)  
  7. {  
  8. }  
  9.   
  10. void Mythread::run()  
  11. {  
  12.     QTimer mytimer;  
  13.     connect(&mytimer, SIGNAL(timeout()), this, SLOT(mytimedout()));  
  14.     cout << "child thread id: " << QThread::currentThreadId() << endl;  
  15.   
  16.     mytimer.start(2000);  
  17.   
  18.     exec();  
  19.   
  20. }  
  21.   
  22. void Mythread::mytimedout()  
  23. {  
  24.     cout << "thread id: " << QThread::currentThreadId() << endl;  
  25.     QEvent *eve = new QEvent((QEvent::Type)(mytype1));  
  26.     QCoreApplication::postEvent(QCoreApplication::instance(), eve);  
  27. }  

[cpp] view plaincopyprint?
  1. #include <QtCore/QCoreApplication>  
  2. #include "mythread.h"   
  3. #include "myapp.h"   
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     Myapp a(argc, argv);  
  8.   
  9.     cout << "reg value:" << QEvent::registerEventType(mytype1) << endl; // 这里注册一下,看该类型的事件是否被别人注册过了,避免混淆  
  10.     cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;  
  11.   
  12.     cout << "main thread id:" << QThread::currentThreadId() << endl;  
  13.   
  14.     Mythread thread;  
  15.     thread.start();  
  16.     thread.moveToThread(&thread);  
  17.   
  18.     return a.exec();  
  19. }  


我们在Mythread::mytimedout()里new 了一个QEvnet对象,却没有去delete它,那么它会内存泄漏吗?其实不会,因为这个事件被发出去后,处理该事件的对象在处理完该事件后,会delete它,比如这里是

[cpp] view plaincopyprint?
  1. bool Myapp::event( QEvent * e )  
  2. {  
  3.     if(e->type() == (mytype1)){  
  4.         cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;  
  5.     }  
  6.     return QCoreApplication::event(e);  
  7. }  
return QCoreApplication::event(e); 这里是转给父类去处理,估计最终会由QObject的event函数来delete掉。


我们可以自己写个类继承自QEvent,QEvent的析构函数为虚。我们的类继承它,别人delete这个类时,除了会执行自己的析构函数以外,也会调用QEvent的析构函数。

[cpp] view plaincopyprint?
  1. #ifndef MYTHREAD_H   
  2. #define MYTHREAD_H   
  3.   
  4. #include <QThread>   
  5. #include <QTimer>   
  6. #include <QEvent>   
  7. #include <iostream>   
  8. using namespace std;  
  9.   
  10. enum Type{ mytype1 = QEvent::User+1, mytype2 };  
  11.   
  12. class Myevent:public QEvent  
  13. {  
  14. public:  
  15.     Myevent(QEvent::Type type):QEvent(type){}  
  16.     ~Myevent(){  
  17.         cout << "thread id: " << QThread::currentThreadId() << "destroy event, type is:" << type() << endl;  
  18.     }  
  19. };  
  20.   
  21.   
  22. class Mythread : public QThread  
  23. {  
  24.     Q_OBJECT  
  25. public:  
  26.     explicit Mythread(QObject *parent = 0);  
  27.   
  28. signals:  
  29.   
  30. public slots:  
  31.     void mytimedout();  
  32.   
  33. protected:  
  34.     void run();  
  35.   
  36.     QTimer _timer;  
  37.   
  38. };  
  39.   
  40. #endif // MYTHREAD_H  

[cpp] view plaincopyprint?
  1. void Mythread::mytimedout()  
  2. {  
  3.     cout << "thread id: " << QThread::currentThreadId() << endl;  
  4.     QEvent *eve = new Myevent((QEvent::Type)(mytype1));  
  5.     QCoreApplication::postEvent(QCoreApplication::instance(), eve);  
  6. }  

可以看到Myapp的线程在处理完事件后,delete掉了事件,因为调用了Myevent的析构函数。



最后,我们让父类接收到mytype1事件后,往子线程发一个mytype2事件。

类Mythread需要重载event方法

[cpp] view plaincopyprint?
  1. bool Mythread::event( QEvent * e )  
  2. {  
  3.     switch(e->type())  
  4.     {  
  5.     case mytype2:  
  6.         cout << "child thread:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;  
  7.         break;  
  8.     default:  
  9.         break;  
  10.     }  
  11.     return QThread::event(e);  
  12. }  

Myapp的event变为

[cpp] view plaincopyprint?
  1. bool Myapp::event( QEvent * e )  
  2. {  
  3.     if(e->type() == (mytype1)){  
  4.         cout << "thread id:" << QThread::currentThreadId() << "recv a event, type is:" << e->type() << endl;  
  5.         QEvent *eve = new Myevent((QEvent::Type)mytype2);  
  6.         postEvent(_recv, eve);  
  7.     }  
  8.     return QCoreApplication::event(e);  
  9. }  

这里_recv是子线程的QObject指针

[cpp] view plaincopyprint?
  1. void setReceive(QObject *obj){  
  2.         _recv = obj;  
  3.     }  
  4. protected:  
  5.     QObject *_recv;  

main函数

[cpp] view plaincopyprint?
  1. #include <QtCore/QCoreApplication>  
  2. #include "mythread.h"   
  3. #include "myapp.h"   
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     Myapp a(argc, argv);  
  8.   
  9.     cout << "reg value:" << QEvent::registerEventType(mytype1) << endl;  
  10.     cout << "reg value:" << QEvent::registerEventType(mytype2) << endl;  
  11.   
  12.     cout << "main thread id:" << QThread::currentThreadId() << endl;  
  13.   
  14.     Mythread thread;  
  15.     a.setReceive(&thread);  
  16.   
  17.     thread.start();  
  18.     thread.moveToThread(&thread);  
  19.   
  20.     return a.exec();  
  21. }  

这样,子线程每2秒处理一个超时信号,打印相关信息和往主线程发事件mytype1,主线程接收到该事件后,往子线程发一个mytype2的事件。子线程处理mytype2事件,打印信息

注意:

[cpp] view plaincopyprint?
  1. 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