2016-11-10 30 views
1

我有我自己的「虛擬鍵盤」。我已經將單擊到KeyEvents的按鈕轉換爲QTextEdit等等。我現在的問題是我想對QWebEngineView中的可寫區域執行相同的操作。QWebEngineView,在視圖中發佈KeyEvents

例如,我用我的鍵盤編輯我的QLineEdit,並請求一個網站。 DONE

比方說,我要求谷歌。現在我的Google網站就在我的面前。我需要從我的鍵盤發送的KeyEvents到它的搜索框(盒這是我的QWebEngineView內

現在讓我們點了幾個要點:。

  1. 我使用PyQt5
  2. 正如我讀過的API說我,這是家長要消耗該KeyEvent到corect地方。here
  3. 這個片段說:‘......就像是可能的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」設置。

我曾嘗試只是這樣說:當您發送事件到QWebEngineViews focusProxy代替

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) 

回答

3

這應該工作 - 這樣的事情應該工作:

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

Mannnnnn,如果我有一百萬美元,我現在就把它給你!耶穌基督,你不知道我有多大的腦子想要弄清楚。嘗試瘋狂的事情,而不是那樣簡單的事情!工作完美!我只需要爲這個「QCoreApplication.postEvent(self .__ receiver,event)」改變這一行「self.web_view.keyPressEvent(event)」。 – yurisnm

+0

通過KeyPressEvent我無法訪問受保護的方法。所以我不得不通過QCoreApplication.postEvent發帖,就像我之前和之前所做的那樣。 :d – yurisnm