Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved MousePressEvent on a QWebEngineView

    Qt for Python
    3
    3
    1028
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      deleted256 last edited by

      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?

      JonB eyllanesc 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @deleted256 last edited by JonB

        @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-WebEngineView

        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, 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?

        Also see Detect mouse clicks on QWebEngineView

        1 Reply Last reply Reply Quote 0
        • eyllanesc
          eyllanesc @deleted256 last edited by eyllanesc

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

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post