我正在创建一个PySide2应用程序。我通过子类化QObject并将其传递给QThread来使用QThread的movetoThread功能。
我有两个按钮(开始/停止),进度条和文本编辑。
在“开始”按钮上,我在线程中执行了繁重的任务。 在“停止”按钮上,我通过验证标志值来停止正在运行的任务。
问题是当我的任务正在线程执行时,GUI响应缓慢(不冻结)
我不确定是什么问题。下面是我的代码文件。
任何指针都会有所帮助。
PS:我在这里也有一段代码: https://github.com/williamcharles/testthreading
app.py
from PySide2.QtWidgets import QApplication
import sys
from mainwindow import MainWindow
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
if __name__ == "__main__":
main()
mainwindow.py
from PySide2.QtWidgets import QMainWindow
from uiloader import loadUi
from PySide2.QtCore import QThread, Slot, Signal
from PySide2.QtGui import QCloseEvent
from util import logthread
from worker import Worker
class MainWindow(QMainWindow):
def __init__(self, parent=None):
logthread('MainWindow.__init__')
QMainWindow.__init__(self, parent)
self.widget = loadUi('window.ui', self)
self.setFixedSize(self.size())
self.thread = QThread()
self.thread.started.connect(self.threadStarted)
self.thread.finished.connect(self.threadFinished)
self.worker = Worker()
self.worker.moveToThread(self.thread)
self.thread.start()
self.worker.qtextedit.connect(self.qtextedit_addtext)
self.worker.stop.connect(self.endNow)
self.worker.progress.connect(self.updateProgressBar)
@Slot()
def on_pbStart_clicked(self):
logthread('MainWindow.on_pbStart_clicked')
self.widget.pbrProgress.setRange(0, 0)
self.teErrors.setText("")
self.worker.start.emit()
@Slot()
def on_pbStop_clicked(self):
logthread('MainWindow.on_pbStop_clicked')
self.widget.pbrProgress.setRange(0, 1)
self.worker.stop.emit()
@Slot(str)
def qtextedit_addtext(self, text):
logthread('MainWindow.qtextedit_addtext args-{}'.format(str(text)))
self.teErrors.setText(str(text))
@Slot()
def endNow(self):
logthread('MainWindow.endNow')
self.worker.continueWork = False
@Slot(int)
def updateProgressBar(self, progress):
logthread('MainWindow.updateProgressBar')
self.widget.pbrProgress.setValue(progress)
@Slot()
def on_actionProject1_triggered(self):
logthread('MainWindow.on_actionProject1_triggered')
from projectsettings1 import SettingsProject1
dialogSettingsProject1 = SettingsProject1()
dialogSettingsProject1.exec_()
@Slot()
def on_actionProject2_triggered(self):
logthread('MainWindow.on_actionProject2_triggered')
from projectsettings2 import SettingsProject2
dialogSettingsProject2 = SettingsProject2()
dialogSettingsProject2.exec_()
def closeEvent(self, event: QCloseEvent):
logthread('MainWindow.closeEvent')
self.worker.continueWork = False
self.thread.quit()
self.thread.wait()
def threadStarted(self):
logthread('MainWindow.threadStarted')
def threadFinished(self):
logthread('MainWindow.threadFinished')
worker.py
from PySide2.QtCore import QObject, Signal, Slot
from util import logthread
class Worker(QObject):
start = Signal()
stop = Signal()
progress = Signal(int)
qtextedit = Signal(str)
def __init__(self):
logthread('Worker.__init__')
super().__init__()
self.continueWork = True
self.start.connect(self.startNow)
self.process = None
@Slot()
def startNow(self):
logthread('Worker.startNow')
self.continueWork = True
try:
self.do_heavy_task()
except Exception as e:
print(str(e))
def do_heavy_task(self):
count = 0
while self.continueWork is True:
count += 1
threading.py
import threading
def logthread(caller):
print('%-25s: %s, %s,' % (caller, threading.current_thread().name,
threading.current_thread().ident))