gpt4 book ai didi

python - QWebEngineView 中的 Qt 事件传播

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:33 26 4
gpt4 key购买 nike

我有一个名为 generate_input_event 的函数。我正在尝试使用此功能来模拟 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)

每当我运行我的程序并在我的 QWebEngineView 中突出显示一个输入字段,并调用 generate_input_event 时,它会将该字母输入到输入字段中。

我还设置了一个事件过滤器来捕获除了我人工生成的按键之外的所有按键。

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

但是当我实际运行我的代码时,这是我得到的以下输出:

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

输出看起来是正确的,但是,QWebEngineView 的输入字段实际上从未获得由generate_input_event 人工生成的字母。

附言如果您出于上下文原因希望查看整个文件/项目,请在此处查看此分支/文件:https://github.com/atlas-engineer/next/blob/generate_events/ports/pyqt-webengine/utility.py

最佳答案

Qt Webengine 使用 RenderWidgetHostViewQtDelegateWidget 进行渲染,这是在加载页面后创建的,因此您必须在 load() 或 setHtml() 之后访问它,以便小部件必须发送这些事件。

以下示例将显示 QWebEngineView 和 QLineEdit,在显示两个窗口后,您在 QLineEdit 中键入的内容将显示在 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_())

关于python - QWebEngineView 中的 Qt 事件传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57011407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com