我已经弄清楚当窗口发生变化时如何触发事件,但是当我从数据库中读取窗口信息时会发生这种情况,并且当窗口缩小时我想写入数据库,所以,我会喜欢基于单击缩小按钮来触发我的事件,而不是在窗口更改时。
答案 0 :(得分:5)
下面的脚本适用于Linux和Win XP(可能是OSX,但我无法测试):
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_())
答案 1 :(得分:0)
您可以使用QWidget.hideEvent
并检查self.isMinimized()
,因为在关闭窗口时也会调用hideEvent
。例如:
#!/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_())