我有自己的“虚拟键盘”。我已经必须将单击的按钮转换为KeyEvents,并将其交付给QTextEdit,依此类推。我现在的问题是,我想对QWebEngineView内的可写区域执行相同的操作。

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

假设我请求了Google。现在,我现在就拥有Google网站。我需要将KeyEvents从我的键盘发送到它的搜索框。(位于QWebEngineView内的框。

现在让我们指出几点:

  • 我正在使用PyQt5
  • 如我所读,API告诉我它的父级应该将KeyEvent消耗到corect位置。 here
  • 此代码段显示“...就像使用QtWebKit一样。”
  • 我现在已经看到不再有QtWebKit,所以用Chromium代替。 (也许这就是我不打算发布这些事件的原因)

    例如,这就是我模拟到QEditText的KeyEvent以及诸如此类的东西。
    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)
    

    最佳答案

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

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

    关于qt - QWebEngineView,在 View 内发布KeyEvents,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40534374/

  • 10-13 02:39