在Python中,QThread是PyQt库中的一个类,用于在应用程序中创建多线程。
使用QThread,可以将耗时的任务和UI操作分开,避免在主线程中执行耗时任务导致UI卡顿的问题。
以下是使用QThread的基本步骤:
from PyQt5.QtCore import QThread class MyThread(QThread): def __init__(self): super().__init__() def run(self): # 执行耗时任务 pass my_thread = MyThread() my_thread.start() from PyQt5.QtCore import pyqtSignal class MyThread(QThread): finished = pyqtSignal() def __init__(self): super().__init__() def run(self): # 执行耗时任务 self.finished.emit() my_thread = MyThread() my_thread.finished.connect(my_slot) 通过以上步骤,就可以在Python中使用QThread创建多线程,并实现多线程操作。