Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. MousePressEvent on a QWebEngineView
Forum Updated to NodeBB v4.3 + New Features

MousePressEvent on a QWebEngineView

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 2.1k Views
  • 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 Offline
    D Offline
    deleted256
    wrote on last edited by
    #1

    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?

    JonBJ eyllanescE 2 Replies Last reply
    0
    • D deleted256

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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
      0
      • D deleted256

        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?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by eyllanesc
        #3

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

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved