<legend id='i2sMG'><style id='i2sMG'><dir id='i2sMG'><q id='i2sMG'></q></dir></style></legend>
    • <bdo id='i2sMG'></bdo><ul id='i2sMG'></ul>
    1. <small id='i2sMG'></small><noframes id='i2sMG'>

      <tfoot id='i2sMG'></tfoot>
      <i id='i2sMG'><tr id='i2sMG'><dt id='i2sMG'><q id='i2sMG'><span id='i2sMG'><b id='i2sMG'><form id='i2sMG'><ins id='i2sMG'></ins><ul id='i2sMG'></ul><sub id='i2sMG'></sub></form><legend id='i2sMG'></legend><bdo id='i2sMG'><pre id='i2sMG'><center id='i2sMG'></center></pre></bdo></b><th id='i2sMG'></th></span></q></dt></tr></i><div id='i2sMG'><tfoot id='i2sMG'></tfoot><dl id='i2sMG'><fieldset id='i2sMG'></fieldset></dl></div>

      1. QWebEngineView 中的 Qt 事件传播

        时间:2023-08-04

        <small id='VPYHM'></small><noframes id='VPYHM'>

              <tbody id='VPYHM'></tbody>
            • <bdo id='VPYHM'></bdo><ul id='VPYHM'></ul>
              <i id='VPYHM'><tr id='VPYHM'><dt id='VPYHM'><q id='VPYHM'><span id='VPYHM'><b id='VPYHM'><form id='VPYHM'><ins id='VPYHM'></ins><ul id='VPYHM'></ul><sub id='VPYHM'></sub></form><legend id='VPYHM'></legend><bdo id='VPYHM'><pre id='VPYHM'><center id='VPYHM'></center></pre></bdo></b><th id='VPYHM'></th></span></q></dt></tr></i><div id='VPYHM'><tfoot id='VPYHM'></tfoot><dl id='VPYHM'><fieldset id='VPYHM'></fieldset></dl></div>
            • <tfoot id='VPYHM'></tfoot>

                <legend id='VPYHM'><style id='VPYHM'><dir id='VPYHM'><q id='VPYHM'></q></dir></style></legend>

                  本文介绍了QWebEngineView 中的 Qt 事件传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个名为 generate_input_event 的函数.我正在尝试使用此功能来模拟 QWebEngineView 中的按键.

                  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)
                  

                  每当我运行我的程序并在我的 QWebEngineView 中突出显示一个输入字段,并调用 generate_input_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
                  

                  输出看起来是正确的,但是,QWebEngineView 的输入字段实际上从来没有得到一个由 generate_input_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.

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

                  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

                  推荐答案

                  可以通过以下最小示例演示发布和侦听 QWebengineview 事件的正确方法:

                  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_())
                  

                  这篇关于QWebEngineView 中的 Qt 事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:根据来自scrapy的信号更新主线程内的PyQt5 Gui 下一篇:python.exe:没有名为 pyuic5 的模块

                  相关文章

                  最新文章

                  <i id='qtLC8'><tr id='qtLC8'><dt id='qtLC8'><q id='qtLC8'><span id='qtLC8'><b id='qtLC8'><form id='qtLC8'><ins id='qtLC8'></ins><ul id='qtLC8'></ul><sub id='qtLC8'></sub></form><legend id='qtLC8'></legend><bdo id='qtLC8'><pre id='qtLC8'><center id='qtLC8'></center></pre></bdo></b><th id='qtLC8'></th></span></q></dt></tr></i><div id='qtLC8'><tfoot id='qtLC8'></tfoot><dl id='qtLC8'><fieldset id='qtLC8'></fieldset></dl></div>
                    <bdo id='qtLC8'></bdo><ul id='qtLC8'></ul>

                    1. <small id='qtLC8'></small><noframes id='qtLC8'>

                      <legend id='qtLC8'><style id='qtLC8'><dir id='qtLC8'><q id='qtLC8'></q></dir></style></legend><tfoot id='qtLC8'></tfoot>