Qt Event Propagation in QWebEngineView - python

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)
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
The output looks correct, HOWEVER, the input field of the QWebEngineView never actually gets a letter that was artificially generated by generate_input_event.
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

Qt Webengine uses RenderWidgetHostViewQtDelegateWidget to render and this is created after loading a page so you must access it after load() or setHtml(), so that widget must send those events.
The following example will show a QWebEngineView and a QLineEdit, after showing both windows what you type in the QLineEdit will be shown in the 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_())

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

Related

Signals or events for QMenu tear-off

Is there a signal/event I can make use of for the QMenu tear-off?
I have a QMenu subclass in which it has .setTearOffEnabled(True), but I would like to set this tear-off to always be on the top if the user clicks on the tear-off 'bar'.
I am unable to utilise QtCore.Qt.WindowStaysOnTopHint, as this will result in my menu already being in the tear-off state.
For example: if my main tool area is bigger than the tear-off, and I click on my main tool, the tear-off window will be behind it.
In the following code the clicked signal is emitted when the tear off (dotted lines) is pressed:
import sys
from PyQt5 import QtCore, QtWidgets
class Menu(QtWidgets.QMenu):
clicked = QtCore.pyqtSignal()
def mouseReleaseEvent(self, event):
if self.isTearOffEnabled():
tearRect = QtCore.QRect(
0,
0,
self.width(),
self.style().pixelMetric(
QtWidgets.QStyle.PM_MenuTearoffHeight, None, self
),
)
if tearRect.contains(event.pos()):
self.clicked.emit()
QtCore.QTimer.singleShot(0, self.after_clicked)
super(Menu, self).mouseReleaseEvent(event)
#QtCore.pyqtSlot()
def after_clicked(self):
tornPopup = None
for tl in QtWidgets.QApplication.topLevelWidgets():
if tl.metaObject().className() == "QTornOffMenu":
tornPopup = tl
break
if tornPopup is not None:
print("This is the tornPopup: ", tornPopup)
tornPopup.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow(parent=None)
menu = Menu("Menu", tearOffEnabled=True)
menu.clicked.connect(lambda: print("clicked"))
w.menuBar().addMenu(menu)
for i in range(5):
action = QtWidgets.QAction("action{}".format(i), w)
menu.addAction(action)
w.show()
sys.exit(app.exec_())
When a menu is torn off, it is hidden and Qt replaces it with a copy created from an internal subclass of QMenu. So to set the WindowStaysOnTopHint on a torn off menu, you would first need to find a way to get a reference to it. One way to do this would be to set an event-filter on the application object and watch for child events of the right type:
class MenuWatcher(QtCore.QObject):
def __init__(self, parent=None):
super().__init__(parent)
QtWidgets.qApp.installEventFilter(self)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.ChildAdded and
event.child().metaObject().className() == 'QTornOffMenu'):
event.child().setWindowFlag(QtCore.Qt.WindowStaysOnTopHint)
return super().eventFilter(source, event)
This class will operate on all torn-off menus created in the application.
However, if the event-filtering was done by the source menu class, its own torn-off menu could be identified by comparing menu-items:
class Menu(QtWidgets.QMenu):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setTearOffEnabled(True)
QtWidgets.qApp.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.ChildAdded:
child = event.child()
if (child.metaObject().className() == 'QTornOffMenu' and
all(a is b for a, b in zip(child.actions(), self.actions()))):
child.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint)
return super().eventFilter(source, event)

How to get the return value of mousePressEvent of other widget in PyQt5

How can I get the value returned from mousePressEvent of a widget and apply it to another widget?
Here's the widget with the mousePressEvent:
class Text(QTextEdit):
...
def mousePressEvent(self, event):
if event.button()==Qt.LeftButton:
return "test"
Now I want to use the string returned from the event and apply it to another widget:
class OtherWidget(QWidget):
...
self.label=QLabel()
self.label.setText(???) # <=== How to put the string here?
...
How can I do that? I have tried the following but it does not work.
self.label.setText(Text().mousePressEvent())
The events do not return anything for what you point out is impossible, Qt to send information asynchronously uses the signals, in the next part I show an example:
from PyQt5 import QtCore, QtWidgets
class Text(QtWidgets.QTextEdit):
mousePressSignal = QtCore.pyqtSignal(str)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
text = "test: {}-{}".format(event.pos().x(), event.pos().y())
self.mousePressSignal.emit(text)
class OtherWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(OtherWidget, self).__init__(parent)
self.label = QtWidgets.QLabel()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
#QtCore.pyqtSlot(str)
def setText(self, text):
self.label.setText(text)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
t = Text()
o = OtherWidget()
o.resize(640, 480)
t.mousePressSignal.connect(o.setText)
t.show()
o.show()
sys.exit(app.exec_())

installEventFilter in PyQt5

