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\'
You shouldn't ever modify the class that was generated from your ui file. Instead you should subclass and modify the subclass.
From the looks of your code, you are actually creating two QMainWindow
s and the closeEvent is being caught for the wrong one (presumably that one is hidden?). That is self.ui
is a QMainWindow
that is not being shown, and is not added to the UI of GUIForm
. Instead you are using the Ui_MainWindow.setupUi()
method explicitly yourself, to add the widgets to your own QMainWindow
, 'GUIForm`.
Instead, what you should to do is leave your Ui_MainWindow
class as it was when it was generated from the ui file, and then modify your main python file to be:
class GUIForm(Ui_MainWindow):
def __init__(self, parent=None):
Ui_MainWindow.__init__(self, parent)
self.threadData()
def closeEvent(self, event):
print "User has clicked the red x on the main window"
event.accept()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = GUIForm()
myapp.show()
ret = app.exec_()
sys.exit(ret)
This way you are extending the behaviour of the auto-generated UI file. This makes it easy to regenerate the python file from the .ui file without having to re-add code (this is precisely why you should never modify the auto-generated Python file)