dark gemini
dark gemini

Reputation: 33

Python & PyQt: Catch Minimize Event

Uhhh... Can somebody please help me with this? I can't seem to catch the minimize event of a widget I created. I tried solutions from every post I could find but it just doesn't seem to work with my code. In one of the solutions I tried, the window minimized and the system tray icon shows up but the app icon is still visible on the taskbar. I can't seem to figure out what I should be doing or what I'm missing here. I'm using Python 2.7 and PyQt 4.11. My OS is Windows 7 so I'm pretty sure its not an os issue. I'd really appreciate it if someone could help me here.

Here is the code.

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(385, 277)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.radioButton = QtGui.QRadioButton(Form)
        self.radioButton.setObjectName(_fromUtf8("radioButton"))
        self.gridLayout.addWidget(self.radioButton, 0, 1, 1, 1)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 1, 1, 1, 1)
        self.listWidget = QtGui.QListWidget(Form)
        self.listWidget.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked|QtGui.QAbstractItemView.EditKeyPressed|QtGui.QAbstractItemView.SelectedClicked)
        self.listWidget.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
        self.listWidget.setMovement(QtGui.QListView.Snap)
        self.listWidget.setUniformItemSizes(False)
        self.listWidget.setObjectName(_fromUtf8("listView"))
        self.gridLayout.addWidget(self.listWidget, 2, 0, 1, 2)
        #system tray
        self.systemTrayIcon = QtGui.QSystemTrayIcon()
        self.systemTrayIcon.setIcon(QtGui.QIcon(":/images/images/mad-icon.jpg"))
        self.systemTrayIcon.setVisible(True)
        self.systemTrayIcon.activated.connect(self.sysTrayIconActivated)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.radioButton.setText(_translate("Form", "monitor clipboard", None))
        self.pushButton.setText(_translate("Form", "Add", None))

    #when system tray icon is clicked, if window is visible, then hide. if it is not visible, then show
    def sysTrayIconActivated(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger:
            if Form.isHidden():
                Form.show()
            else:
                Form.hide()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_()) 

Upvotes: 2

Views: 4606

Answers (2)

Aleksandr Sokolov
Aleksandr Sokolov

Reputation: 59

This code should work

def changeEvent(self, event):
    if event.type() == QEvent.WindowStateChange:
        if event.oldState() and Qt.WindowMinimized:
            print("WindowMinimized")
        elif event.oldState() == Qt.WindowNoState or self.windowState() == Qt.WindowMaximized:
            print("WindowMaximized")

Upvotes: 5

Daniele Pantaleone
Daniele Pantaleone

Reputation: 2723

You could use the changeEvent provided by QWidget:

def changeEvent(self, event):
    if event.type() == QEvent.WindowStateChange:
        if self.windowState() & Qt.WindowMinimized:
            pass

Upvotes: 3

Related Questions