示例#1
0
    def eventFilter(self, obj: QObject, event: QEvent) -> bool:
        """Event filter to process an event for the given destination.

        In this case, this widget listens for Enter events on the application level
        (during this widget's init, this eventFilter is installed on the app instance).

        When the destination object is a QWidget,
        we can try to update this widget's help_text with the destination widget's what's this help text.
        If there is no what's this text, we set the help_text back to its placeholderText.
        """
        if event.type() == QEvent.Enter:
            if isinstance(obj, QWidget):
                help_text = obj.whatsThis()
                # When the obj has help text, let's update the help text widget
                if help_text != "":
                    self._help_display.setText(help_text)
                    # Store a ref to the entered widget so we can maintain the help text if its children don't have any
                    self._cached_entered_widget = weakref.ref(obj)

        if event.type() == QEvent.Leave:
            if isinstance(obj, QWidget):
                # When we leave a widget, if it didn't have help text,
                # we want to check if the previously cached (entered) widget is its ancestor.
                # This will maintain the ancestor's help text in the help text widget when the child has not help text.
                if obj.whatsThis() == "":
                    # Revert to placeholder text when we have left a widget and the cached widget is not its ancestor
                    # (i.e. the previously entered widget with help text is not related).
                    if not self._is_child_of_previously_entered_widget_with_help_text(
                            obj):
                        self._help_display.setText(
                            self._help_display.placeholderText())

        return super(HelpWidget, self).eventFilter(obj, event)