-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyobject.h
76 lines (70 loc) · 1.51 KB
/
myobject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QObject>
#include <iostream>
#include <QtCore>
using namespace std;
class Worker : public QObject
{
Q_OBJECT
public:
Worker(){
num=0;
}
void operate(){
QEvent *ev=new QEvent((QEvent::Type)2333);
QCoreApplication::postEvent(this,ev);
}
void work(){
for(int i=0;i<10;i++){
emit Message();
QThread::msleep(1500);
}
}
signals:
void Message();
private:
bool event(QEvent *event){
num++;
qDebug()<<"Event #"<<num<<":"<<event->type();
if(event->type()==(QEvent::Type)2333){
onTimeout();
return true;
}
return QObject::event(event);
}
int num;
private slots:
void onTimeout()
{
qDebug()<<"Worker::onTimeout get called from?: "<<QThread::currentThreadId();
}
};
class MyThread:public QThread
{
Q_OBJECT
public:
MyThread(){
connect(&worker,&Worker::Message,this,&MyThread::post);
connect(&timer,&QTimer::timeout,this,&MyThread::post);
connect(this,&MyThread::begin,&worker,&Worker::work);
timer.start(500);
moveToThread(this);
worker.moveToThread(this);
timer.moveToThread(this);
}
Worker worker;
void post(){
qDebug()<<"Message in"<<currentThreadId();
}
void run(){
this->exec();
}
void startWorker(){
emit begin();
}
QTimer timer;
signals:
void begin();
};
#endif // MYOBJECT_H