I'm trying to implement an event in PyQT5, but i get this error:
TypeError: installEventFilter(self, QObject): argument 1 has unexpected type 'MainWindow_EXEC'
This is my code
import sys
from time import sleep
from PyQt5 import QtCore, QtWidgets
from view_cortes2 import Ui_cortes2enter
class MainWindow_EXEC():
def __init__(self):
app = QtWidgets.QApplication(sys.argv)
cortes2 = QtWidgets.QMainWindow()
self.ui = Ui_cortes2()
self.ui.setupUi(cortes2)
self.flag = 0
self.ui.ledit_corteA.installEventFilter(self)
self.ui.ledit_corteB.installEventFilter(self)
self.ui.buttonGroup.buttonClicked.connect(self.handleButtons)
cortes2.show()
sys.exit(app.exec_())
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.FocusIn and source is self.ui.ledit_corteA):
print("A")
self.flag = 0
if (event.type() == QtCore.QEvent.FocusIn and source is self.ui.ledit_corteA):
print("B")
self.flag = 1
return super(cortes2, self).eventFilter(source, event)
if __name__ == "__main__":
MainWindow_EXEC()
The event that I'm trying to add is when I focus in a TextEdit it changes the value of a flag. If i change
self.ui.ledit_corteA.installEventFilter(self)
by
self.ui.ledit_corteA.installEventFilter(cortes2)
I works, but never changes the value of my flag.
Please help.
installEventFilter expects a QObject, and in your case MainWindow_EXEC is not.
If you are using the Qt Designer design it is recommended to create a new class that inherits from the appropriate widget and use the class provided by Qt Designer to fill it as shown below:
import sys
from PyQt5 import QtCore, QtWidgets
from view_cortes2 import Ui_cortes2
class MainWindow(QtWidgets.QMainWindow, Ui_cortes2):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.flag = 0
self.ledit_corteA.installEventFilter(self)
self.ledit_corteB.installEventFilter(self)
#self.buttonGroup.buttonClicked.connect(self.handleButtons)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.FocusIn and source is self.ledit_corteA:
print("A")
self.flag = 0
if event.type() == QtCore.QEvent.FocusIn and source is self.ledit_corteB:
print("B")
self.flag = 1
return super(MainWindow, self).eventFilter(source, event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
References:
http://pyqt.sourceforge.net/Docs/PyQt5/designer.html

How to prevent a key from functioning inside QPlainTextEdit

Suppose I have a text edit in my GUI interface, and i press a key like a, then i should not get 'a' written on my text edit.
I can add my own function to a key press event, but i cannot prevent it from doing the default mechanism.
Help me with this please.
You can prevent the default keyPressEvent behaviour for a specific key, like this:
class CodeEditor(QtWidgets.QPlainTextEdit):
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_A:
# do nothing
event.accept()
else:
# do the default
super().keyPressEvent(event)
You can install an eventFilter and prevent the key from being transmitted.
import sys
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
from PyQt5.QtCore import QObject, Qt, QEvent
class Helper(QObject):
def disable_key(self, w, key):
self.m_w = w
self.m_w.installEventFilter(self)
self.m_key = key
def eventFilter(self, obj, event):
if obj == self.m_w and event.type() == QEvent.KeyPress:
if event.key() == self.m_key:
return True
return QObject.eventFilter(self, obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
helper = Helper()
view = QPlainTextEdit()
helper.disable_key(view, Qt.Key_A)
view.show()
sys.exit(app.exec_())
or by letter:
import sys
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
from PyQt5.QtCore import QObject, Qt, QEvent
class Helper(QObject):
def disable_key(self, w, letter):
self.m_w = w
self.m_w.installEventFilter(self)
self.m_letter = letter
def eventFilter(self, obj, event):
if obj == self.m_w and event.type() == QEvent.KeyPress:
if event.text() == self.m_letter:
return True
return QObject.eventFilter(self, obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
helper = Helper()
view = QPlainTextEdit()
helper.disable_key(view, "a")
view.show()
sys.exit(app.exec_())

Use keypress to input text in QLineEdit AND emit signal in PyQt4

Using PyQt 4.8 and Python 3.3
I'm using a modified version of this example: whereas this example emits a signal on tab press and adds arbitrary text to the second QLineEdit, I want my script to emit a signal on any keypress, add arbitrary signal text to the 2nd QLineEdit, and add the typed character to the 1st QLineEdit (assuming it's a valid ASCII character).
Whenever I try to use any keypress as a signal, I can no longer grab that text to input into QLineEdit. Here's what I have so far and where I'm stuck:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
####################################################################
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
####################################################################
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
self.la = QLabel("Type in this box:")
self.le = MyLineEdit()
self.la2 = QLabel("\nLook here:")
self.le2 = QLineEdit()
self.char = MyLineEdit.char # HOW CAN I GET THIS WORKING?
# layout
layout = QVBoxLayout()
layout.addWidget(self.la)
layout.addWidget(self.le)
layout.addWidget(self.la2)
layout.addWidget(self.le2)
self.setLayout(layout)
# connections
self.connect(self.le, SIGNAL("keyPressed"),
self.update)
def update(self):
newtext1 = self.le.text() + self.char
newtext2 = self.le2.text() + "kP "
self.le.setText(newtext1)
self.le2.setText(newtext2)
####################################################################
class MyLineEdit(QLineEdit):
def __init__(self, *args):
QLineEdit.__init__(self, *args)
def event(self, event):
if (event.type() == QEvent.KeyPress):
self.emit(SIGNAL("keyPressed"))
self.char = "%c" % (event.key())
return True
return QLineEdit.event(self, event)
####################################################################
if __name__ == "__main__":
main()
Any and all help is greatly appreciated. Is there something within PyQt4 that allows me to use a keypress as both a signal and input text, or is my Python off?
Problem1: you are emitting the signal before setting self.char:
class MyLineEdit(QLineEdit):
def __init__(self, *args):
QLineEdit.__init__(self, *args)
self.char = ""
def event(self, event):
if (event.type() == QEvent.KeyPress):
self.char = "%c" % (event.key()) #this line above the next
self.emit(SIGNAL("keyPressed"))
return True
return QLineEdit.event(self, event)
Problem 2: use the char value in your MyLineEdit object:
def update(self):
newtext1 = self.le.text() + self.le.char
newtext2 = self.le2.text() + "kP "
self.le.setText(newtext1)
self.le2.setText(newtext2)
Finally you don't need self.char on MyWindow

Categories

Resources