Qt Event Propagation in QWebEngineView

天大地大妈咪最大 提交于 2020-02-16 05:48:03

问题


I have a function named generate_input_event. I'm trying to use this function to simulate a keypress within a QWebEngineView.

def generate_input_event(window_id, key_code, modifiers, low_level_data, x, y):
    modifiers_flag = create_modifiers_flag(modifiers)
    logging.info("generate input, window: {} code: {}, modifiers {}".format(
        window_id, key_code, modifiers_flag))
    event = QKeyEvent(QEvent.KeyPress, key_code, modifiers_flag)
    event.artificial = True
    event_window = window.get_window(window_id)
    QCoreApplication.sendEvent(event_window.qtwindow, event)

Whenever I run my program and highlight an input field within my QWebEngineView, and invoke generate_input_event it is expected that it will type that letter into the input field.

I also set-up an event filter to capture everything all key presses EXCEPT for my artificially generated ones.

class EventFilter(QWidget):
    def __init__(self, parent=None):
        super(EventFilter, self).__init__(parent)
        qApp.installEventFilter(self)

    def eventFilter(self, obj, event):
        if (event.type() == QEvent.KeyPress and hasattr(event, 'artificial')):
            logging.info("artificial event")
            return False. # send to widget
        elif (event.type() == QEvent.KeyPress and not is_modifier(event.key())):
            modifiers = create_modifiers_list(event.modifiers())
            key_string = create_key_string(event)
            key_code = event.key()
            logging.info("send code: {} string: {} modifiers {}".format(
                key_code, key_string, modifiers))
            return True. # do not forward to widgets
        return False

However when I actually run my code, this is the following output I get:

INFO:root:send code: 65 string: a modifiers ['']
INFO:root:generate input, window: 1 code: 65, modifiers <PyQt5.QtCore.Qt.KeyboardModifiers object at 0x106a4ea58>
INFO:root:artificial event

The output looks correct, HOWEVER, the input field of the QWebEngineView never actually gets a letter that was artificially generated by generate_input_event.

P.S. Should you wish to see the whole of the file/project for reasons of context, please look at this branch/file here: https://github.com/atlas-engineer/next/blob/generate_events/ports/pyqt-webengine/utility.py


回答1:


Qt Webengine uses RenderWidgetHostViewQtDelegateWidget to render and this is created after loading a page so you must access it after load() or setHtml(), so that widget must send those events.

The following example will show a QWebEngineView and a QLineEdit, after showing both windows what you type in the QLineEdit will be shown in the QWebEngineView.

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets


class EventFilter(QtCore.QObject):
    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.KeyPress and hasattr(
            event, "artificial"
        ):
            print("event:", event.key(), event.text())
            return False
        return super().eventFilter(obj, event)


class ForwardKeyEvent(QtCore.QObject):
    def __init__(self, sender, receiver, parent=None):
        super(ForwardKeyEvent, self).__init__(parent)
        self.m_sender = sender
        self.m_receiver = receiver
        self.m_sender.installEventFilter(self)

    def eventFilter(self, obj, event):
        if self.m_sender is obj and event.type() == QtCore.QEvent.KeyPress:
            # self.m_receiver.setFocus()
            new_event = QtGui.QKeyEvent(
                QtCore.QEvent.KeyPress,
                event.key(),
                event.modifiers(),
                event.text(),
            )
            new_event.artificial = True
            QtCore.QCoreApplication.postEvent(self.m_receiver, new_event)
        return super().eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ef = EventFilter()
    app.installEventFilter(ef)

    lineedit = QtWidgets.QLineEdit()
    lineedit.show()

    view = QtWebEngineWidgets.QWebEngineView()
    view.resize(640, 480)
    view.show()

    view.load(QtCore.QUrl("https://www.google.com/"))
    # RenderWidgetHostViewQtDelegateWidget is created after loading a page 
    # so you must access it after load() or setHtml().
    render_widget = view.findChild(QtWidgets.QWidget)
    print(render_widget.metaObject().className())
    assert(render_widget)
    fe = ForwardKeyEvent(lineedit, render_widget)
    sys.exit(app.exec_())



回答2:


The correct way to post and listen to events for a QWebengineview can be demonstrated with the following minimal example:


from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import Qt


class ForwardKeyEvent(QtCore.QObject):
    def __init__(self, sender, receiver, parent=None):
        super(ForwardKeyEvent, self).__init__(parent)
        self.m_sender = sender
        self.m_receiver = receiver
        self.m_sender.installEventFilter(self)

    def eventFilter(self, obj, event):
        if self.m_sender is obj and event.type() == QtCore.QEvent.KeyPress:
            new_event = QtGui.QKeyEvent(
                QtCore.QEvent.KeyPress,
                65,
                Qt.KeyboardModifiers(),
                "a",
            )
            new_event.artificial = True
            QtCore.QCoreApplication.postEvent(self.m_receiver.focusProxy(), new_event)
            return True
        return False


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    lineedit = QtWidgets.QLineEdit()
    lineedit.show()
    view = QtWebEngineWidgets.QWebEngineView()
    view.resize(640, 480)
    view.show()
    view.load(QtCore.QUrl("https://www.google.com/"))
    # RenderWidgetHostViewQtDelegateWidget is created after loading a page
    # so you must access it after load() or setHtml().
    fe = ForwardKeyEvent(lineedit, view)
    sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/57011407/qt-event-propagation-in-qwebengineview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!