-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxthread.cpp
More file actions
127 lines (104 loc) · 2.37 KB
/
xthread.cpp
File metadata and controls
127 lines (104 loc) · 2.37 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "xthread.h"
#include <QApplication>
#include <QDebug>
int XThread::m_count = 0;
void XThread::initBuild()
{
m_count++;
m_funcVoid = nullptr;
m_funcInt = nullptr;
m_funcString = nullptr;
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
createTimer();
}
void XThread::createTimer()
{
// m_timer = NULL;
// m_timerInterval = 1000;
// m_timerTimeoutCounter = 0;
// m_timer = new QTimer();//不能指定父对象
// m_timer->moveToThread(this);
// m_timer->setInterval(m_timerInterval);
// connect(this, SIGNAL(started()), m_timer, SLOT(start()));
// connect(m_timer,SIGNAL(timeout()),this,SLOT(slotTimerTimeout()),Qt::DirectConnection);
}
XThread::XThread(QObject *parent)
: QThread(parent)
{
initBuild();
}
XThread::XThread(XTHREAD_VOID_FUNC func, QObject *parent)
: QThread(parent)
{
initBuild();
m_funcVoid = func;
}
XThread::XThread(XTHREAD_INT_FUNC func, QObject *parent)
: QThread(parent)
{
initBuild();
m_funcInt = func;
}
XThread::XThread(XTHREAD_STRING_FUNC func, QObject *parent)
: QThread(parent)
{
initBuild();
m_funcString = func;
}
XThread::~XThread()
{
m_count--;
}
void XThread::run()
{
runCallbackFunc();
// exec(); //如果创建了timer,必须运行exec才能生效
}
int XThread::count()
{
return m_count;
}
void XThread::setCallback(XTHREAD_VOID_FUNC func)
{
m_funcVoid = func;
}
void XThread::setCallback(XTHREAD_INT_FUNC func)
{
m_funcInt = func;
}
void XThread::setCallback(XTHREAD_STRING_FUNC func)
{
m_funcString = func;
}
void XThread::runCallbackFunc()
{
if (m_funcVoid != nullptr)
{
m_funcVoid();
}
else if (m_funcInt != nullptr)
{
int ret = -1;
m_funcInt( ret );
emit signalFinishResult(ret);
}
else if (m_funcString != nullptr)
{
QString str = "";
m_funcString( str );
emit signalFinishResult(str);
}
}
//void XThread::setTimerInterval(int msec)
//{
// m_timerInterval = msec;
//}
//int XThread::timerInterval() const
//{
// return m_timerInterval;
//}
//void XThread::slotTimerTimeout()
//{
// m_timerTimeoutCounter++;
// emit signalTimerTimeout(m_timerTimeoutCounter);
//}