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't been having any success. Right now, my code looks something like this:
class Ui_MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
#setup code goes here
def retranslateUi(self, MainWindow):
#re translation of the GUI code
def closeEvent(self, event):
print "User has clicked the red x on the main window"
In a seperate "main" file I have the folowing:
class GUIForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#self.ui.ECUStatus_txt = MyWidget.__init__.text_list
self.threadData()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = GUIForm()
myapp.show()
ret = app.exec_()
sys.exit(ret)
However, when I run via command prompt, I am not able to see the print statement when I hit the red x. I know I am new to Qt, but I have seen quite a few people ask this question, and none of the answers seemed to get beyond what is already written above.
One of these answers: Answer #1 Answer #2
Both of these solutions are similar to mine, but it still doesn't work
Despite the answer, which may have worked for that user's specific code, my fellow PyQt colleagues and I are still quite foggy on the reasoning ours is not working. Is there a defined button name for the "Red X box" built into PyQt? Can I connect it to another function the someway I would for other buttons?
aboutToQuit
signal is what you're looking for? qt-project.org/doc/qt-5/qcoreapplication.html#aboutToQuitapp
instantinating:app.aboutToQuit.connect(on_exit)