Stephen
Stephen

Reputation: 673

pyqt event trigger on click of minimze button

I've figured out how to trigger an event when the window changes, but this happens when I'm reading window information from the database, and I want to write to the database when the window is reduced, so, I would like to trigger my event based on clicking the reduce button as opposed to just any time the window changes.

Upvotes: 1

Views: 4157

Answers (2)

ekhumoro
ekhumoro

Reputation: 120588

The script below works on both Linux and Win XP (and probably OSX, but I can't test it):

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.WindowStateChange:
            if self.windowState() & QtCore.Qt.WindowMinimized:
                print('changeEvent: Minimised')
            elif event.oldState() & QtCore.Qt.WindowMinimized:
                print('changeEvent: Normal/Maximised/FullScreen')
        QtGui.QWidget.changeEvent(self, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 300)
    window.show()
    sys.exit(app.exec_())

Upvotes: 6

reclosedev
reclosedev

Reputation: 9502

You can use QWidget.hideEvent and check for self.isMinimized(), because hideEvent called when closing window too. Example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *


class HookMinimize(QWidget):
    def hideEvent(self, event):
        QWidget.hideEvent(self, event)
        if self.isMinimized():
            print "Doing background task"


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = HookMinimize()
    window.resize(300, 400)
    window.show()
    sys.exit(app.exec_())

Upvotes: 0

Related Questions