Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QCoreApplication: How to Exit non-GUI application
Forum Updated to NodeBB v4.3 + New Features

QCoreApplication: How to Exit non-GUI application

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crankin360
    wrote on last edited by
    #1

    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_())
    
    Volodymyr14V 1 Reply Last reply
    0
    • C crankin360

      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_())
      
      Volodymyr14V Offline
      Volodymyr14V Offline
      Volodymyr14
      wrote on last edited by Volodymyr14
      #2

      @crankin360 Try instead:

      QtCore.QCoreApplication.quit()
      

      this line:

      sys.exit()
      

      PyQt/PySide

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved