2018-09-25 23:20:03 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Created on 2018年3月9日
|
|
|
|
@author: Irony
|
2021-07-13 14:52:26 +08:00
|
|
|
@site: https://pyqt.site , https://github.com/PyQt5
|
2018-09-25 23:20:03 +08:00
|
|
|
@email: 892768447@qq.com
|
|
|
|
@file: moveToThread
|
|
|
|
@description: moveToThread
|
|
|
|
"""
|
|
|
|
|
2021-07-13 14:52:26 +08:00
|
|
|
try:
|
|
|
|
from PyQt5.QtCore import QObject, pyqtSignal, QThread
|
|
|
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QPushButton
|
|
|
|
except ImportError:
|
|
|
|
from PySide2.QtCore import QObject, Signal as pyqtSignal, QThread
|
|
|
|
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QPushButton
|
2018-09-25 23:20:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Worker(QObject):
|
|
|
|
valueChanged = pyqtSignal(int) # 值变化信号
|
|
|
|
|
|
|
|
def run(self):
|
2021-07-13 14:52:26 +08:00
|
|
|
print('thread id', )
|
2018-09-25 23:20:03 +08:00
|
|
|
for i in range(1, 101):
|
2021-07-13 14:52:26 +08:00
|
|
|
if QThread.currentThread().isInterruptionRequested():
|
|
|
|
break
|
2018-09-25 23:20:03 +08:00
|
|
|
print('value', i)
|
|
|
|
self.valueChanged.emit(i)
|
|
|
|
QThread.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
class Window(QWidget):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Window, self).__init__(*args, **kwargs)
|
|
|
|
layout = QVBoxLayout(self)
|
|
|
|
self.progressBar = QProgressBar(self)
|
|
|
|
self.progressBar.setRange(0, 100)
|
|
|
|
layout.addWidget(self.progressBar)
|
|
|
|
layout.addWidget(QPushButton('开启线程', self, clicked=self.onStart))
|
|
|
|
|
|
|
|
# 当前线程id
|
2021-07-13 14:52:26 +08:00
|
|
|
print('main id', QThread.currentThread())
|
2018-09-25 23:20:03 +08:00
|
|
|
|
|
|
|
# 启动线程更新进度条值
|
|
|
|
self._thread = QThread(self)
|
|
|
|
self._worker = Worker()
|
|
|
|
self._worker.moveToThread(self._thread) # 移动到线程中执行
|
|
|
|
self._thread.finished.connect(self._worker.deleteLater)
|
2021-07-13 14:52:26 +08:00
|
|
|
self._thread.started.connect(self._worker.run)
|
2018-09-25 23:20:03 +08:00
|
|
|
self._worker.valueChanged.connect(self.progressBar.setValue)
|
|
|
|
|
|
|
|
def onStart(self):
|
2021-07-13 14:52:26 +08:00
|
|
|
if not self._thread.isRunning():
|
|
|
|
print('main id', QThread.currentThread())
|
|
|
|
self._thread.start() # 启动线程
|
2018-09-25 23:20:03 +08:00
|
|
|
|
|
|
|
def closeEvent(self, event):
|
|
|
|
if self._thread.isRunning():
|
2021-07-13 14:52:26 +08:00
|
|
|
self._thread.requestInterruption()
|
2018-09-25 23:20:03 +08:00
|
|
|
self._thread.quit()
|
2021-07-13 14:52:26 +08:00
|
|
|
self._thread.wait()
|
2018-09-25 23:20:03 +08:00
|
|
|
# 强制
|
|
|
|
# self._thread.terminate()
|
2021-07-13 14:52:26 +08:00
|
|
|
self._thread.deleteLater()
|
2018-09-25 23:20:03 +08:00
|
|
|
super(Window, self).closeEvent(event)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2021-07-13 14:52:26 +08:00
|
|
|
import cgitb
|
|
|
|
|
|
|
|
cgitb.enable(format='text')
|
|
|
|
|
2018-09-25 23:20:03 +08:00
|
|
|
app = QApplication(sys.argv)
|
|
|
|
w = Window()
|
|
|
|
w.show()
|
|
|
|
sys.exit(app.exec_())
|