Skip to content

EventCatcher

Qt Base Class: QObject

Signature: QObject(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
BaseEventFilter
prettyqt.eventfilters.baseeventfilter

⋔ Inheritance diagram

graph TD
  1473367055024["eventfilters.EventCatcher"]
  1473367044288["eventfilters.BaseEventFilter"]
  1473299782816["core.Object"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473367044288 --> 1473367055024
  1473299782816 --> 1473367044288
  1473299815024 --> 1473299782816
  140713234304496 --> 1473299815024
  1473288842240 --> 1473299782816
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: BaseEventFilter

Source code in prettyqt\eventfilters\eventcatcher.py
class EventCatcher(eventfilters.BaseEventFilter):
    ID = "eventcatcher"

    caught = core.Signal(core.QEvent)

    def __init__(
        self,
        include: core.QEvent.Type
        | core.event.TypeStr
        | Sequence[core.QEvent.Type | core.event.TypeStr]
        | None = None,
        exclude: core.QEvent.Type
        | core.event.TypeStr
        | Sequence[core.QEvent.Type | core.event.TypeStr]
        | None = None,
        do_filter: bool | Callable[[core.QEvent], bool] = False,
        **kwargs,
    ):
        super().__init__(**kwargs)
        match include:
            case str():
                self.include = [core.event.TYPE[include]]
            case Sequence():
                self.include = [
                    core.event.TYPE[i] if isinstance(i, str) else i for i in include
                ]
            case core.QEvent.Type():
                self.include = [include]
            case None:
                self.include = []
            case _:
                raise ValueError(include)
        match exclude:
            case str():
                self.exclude = [core.event.TYPE[exclude]]

            case Sequence():
                self.exclude = [
                    core.event.TYPE[i] if isinstance(i, str) else i for i in exclude
                ]
            case core.QEvent.Type():
                self.exclude = [exclude]
            case None:
                self.exclude = []
            case _:
                raise ValueError(exclude)
        self.do_filter = do_filter

    def eventFilter(self, source: core.QObject, event: core.QEvent) -> bool:
        if (not self.include or event.type() in self.include) and (
            not self.exclude or event.type() not in self.exclude
        ):
            self.caught.emit(event)
            # logger.debug(f"{source!r}: {event.type()!r}")
            val = self.do_filter(event) if callable(self.do_filter) else self.do_filter
            if not isinstance(val, bool):
                logger.warning(f"Non-bool value returned for {source!r}: {val} ")
            return bool(val)
        return False

⌗ Property table

Qt Property Type Doc
objectName QString