【问题标题】:Detect mouse clicks on QWebEngineView检测 QWebEngineView 上的鼠标点击
【发布时间】:2021-04-15 15:59:54
【问题描述】:

如何在 QWebEngineView 小部件中检测鼠标点击?

我试过了,但没有用:

class MyWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.view.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == event.MouseButtonPress:
             print ("Widget click")
        return super(QtWidgets.QMainWindow, self).eventFilter(obj, event)

【问题讨论】:

  • 什么是view?除此之外,你为什么在事件过滤器中使用 super() 和 QMainWindow 参数?
  • view 是一个小部件类型“QWebEngineView”。我不知道为什么我使用 super(),我只是复制它。抱歉英语不好。
  • 我需要什么退货?
  • 问题不在于你返回的是什么,而在于 super 的参数,它应该是当前类:super(MyWin, self),或者,对于 python 3,只是 super() 没有任何参数.
  • 感谢您的帮助。

标签: python pyqt pyqt5 qwebengineview


【解决方案1】:

假设视图是 QWebEngineView 对象并且您想要跟踪它的鼠标事件,那么您应该使用 focusProxy,它是一个处理这些类型事件的内部小部件。另一方面,您必须正确应用继承。

class MyWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__(parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.view.focusProxy().installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.ui.view.focusProxy() and event.type() == event.MouseButtonPress:
            print("Widget click")
        return super(MyWin, self).eventFilter(obj, event)

【讨论】:

  • 我收到此错误:AttributeError: 'NoneType' object has no attribute 'installEventFilter'
  • @Cliffhanger 嗯,尝试在self.ui.view.focusProxy().installEventFilter(self) 之前添加self.show()self.ui.view.show()。如果还是不行则需要提供Ui_MainWindow类的代码来分析错误原因
  • 感谢您的帮助。我在 UI_MainWindow 类中发现了错误,我没有在 self.view 中加载页面。我在 UI_MainWindow 类中创建 QWebEngineWiev 之后添加了这一行:self.view.load(url) 现在它可以工作了。感谢和抱歉英语不好。
  • 我也遇到了错误:AttributeError: 'NoneType' 对象没有属性 'installEventFilter'。那么除了使用focusProxy()之外还有其他解决方法吗?
  • @iMath 尝试在 self.ui.view.focusProxy().installEventFilter(self) 之前添加 self.show() 和 self.ui.view.show()
猜你喜欢
  • 1970-01-01
  • 2020-04-18
  • 2021-09-06
  • 2015-08-03
相关资源
最近更新 更多