Artur
Artur

Reputation: 73

Overriding an event stops signal/slot from triggering

why overriding keyPressEvent() in QPlainTextEdit, self.clientDataEdit.keyPressEvent = self.clientKeyPressEvent

stops the signal textChanged() in QPlainTextEdit from triggering

The clientKeyPressEvent() method triggers when keys are pressed but QPlainTextEdit stopped updating and textChanged() signal stopped triggering.

If required, I can try posting code but maybe this is something 'simple'

Upvotes: 0

Views: 45

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

Short Answer:

Because they are removing the default behavior, and one of them was to emit that signal.

Long Answer:

Do not modify the methods by doing foo.bar = baz especially in the Qt bindings since they keep a cache of the used methods that can cause you these problems.

If you want to modify the behavior of a method without losing the default behavior then create a class that inherits and calls super from the method:

class PlainTextEdit(QPlainTextEdit):
    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        # TODO

In addition, Qt allows you to listen to the events without the need to override the methods using an event filter:

class Helper(QObject):
    def __init__(self, widget):
        super().__init__(widget)
        self._widget = widget
        self.widget.installEventFilter(self)

    @property
    def widget(self):
        return self._widget

    def eventFilter(self, obj, event):
        if self.widget is obj and event.type() == QEvent.KeyPress:
            print(event.key(), event.text())
        return super().eventFilter(obj, event)
helper = Helper(self.clientDataEdit)

Upvotes: 2

Related Questions