'PyQT: Differentiate between close() function and title-bar close [X]

I have the following function to ask the user if he really wants to quit the window:

def closeEvent(self, event):
    if self.running:
        reply = QMessageBox.question(self, 'QUIT',
                                     'Calculations are currently running!\n'
                                     'Do you really want to quit?',
                                     QMessageBox.Yes | QMessageBox.No,
                                     QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
    else:
        return QMainWindow.closeEvent(self, event)

So when the user presses the red X the QMessageBox will be shown. But I still want to be able to terminate the window without the QMessageBox, for example if I use the close()-function inside the code. Is there a possibility to distinguish between different senders of the CloseEvent like the close()-function or the X-Button?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source