带有eventFilter的PyQt4信号和插槽

时间:2015-01-02 21:43:42

标签: python event-handling pyqt pyqt4 signals-slots

我无法接收事件过滤器发送的信号。在下面的示例中,按下按下的信号/插槽工作正常,并且焦点输出滤波器信号发出OK。然而,焦点输出信号没有被截取,并且插槽不会触发。知道我做错了吗?

from PyQt4.QtCore import SIGNAL, QObject, QEvent
from PyQt4.QtGui import QApplication, QLabel, QWidget, QLineEdit, QPushButton, QTextEdit, QVBoxLayout

class SignalOnFocus(QWidget):
    def __init__(self):
        super(SignalOnFocus, self).__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Type in some text then push button")
        self.inputLineEdit1 = QLineEdit()
        self.inputLineEdit1.setObjectName("inputLineEdit1")

        self.focusOutFilter = FocusOutFilter()
        self.inputLineEdit1.installEventFilter(self.focusOutFilter)
        self.connect(self.inputLineEdit1, SIGNAL("focus_out"),
                 self.focusLost)
        self.inputLineEdit2 = QLineEdit()
        self.inputLineEdit2.setObjectName("inputLineEdit2")
        self.mousePressedFilter = MousePressedFilter()
        self.inputLineEdit2.installEventFilter(self.mousePressedFilter)
        self.connect(self.inputLineEdit2, SIGNAL("mouse_clicked"), self.mouseClicked)
        self.button1 = QPushButton("Press me")
        self.button1.setObjectName("button1")
        self.connect(self.button1, SIGNAL("clicked()"), self.buttonPressed)
        self.textEdit = QTextEdit()
        layout.addWidget(self.label)
        layout.addWidget(self.inputLineEdit1)
        layout.addWidget(self.inputLineEdit2)
        layout.addWidget(self.button1)  
        layout.addWidget(self.textEdit)
        self.setLayout(layout)

    def mouseClicked(self):
        self.textEdit.append(" mouse clicked")

    def buttonPressed(self):
        self.textEdit.append(" button pressed")

    def focusLost(self):
        self.textEdit.append(" focus_out")

class MousePressedFilter(QObject):
    def eventFilter(self, widget, event):
        if event.type() == QEvent.MouseButtonPress:
            print("--eventFilter() mouse_clicked on "+str(widget.objectName()))
            self.emit(SIGNAL("mouse_clicked"))
            return False
        else:
            return False

class FocusOutFilter(QObject):
    def eventFilter(self, widget, event):
        if event.type() == QEvent.FocusOut:
            print("--eventFilter() focus_out on "+str(widget.objectName()))
            self.emit(SIGNAL("focus_out"))
            return False
        else:
            return False

if __name__ == "__main__":
    app = QApplication([])
    form = SignalOnFocus()
    form.show()
    app.exec_()

1 个答案:

答案 0 :(得分:6)

过滤器对象正在发出信号,因此在连接它们时需要指定:

    self.connect(self.focusOutFilter, SIGNAL("focus_out"), self.focusLost)
    ...
    self.connect(self.mousePressedFilter, SIGNAL("mouse_clicked"), self.mouseClicked)

但请认真考虑摆脱用于连接信号的丑陋,旧式语法。 Qt4的官方支持即将在今年结束,PyQt5已经使旧式语法完全过时。

使用new-style syntax,您的示例如下所示:

from PyQt4.QtCore import pyqtSignal, QObject, QEvent

class SignalOnFocus(QWidget):
    def __init__(self):
        ...    
        self.focusOutFilter = FocusOutFilter()
        self.inputLineEdit1.installEventFilter(self.focusOutFilter)
        self.focusOutFilter.focusOut.connect(self.focusLost)

class FocusOutFilter(QObject):
    focusOut = pyqtSignal()

    def eventFilter(self, widget, event):
        if event.type() == QEvent.FocusOut:
            print("--eventFilter() focus_out on " + widget.objectName())
            self.focusOut.emit()

我希望你会同意看起来更具可读性(并且更容易做对)。

(另请注意,如果您使用Python 3和PyQt,默认情况下,任何返回QString的Qt方法都是automatically converted to a python string - 所以您不需要使用{{ 1}})。