【发布时间】:2021-11-17 05:07:59
【问题描述】:
我想获得鼠标滚轮值增量。具有 WHeelEvent(self,event) 功能,但在 webview 为焦点时不起作用
我尝试使用其他布局小部件并且可以正常工作
请问我该怎么做才能在我的网络视图中获取它?
【问题讨论】:
-
请提供足够的代码,以便其他人更好地理解或重现问题。
标签: python pyside2 mousewheel
我想获得鼠标滚轮值增量。具有 WHeelEvent(self,event) 功能,但在 webview 为焦点时不起作用
我尝试使用其他布局小部件并且可以正常工作
请问我该怎么做才能在我的网络视图中获取它?
【问题讨论】:
标签: python pyside2 mousewheel
QWebEngineView 使用 focusProxy 来处理鼠标事件,因此您应该使用 eventfilter 监听来自该小部件的事件:
from PySide2.QtCore import QEvent, QUrl
from PySide2.QtWidgets import QApplication
from PySide2.QtWebEngineWidgets import QWebEngineView
class WebView(QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
# required for the focusProxy to be created
self.load(QUrl())
self.focusProxy().installEventFilter(self)
def eventFilter(self, obj, event):
if obj is self.focusProxy() and event.type() == QEvent.Type.Wheel:
print(event.angleDelta())
return super().eventFilter(obj, event)
def main():
app = QApplication()
w = WebView()
w.load(QUrl("https://stackoverflow.com/"))
w.resize(640, 480)
w.show()
app.exec_()
if __name__ == "__main__":
main()
【讨论】: