【问题标题】:QWebEngineView, post KeyEvents inside the ViewQWebEngineView,在 View 内发布 KeyEvents
【发布时间】:2017-03-24 21:08:35
【问题描述】:

我有自己的“虚拟键盘”。我已经将点击的按钮转换为 KeyEvents 并将其传递给 QTextEdit 等等。我现在的问题是我想对 QWebEngineView 内的可写区域执行相同的操作。

例如,我使用键盘来编辑我的 QLineEdit,并请求一个网站。 完成

假设我请求了谷歌。现在我的 Google 网站就在我面前。我需要将 KeyEvents 从我的键盘发送到它的搜索框。(在我的 QWebEngineView 内的框。

现在让我们指出几点:

  1. 我正在使用 PyQt5
  2. 正如我所读到的,API 告诉我它的父级应该将 KeyEvent 消耗到正确的位置。 here
  3. 这个 sn-p 表示“......就像使用 QtWebKit 一样。”
  4. 我现在发现没有 QtWebKit,而是 Chromium。(也许这就是我无法发布这些事件的原因)

这就是我所拥有的,例如将 KeyEvents 模拟到我的 QEditText 等等..

from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QEvent
from PyQt5.QtCore import QSize
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QPushButton


class KeyboardKey(QPushButton):

    __path = ""

    __size = [30, 30]
    __style = ""
    __icon_on = ""
    __icon_off = ""
    __auto_repeat = True
    __receiver = None
    __key = None
    __str_key = None

    def __init__(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
        super(KeyboardKey, self).__init__()
        self.__size = size
        self.__style = style
        self.__icon_on = str_icon_on
        self.__icon_off = str_icon_off
        self.__auto_repeat = auto_repeat
        self.__receiver = receiver
        self.__key = key
        self.__str_key = str_key
        self.set_up_button(style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key)

    def set_up_button(self, style, str_icon_on, str_icon_off, auto_repeat, size, receiver, key, str_key):
        self.__size = size
        self.__style = style
        self.__icon_on = str_icon_on
        self.__icon_off = str_icon_off
        self.__auto_repeat = auto_repeat
        self.__receiver = receiver
        self.__key = key
        self.__str_key = str_key
        self.setText(str_key)

        self.setFixedSize(size[0], size[1])
        self.setStyleSheet(style)
        self.setIconSize(QSize(size[0], size[1]))
        self.setIcon(QIcon(self.__path + str_icon_off + ".png"))
        self.setAutoRepeat(auto_repeat)
        pixmap = QPixmap(self.__path + str_icon_off + ".png")
        self.setMask(pixmap.mask())
        self.pressed.connect(self.key_pressed)
        self.released.connect(self.key_released)

    def set_receiver(self, receiver):
        self.__receiver = receiver

    def key_pressed(self):
        self.setStyleSheet("""
                            border-width: 5px;
                            border-color: rgb(37,43,52);
                            color: white;
                            background-color: rgb(0,187,255);
                        """,)

    def key_released(self):
        event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
                          "a", False)
        # self.__receiver is my QEditText/QLineEdit/...
        self.__receiver.keyPressEvent(event)

最后一部分是我在“self.__receiver”上发布我的事件的部分。此接收器始终为调用它的“QWidget”设置。

我只是想说:

def key_released(self):
    event = QKeyEvent(QEvent.KeyPress, Qt.Key_A, Qt.NoModifier,
                      "a", False)
    # web_view is my QWebEngineView... but it won't consume. Or maybe it's not consuming to the right place.
    self.web_view.keyPressEvent(event)

【问题讨论】:

    标签: qt pyqt qevent qkeyevent


    【解决方案1】:

    当您将事件发送到 QWebEngineViews focusProxy 时,这应该可以工作 - 这样的事情应该可以工作:

    recipient = self.web_view.focusProxy()
    QApplication.postEvent(recipient, event)
    

    【讨论】:

    • Mannnnnn,如果我有一百万美元,我现在就给你!天哪,你不知道我为了弄清楚这一点而伤透了我的大脑。尝试过疯狂的事情,而不是那么简单的事情!完美运行!我只需要为此“QCoreApplication.postEvent(self.__receiver, event)”更改“self.web_view.keyPressEvent(event)”这一行。
    • 通过 KeyPressEvent 我无法访问受保护的方法。所以我必须通过 QCoreApplication.postEvent 发布,就像我之前所做的那样,正如你所说的那样。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多