First off, I am a complete newbie to PyQt.
I have been trying to link a function to the Main Window\'s close button (the red x in the corner of the window) but I haven\'
I ran into this same problem, where I had a dialog window prompt, asking the user for input before proceeding in an operation. The user could add input before the operation was processed, skip adding input and process anyway or cancel out of the operation using a Cancel button or by clicking the X of the dialog window.
What I did instead was create a variable holding a boolean value that would be checked before closing the window, so that in the event the window was forced closed, it's clear that a process was aborted.
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
#This variable will be your way of determining how the window was closed
self.force_close = True
self.buttonOk.clicked.connect(self.someOtherFunction)
def setupUi(self, MainWindow):
#setup code goes here
def retranslateUi(self, MainWindow):
#re translation of the GUI code
def someOtherFunction(self):
# Do some things here if needed
print('All done')
# Assuming some operation is performed and a value or result is generated
# Since this function completed, there is no need to force close the window
# So it can be set to False
self.force_close = False
# the below close() is a built-in function and will automatically trigger the
# closeEvent
self.close()
def closeEvent(self, event):
if self.force_close is True:
# Here is where you could add you code to perform some operation
# due to the user clicking the X