Reputation: 497
I want to be able to instantiate a number of QWidget() type classes, each in a separate process. Like Google Chrome does when you open a new tab.
Is this possible in python ?
Upvotes: 3
Views: 1734
Reputation: 37559
GUI elements (including QWidget
) can only be created in the main thread.
However, you could put the model and business logic for each tab in a separate thread, and have each communicate with the main thread using Signals
and Slots
. The Qt documentation for QThreads
provides an example of the Worker Pattern for doing this.
This way, if any of the worker processes become hung, it won't effect the responsiveness of your main GUI thread.
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})
Upvotes: 3