title: Cleanly closing a qt Gui application application programatically
body: <p>Hello I've been using PyQt 4.8 on windows to create a simple gui application. The gui(main window) has one button, and one QLineEdit widget. Pressing the button simply calls a function to process the contents of the QLineEdit widget. 
  So far it works well. My main looks like this</p>
  
  <pre><code>class StartQt4App(QtGui.QMainWindow):
  
      def process_stuff(self,param):
          #Do stuff here
  
  
  if __name__="__main__":
      app=QtGui.QApplications(sys.argv)
      myapplication=StartQt4App()
      myapplication.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>Using py2exe I can create an windows executable e.g mygui_app.exe
  However I want to adapt it so that it can be run from the windows command line. i.e if the user types </p>
  
  <blockquote>
    <p>Run mygui_app.exe "c:\text_file.txt"</p>
  </blockquote>
  
  <p>The application launches, this time without the GUI and automatically processes the parameter that was entered when the application was called.</p>
  
  <p>So far this is what I have come up with</p>
  
  <pre><code>class StartQt4App(QtGui.QMainWindow):
  
      def process_stuff(self,param):
          #Do stuff here
  
      def process_stuff_again(self,param):
          #Do stuff here
          return
  
  
  if __name__="__main__":
      if len(sys.argv)==1:
  
          app=QtGui.QApplications(sys.argv)
          myapplication=StartQt4App()
          myapplication.show()
          sys.exit(app.exec_())
      else:
          app=QtGui.QApplications(sys.argv)
          myapplication=StartQt4App()
          myapplication.process_stuff_again(param)
          sys.exit(app.exec_())
  </code></pre>
  
  <p>Essentially if the application has been called with parameters I don't want/need to show the gui, simply call the processing function. Once processing is done exit cleanly</p>
  
  <p>Currently it works, at least as far as processing the file goes. However it does not exit, once the processing is complete. Instead the program remains active (I can see it using the Windows Task Manager). </p>
  
  <p>My question is how do I programatically quit the application? so that once done it cleanly shuts itself down.</p>
  
  <p>I have experimented with</p>
  
  <ul>
  <li>app.quit() </li>
  <li>app.exit()</li>
  <li>myapplication.exit()</li>
  <li>myapplication.quit()</li>
  </ul>
  
  <p>None work, essentially the application is still alive and kicking. In desperation I removed the </p>
  
  <blockquote>
    <p>sys.exit(app.exec_())</p>
  </blockquote>
  
  <p>from the else portion of the code and now it just crashes, as it it processes the file and then promptly crashes. Very consistently crashes.</p>
  
  <p>Any suggestion as to how to cleanly get the application to close nicely when its called from the command line?</p>