GUI元素(包括QWidget)只能在主线程中创建。在
但是,您可以将每个选项卡的模型和业务逻辑放在单独的线程中,并让每个选项卡使用Signals和Slots与主线程通信。Qt documentation for ^{}提供了一个Worker模式的示例。在
这样,如果任何工作进程挂起,它不会影响主GUI线程的响应。在class MyTab(QtGui.QWidget):
def __init__(self, parent):
...
self.worker = Worker()
self.thread = QtCore.QThread(self)
self.worker.moveToThread(self.thread)
self.worker.resultReady.connect(self.handleResult)
self.thread.start()
def callSomeFunction(self):
QtCore.QMetaObject.invokeMethod(self.worker, 'someFunction', QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, 'arg1'))
@QtCore.pyqtSlot(object)
def handleResult(self, result):
... # do stuff with result
class Worker(QtCore.QObject):
resultReady = QtCore.pyqtSignal(object)
@QtCore.pyqtSlot(str)
def someFunction(self, arg):
...
self.resultReady.emit({'func': 'someFunction', 'result': True})