UrbKr
UrbKr

Reputation: 663

Making closeEvent() work outside of a class in PySide

Seemingly the closeEvent method in the following code is called upon clicking x (in the top right corner of the window). I'm guessing the information that allows python to make that connection is inside the self argument.

Is there a way to implement this into a procedural or functional program.

import sys
from PySide import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Message box')
        self.show()

    def closeEvent(self, event):

        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes |
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 1915

Answers (1)

MadeOfAir
MadeOfAir

Reputation: 3183

Apparently, you can assign your own function to the QWidget.closeEvent function (property), given that you take the automatically passed in instance argument and event into account:

def myHandler(widget_inst, event):
    print("Handling closeEvent")

mywidget = QWidget()
mywidget.closeEvent = myHandler

This is going to get tedious and is not the way things were intended to be done.

Upvotes: 1

Related Questions