forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtThreading.py
66 lines (49 loc) · 1.56 KB
/
QtThreading.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年3月8日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: Threading.QtThreading
@description:
"""
from threading import Thread
from time import sleep
try:
from PyQt5.QtCore import QObject, pyqtSignal, QTimer, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QProgressBar, QApplication
except ImportError:
from PySide2.QtCore import QObject, Signal as pyqtSignal, QTimer, Qt
from PySide2.QtWidgets import QWidget, QVBoxLayout, QProgressBar, QApplication
class _Signals(QObject):
updateProgress = pyqtSignal(int)
Signals = _Signals()
class UpdateThread(Thread):
def run(self):
self.i = 0
for i in range(101):
self.i += 1
Signals.updateProgress.emit(i)
sleep(1)
self.i = 0
Signals.updateProgress.emit(i)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 400)
layout = QVBoxLayout(self)
self.progressBar = QProgressBar(self)
layout.addWidget(self.progressBar)
Signals.updateProgress.connect(
self.progressBar.setValue, type=Qt.QueuedConnection)
QTimer.singleShot(2000, self.doStart)
def doStart(self):
self.updateThread = UpdateThread(daemon=True)
self.updateThread.start()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())