Skip to content Skip to sidebar Skip to footer

How Do I Catch A Pyqt Closeevent And Minimize The Dialog Instead Of Exiting?

I have a QDialog object. When the user clicks on the X button or presses Ctrl+Q, I want the dialog to go to a minimized view or system tray icon, instead of closing. How do I do

Solution 1:

A simple subclass that minimizes instead of closing is the following:

classMyDialog(QtGui.QDialog):
    # ...def__init__(self, parent=None):
        super(MyDialog, self).__init__(parent)

        # when you want to destroy the dialog set this to True
        self._want_to_close = FalsedefcloseEvent(self, evnt):
        if self._want_to_close:
            super(MyDialog, self).closeEvent(evnt)
        else:
            evnt.ignore()
            self.setWindowState(QtCore.Qt.WindowMinimized)

You can test it with this snippet in the interactive interpreter:

>>>from PyQt4 import QtCore, QtGui>>>app = QtGui.QApplication([])>>>win = MyDialog()>>>win.show()>>>app.exec_()   #after this try to close the dialog, it wont close bu minimize

Post a Comment for "How Do I Catch A Pyqt Closeevent And Minimize The Dialog Instead Of Exiting?"