了解事件过滤器及其在PyQt中的工作方式

时间:2019-05-16 19:28:47

标签: python python-3.x pyqt pyqt5

我对PyQt相当陌生,我发现了一段代码,用于在主程序关闭时帮助关闭进度栏窗口。

我了解大多数情况,但是我什至不知道为什么为什么要调用installEventFilter / removeEventFilter方法,因为我使用python进行了查找,并且这些函数表明由于传递而没有执行任何操作

    def installEventFilter(self, QObject): # real signature unknown; restored from __doc__
        """ installEventFilter(self, QObject) """
        pass

    def removeEventFilter(self, QObject): # real signature unknown; restored from __doc__
        """ removeEventFilter(self, QObject) """
        pass

我通过删除这些代码行对其进行了测试,并且它按预期运行。我的问题是在这种情况下事件过滤器到底如何工作?


from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication,QProgressBar,QMainWindow,QDialog,QLabel

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(477, 240)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(477, 240))
        MainWindow.setMaximumSize(QtCore.QSize(477, 240))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(160, 100, 141, 23))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
        self.pushButton.setSizePolicy(sizePolicy)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 477, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Show Progress Bar"))

class MCVE(QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.showProgressBar)

    def showProgressBar(self):
        self.prog_win = QDialog(self)
        self.prog_win.resize(400, 100)
        self.prog_win.setFixedSize(self.prog_win.size())
        self.prog_win.setWindowTitle("Loading")
        EventWatcher(self.prog_win)
        self.lbl = QLabel(self.prog_win)
        self.lbl.setText("Please Wait...")
        self.lbl.move(15, 18)
        self.progressBar = QProgressBar(self.prog_win)
        self.progressBar.resize(410, 25)
        self.progressBar.move(15, 40)
        self.progressBar.setRange(0, 1)
        self.prog_win.show()
        self.progressBar.setRange(0, 0)


class EventWatcher(QtCore.QObject):
    def __init__(self, parent):
        super().__init__(parent)
        parent.installEventFilter(self)

    def eventFilter(self, source, event):
        if source is self.parent():
            if event.type() == QtCore.QEvent.Show:
                target = source.parent()
                while target.parent() is not None:
                    target = target.parent()
                source.removeEventFilter(self)
                target.installEventFilter(self)
        elif event.type() == QtCore.QEvent.Close:
            source.closeEvent(event)
            if event.isAccepted():
                self.parent().close()
            return True
        return QtCore.QObject.eventFilter(self, source, event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MCVE()
    window.show()
    sys.exit(app.exec_())

0 个答案:

没有答案