我试图了解如何做一个 Qthread,我有这个骨架代码。我的主要目标是在后端执行一些数据库操作并同时更新 GUI 中的表小部件时不要让 GUI“挂起”。背景:在 Windows 操作系统上执行此操作。Pyside 作为 GUI。(我也尝试过 Python 线程,但每次我的应用程序崩溃)
class GenericThread(QtCore.QThread):
def __init__(self, function, *args, **kwargs):
QtCore.QThread.__init__(self)
self.function = function
self.args = args
self.kwargs = kwargs
def __del__(self):
self.wait()
def run(self):
self.function(*self.args,**self.kwargs)
return
clas myMainGUI(...)
def add(self):
...
for myfiles in os.listdir(..): <--- intensive process, lots of files
column headers = [ .... ]
result = select_data_from_file(myfiles) <----- database file processing
self.insert_table_widget ( column headers, result ) <--want to insert to widge in "realtime" and do other stuff without GUI getting "hang"
....
self.mythread.emit() <-- ??
def coolie(self): # some button will call this function.
if buttonclick == "add":
self.mythread = GenericThread(self.add)
self.mythread.disconnect() <------??
self.mythread.connect() <------ ??
self.mythread.start()
知道我的 emit()、connect() 或 disconnect() 应该如何吗?谢谢