Skip to content

Commit ec891bd

Browse files
committed
QtThreading
1 parent 7d1a9ca commit ec891bd

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

.settings/org.eclipse.core.resources.prefs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ encoding//Demo/Lib/FramelessWindow.py=utf-8
99
encoding//Demo/NativeEvent.py=utf-8
1010
encoding//Demo/Notification.py=utf-8
1111
encoding//Demo/ProbeWindow.py=utf-8
12+
encoding//Demo/QtThreading.py=utf-8
1213
encoding//Demo/RestartWindow.py=utf-8
1314
encoding//Demo/SharedMemory.py=utf-8
1415
encoding//Demo/SingleApplication.py=utf-8

Demo/QtThreading.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2019年3月8日
6+
@author: Irony
7+
@site: https://pyqt5.com https://github.com/892768447
8+
@email: 892768447@qq.com
9+
@file: Threading.QtThreading
10+
@description:
11+
"""
12+
from threading import Thread
13+
from time import sleep
14+
15+
from PyQt5.QtCore import QObject, pyqtSignal, QTimer, Qt
16+
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QProgressBar
17+
18+
19+
__Author__ = """By: Irony
20+
QQ: 892768447
21+
Email: 892768447@qq.com"""
22+
__Copyright__ = 'Copyright (c) 2019 Irony'
23+
__Version__ = 1.0
24+
25+
26+
class _Signals(QObject):
27+
28+
updateProgress = pyqtSignal(int)
29+
30+
31+
Signals = _Signals()
32+
33+
34+
class UpdateThread(Thread):
35+
36+
def run(self):
37+
self.i = 0
38+
for i in range(101):
39+
self.i += 1
40+
Signals.updateProgress.emit(i)
41+
sleep(1)
42+
self.i = 0
43+
Signals.updateProgress.emit(i)
44+
45+
46+
class Window(QWidget):
47+
48+
def __init__(self, *args, **kwargs):
49+
super(Window, self).__init__(*args, **kwargs)
50+
self.resize(400, 400)
51+
layout = QVBoxLayout(self)
52+
self.progressBar = QProgressBar(self)
53+
layout.addWidget(self.progressBar)
54+
Signals.updateProgress.connect(
55+
self.progressBar.setValue, type=Qt.QueuedConnection)
56+
57+
QTimer.singleShot(2000, self.doStart)
58+
59+
def doStart(self):
60+
self.updateThread = UpdateThread(daemon=True)
61+
self.updateThread.start()
62+
63+
64+
if __name__ == '__main__':
65+
import sys
66+
from PyQt5.QtWidgets import QApplication
67+
app = QApplication(sys.argv)
68+
w = Window()
69+
w.show()
70+
sys.exit(app.exec_())

Demo/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,11 @@ PyQt 结合 Opencv 进行人脸检测;
138138
3. [dlib-19.4.0.win32-py3.5.exe](Data/dlib-19.4.0.win32-py3.5.exe)
139139
4. [shape-predictor-68-face-landmarks.dat.bz2](http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2)
140140

141-
![FacePoints](ScreenShot/FacePoints.png)
141+
![FacePoints](ScreenShot/FacePoints.png)
142+
143+
## 16、使用Threading
144+
[运行 QtThreading.py](QtThreading.py)
145+
146+
在PyQt中使用Theading线程
147+
148+
![QtThreading](ScreenShot/QtThreading.gif)

Demo/ScreenShot/QtThreading.gif

20 KB
Loading

0 commit comments

Comments
 (0)