Important: Please read the Qt Code of Conduct -

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):
        def eventFilter(self, source, event):
            if event.type() == QEvent.MouseButtonPress:
            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:

    I 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, 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;

    So maybe you have to look at the first child of QWebEngineView?

    Also see Detect mouse clicks on QWebEngineView

  • @benyeyeye use focusProxy():

    from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
    # or
    # from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets
    class BetterWebView(QtWebEngineWidgets.QWebEngineView):
        def __init__(self):
        def eventFilter(self, source, event):
            if (
                self.focusProxy() is source
                and event.type() == QtCore.QEvent.MouseButtonPress
            return super().eventFilter(source, event)
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        view = BetterWebView()

Log in to reply