File tree Expand file tree Collapse file tree 4 files changed +53
-1
lines changed Expand file tree Collapse file tree 4 files changed +53
-1
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ Created on 2020/11/27
6
+ @author: Irony
7
+ @site: https://pyqt.site https://github.com/PyQt5
8
+
9
+ @file: QuitThread
10
+ @description:
11
+ """
12
+
13
+ __Author__ = 'Irony'
14
+ __Copyright__ = 'Copyright (c) 2020'
15
+ __Version__ = 'Version 1.0'
16
+
17
+ import sys
18
+ from time import time
19
+
20
+ from PyQt5 .QtCore import QThread , QCoreApplication , QTimer
21
+
22
+
23
+ class Thread (QThread ):
24
+
25
+ def run (self ):
26
+ print ('thread id' , int (QThread .currentThreadId ()))
27
+ i = 0
28
+ while i < 101 and not self .isInterruptionRequested ():
29
+ print ('value' , i , time ())
30
+ i += 1
31
+ QThread .msleep (500 )
32
+ print ('thread quit' )
33
+
34
+
35
+ if __name__ == '__main__' :
36
+ app = QCoreApplication (sys .argv )
37
+ t = Thread ()
38
+ t .finished .connect (app .quit )
39
+ t .start ()
40
+ # 3秒后退出
41
+ print ('will quit 3s latter' )
42
+ QTimer .singleShot (3000 , t .requestInterruption )
43
+ sys .exit (app .exec_ ())
Original file line number Diff line number Diff line change 5
5
- [ moveToThread] ( #2moveToThread )
6
6
- [ 线程挂起恢复] ( #3线程挂起恢复 )
7
7
- [ 线程休眠唤醒] ( #4线程休眠唤醒 )
8
+ - [ 线程退出] ( #5线程退出 )
8
9
9
10
## 1、继承QThread
10
11
[ 运行 InheritQThread.py] ( InheritQThread.py )
34
35
35
36
使用 ` QWaitCondition ` 的 ` wait ` 和 ` wakeAll ` 方法
36
37
37
- ![ WakeupThread] ( ScreenShot/WakeupThread.gif )
38
+ ![ WakeupThread] ( ScreenShot/WakeupThread.gif )
39
+
40
+ ## 5、线程退出
41
+ [ 运行 QuitThread.py] ( QuitThread.py )
42
+
43
+ ` isInterruptionRequested ` 和 ` requestInterruption ` 函数作为退出标识调用
44
+
45
+ ![ QuitThread] ( ScreenShot/QuitThread.jpg )
Original file line number Diff line number Diff line change @@ -148,6 +148,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
148
148
- [ moveToThread] ( QThread/moveToThread.py )
149
149
- [ 线程挂起恢复] ( QThread/SuspendThread.py )
150
150
- [ 线程休眠唤醒] ( QThread/WakeupThread.py )
151
+ - [ 线程退出] ( QThread/QuitThread.py )
151
152
152
153
- [ QtQuick] ( QtQuick )
153
154
- [ Flat样式] ( QtQuick/FlatStyle.py )
You can’t perform that action at this time.
0 commit comments