QCoreApplication: How to Exit non-GUI application
Solved
Qt for Python
-
I have a small non-GUI application that basically starts an event loop, connects a signal to a slot, and emits a signal. I would like the slot to stop the event loop and exit the application.
However, the application does not exit.
Does anyone have any ideas on how to exit from the event loop?
Conrad
import sys from PySide2 import QtCore, QtWidgets class ConsoleTest(QtCore.QObject): all_work_done = QtCore.Signal(str) def __init__(self, parent=None): super(ConsoleTest, self).__init__(parent) self.run_test() def run_test(self): self.all_work_done.connect(self.stop_test) self.all_work_done.emit("foo!") @QtCore.Slot(str) def stop_test(self, msg): print(f"Test is being stopped, message: {msg}") # neither of the next two lines will exit the application. QtCore.QCoreApplication.quit() # QtCore.QCoreApplication.exit(0) return if __name__ == "__main__": app = QtCore.QCoreApplication(sys.argv) mainwindow = ConsoleTest() sys.exit(app.exec_())
-