QScrollBar 没有自带的判断左右滑动的事件,但可以通过继承 QScrollBar 类并重写其 mouseMoveEvent() 函数来实现:
class MyScrollBar(QScrollBar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.last_pos = None
def mouseMoveEvent(self, event):
if self.last_pos is not None:
# 判断鼠标移动方向
if event.pos().x() > self.last_pos.x():
print("scroll right")
elif event.pos().x() < self.last_pos.x():
print("scroll left")
self.last_pos = event.pos()
super().mouseMoveEvent(event)
在该子类中,记录了上一个鼠标位置 last_pos,并在每次鼠标移动时判断当前位置和上一个位置的相对关系,从而确定滚动条的滑动方向。