[英]How to detect mouse hover event in PySide6 widget
I am trying to create a custom image viewer widget with zoom to the mouse position.我正在尝试创建一个缩放到鼠标位置的自定义图像查看器小部件。 So far I have managed to detect mouse scroll events, but I cannot detect a mouse hover events so I could determine a mouse position to zoom to.
到目前为止,我已经设法检测到鼠标滚动事件,但我无法检测到鼠标悬停事件,因此我可以确定要缩放到的鼠标位置。
I seems to me that the mouse hover event is not even occurring.在我看来,鼠标悬停事件甚至没有发生。 I tried to print out all events, but the
QHoverEvent
is just not there.我试图打印出所有事件,但
QHoverEvent
不存在。 The only event occurring during mouse hover is QEvent::ToolTip
which has the mouse position but it only occurs after the mouse hovering stops and it has quite a delay (~0.5s).鼠标悬停期间发生的唯一事件是
QEvent::ToolTip
,它具有鼠标位置,但仅在鼠标悬停停止后发生,并且有相当长的延迟(约 0.5 秒)。
Here is the code:这是代码:
import sys
from PySide6 import QtWidgets
from PySide6.QtWidgets import QDialog, QVBoxLayout, QLabel
from PySide6.QtGui import QPixmap
from PySide6.QtCore import Qt
from PIL.ImageQt import ImageQt
class ImageViewer(QDialog):
def eventFilter(self, object, event):
print("Event:" + str(event))
if str(event) == '<PySide6.QtGui.QWheelEvent(Qt::NoScrollPhase, pixelDelta=QPoint(0,0), angleDelta=QPoint(0,-120))>':
print("detected zoom out")
if str(event) == '<PySide6.QtGui.QWheelEvent(Qt::NoScrollPhase, pixelDelta=QPoint(0,0), angleDelta=QPoint(0,120))>':
print("detected zoom in")
if str(event) == '<PySide6.QtCore.QEvent(QEvent::ToolTip)>':
print("detected tooltip")
return True
def __init__(self, img: ImageQt):
super().__init__()
self.setWindowTitle('Image viewer example')
self.imageWidget = QLabel()
self.imageWidget.setAlignment(Qt.AlignCenter)
self.imageWidget.setPixmap(QPixmap.fromImage(img))
self.layout = QVBoxLayout()
self.layout.addWidget(self.imageWidget)
self.setLayout(self.layout)
self.imageWidget.installEventFilter(self)
if __name__ == '__main__':
# prepare app
app = QtWidgets.QApplication(sys.argv)
# create viewer widget
imageViewer = ImageViewer(ImageQt("img.png"))
imageViewer.show()
# close app
sys.exit(app.exec())
I am able to detect the mouse scrolling, enter widget, leave, mouse button press/release, mouse move (with mouse pressed).我能够检测到鼠标滚动、输入小部件、离开、鼠标按钮按下/释放、鼠标移动(按下鼠标)。 But the mouse hover is just not there.
但是鼠标悬停不存在。 Could someone tell me how to detect the mouse hover event (with mouse position info)?
有人可以告诉我如何检测鼠标悬停事件(带有鼠标位置信息)吗?
它适用于__init__()
构造函数中的self.setAttribute(Qt.WidgetAttribute.WA_Hover)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.