Unsolved MousePressEvent on a QWebEngineView
-
I'm trying to run a function when the mouse is clicked on a QWebEngineView, so far i have used the following code:
class BetterWebView(QWebEngineView): def __init__(self): super().__init__() self.installEventFilter(self) def eventFilter(self, source, event): print(event.type()) if event.type() == QEvent.MouseButtonPress: print("-") self.parent().openCardInfo() return super().eventFilter(source, event)
When i run it, it prints the event.type(), but clicking on the QWebEngineView doesn't seem to call a QEvent.MouseButtonPress.
Any thoughts on why it doesn't work / how i can get around it?
-
@benyeyeye
I don't know if it's right, but this doesn't look very good for you: https://www.qtcentre.org/threads/62149-Mouseevents-on-a-QwebView-or-WebEngineViewI am asking because QWebEngineView overwrites the event() method (according to the documentation), so it might not call the QWidget event handlers at all
I've debuged the Events on the WebEngineView.
Only the Enter Event 10) and the ContextMenu (Event 82,68,69) Events are ermited.However, it may be more complex than that. In, say, https://stackoverflow.com/questions/43067680/qt-webengine-simulate-mouse-event a user states
QWebEngineView webView = new QWebEngineView(); // You need to find the first child widget of QWebEngineView. It can accept user input events. QWidget* eventsReciverWidget = nullptr; foreach(QObject* obj, webView->children()) { QWidget* wgt = qobject_cast<QWidget*>(obj); if (wgt) { eventsReciverWidget = wgt; break; } }
So maybe you have to look at the first child of
QWebEngineView
? -
@benyeyeye use
focusProxy()
:from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets # or # from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets class BetterWebView(QtWebEngineWidgets.QWebEngineView): def __init__(self): super().__init__() self.load(QtCore.QUrl()) self.focusProxy().installEventFilter(self) def eventFilter(self, source, event): if ( self.focusProxy() is source and event.type() == QtCore.QEvent.MouseButtonPress ): print("ok") return super().eventFilter(source, event) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) view = BetterWebView() view.load(QtCore.QUrl("https://www.qt.io")) view.show() sys.exit(app.exec_())