简体   繁体   English

Python&PyQt:捕获最小化事件

[英]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. 我正在使用Python 2.7和PyQt 4.11。 My OS is Windows 7 so I'm pretty sure its not an os issue. 我的操作系统是Windows 7,因此我很确定它不是操作系统问题。 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_()) 

You could use the changeEvent provided by QWidget : 您可以使用QWidget提供的changeEvent

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

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM