0

I have written small tool and works well. I would like to system testing where I can open window and close window to run high level flow. But I am not able to figure to close window through program. One silly option is, to kill process to close window but I feel there could be some better way.

Just sharing invoking code

self.logger.info("Running gui mode")
        if self.approval:
            app = QApplication([])
            window = ApprovalWindow(app, self.block, self.mil, 
                                    self.vio, self.app, 
                                    self.prep, self.logger)
        else:
            app = QApplication([])
            window = ReviewWindow(app, self.block, self.mil, 
                                  self.vio, 
                                  self.pre, self.logger)
 
        window.create_widget()
        window.show()
        app.exec_()

Tried method
#       app.close()
#        for widget in appl.allwidgest():
#           widget.close
      
user765443
  • 1,856
  • 7
  • 31
  • 56

1 Answers1

1

If you want to terminate the application then you must use the quit() method:

QCoreApplication.quit()

If instead you want to close all the windows then there are the following options:

  1. Close all toplevel widgets:

    for tl_widget in QApplication.topLevelWidgets():
        tl_widget.close()
    
  2. Close all QWindows

    for window in QGuiApplication.topLevelWindows():
        window.close()
    
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • will internally call gui and close window am i right? – user765443 Oct 09 '21 at 06:56
  • @user765443 What it does is terminate the Qt eventloop which involves closing all windows – eyllanesc Oct 09 '21 at 06:57
  • one more info, winow stop(block) when exec_ call. So I need to remove this call am i right? – user765443 Oct 09 '21 at 07:11
  • @user765443 exec_ does not block but starts the eventloop, it is what allows Qt to obtain information from the OS, interact with the user, send and receive signals, etc. Please read https://stackoverflow.com/questions/40880949/what-is-an-event-loop-in-qt and https://doc.qt.io/qt-5/qapplication.html#exec – eyllanesc Oct 09 '21 at 07:15
  • @user765443 Do you want to close the application after a time T of showing the window? – eyllanesc Oct 09 '21 at 07:17
  • no.. just want to run system regression where gui run successful and call data model correct way kind of dry run – user765443 Oct 09 '21 at 11:26