The correct way to handle the close button in the main PyQt window (red "X")

First off, I'm a complete newbie to PyQt.

I am trying to associate a function with the close button of the main window (red x in the corner of the window), but I have not succeeded. 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 separate "main" file, I have the following:

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 through the command line, I cannot see the print statement when I hit red x. I know that I am new to Qt, but I have seen many people ask this question, and none of the answers go beyond what is already written above.

One of these answers: Answer # 1 Answer # 2

Both of these solutions are similar to mine, but they still do not work.

, , , , PyQt , . "Red X box", PyQt? , ?

+4
2

, ui. .

QMainWindow, closeEvent (, ?). self.ui - QMainWindow, , GUIForm. Ui_MainWindow.setupUi(), QMainWindow, 'GUIForm`.

Ui_MainWindow , , ui, python :

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)

. python .ui ( Python)

+7

.

:

app.aboutToQuit.connect(self.closeEvent)

closeEvent.

:

- QtGui.QApplication

:

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        #{================================

        app.aboutToQuit.connect(self.closeEvent)

        #}================================

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle('Demo')

    #{================================

    def closeEvent(self):
        #Your desired functionality here
        print('Close button pressed')
        import sys
        sys.exit(0)

    #}================================


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

, .

+2

Source: https://habr.com/ru/post/1546858/


All Articles