class QApplicationRunner(QObject): """ Application runner starts application in own thread """ def __init__(self): QObject.__init__(self) self._thread = QThread() self.moveToThread(self._thread) self._thread.started.connect(self.start) self._ev = Event() self._app = None self._thread.start() @Slot() def start(self): self.app = Application() self._ev.set() self.app.exec_() def exit(self): self.app.exit() # perform polite disposal of resources if self._thread.isRunning(): self._thread.wait(1000) # wait 1 sec self._thread.terminate() # no-way @property def app(self): if not self._ev.isSet(): self._ev.wait() return self._app @app.setter def app(self, app): self._app = app
class QWebPageRunner(QObject): """ Web page runner starts WebPage instances in one separate thread and implements custom event loop. """ #FIXME: consider using QEventLoop instead def __init__(self, app): QObject.__init__(self) self._thread = QThread() self.moveToThread(self._thread) self._thread.started.connect(self.start) self._destroying = Event() self._destroying.clear() self._result = None self._commandQueue = Queue() self._app = app self._thread.start() @Slot() def start(self): try: while not self._destroying.is_set(): self._app.processEvents() try: cmd = self._commandQueue.get(timeout=0.1) args = () if isinstance(cmd, tuple): if not len(cmd): continue args = cmd[1:] cmd = cmd[0] if isinstance(cmd, NewPage): args = (self._app,) if isinstance(cmd, Command): cmd(*args) else: raise ValueError('Unknown command %s(%s).' % (cmd, args)) except Empty: pass except Exception as e: logger.exception(e) def exit(self): self._destroying.set() if self._thread.isRunning(): self._thread.wait(1000) # wait 1 sec self._thread.terminate() # no-way def invoke(self, cmd, *args): if isinstance(cmd, type) and issubclass(cmd, Command): cmd = cmd() if not isinstance(cmd, Command): cmd = Command(cmd) cmd.event.clear() self._commandQueue.put((cmd,)+args) while not cmd.event.is_set(): self._app.processEvents() cmd.event.wait(0.1) return cmd.result
def main(): loadtesterplugins() qt.mainthread = QThread.currentThread() # Store current thread qt.initialize_view() # Get the view ready dispatch_thread = QThread() # The QThread to put the dispatcher on qt.maindispatch = QTDispatcher(qt.mainview) # Set up the dispatcher qt.qapp.lastWindowClosed.connect(dispatch_thread.quit) # Connect the close signal to the thread quit signal qt.maindispatch.moveToThread(dispatch_thread) # Move the dispatcher to the new thread dispatch_thread.start() # Start the thread qt.mainview.show() # Show the main window res = qt.qapp.exec_() # Start the event loop, exits when the last window has been closed dispatch_thread.wait() # Wait for the dispatcher thread to finish sys.exit(res)
def main(argv = None): if argv is None: argv = sys.argv version = '20130216' # modification date in yyyymmdd format connmgr = ConnectionManager() comm = SerialComm(connmgr) app = QApplication(argv) appwindow = MainWindow(connmgr, 'Projector Control Panel') commthread = QThread() comm.enumerateSerialPorts() comm.moveToThread(commthread) commthread.start() appwindow.show() appwindow.writeToLog('Software version ' + version + '.') result = app.exec_() commthread.quit() commthread.wait() return result