QScrollArea手指触摸屏滑动

网上看到的,外加部分自己搜索的,简单的记录下,笔记做的太杂了,需要整理下

函数的原型为:

[static] Qt::GestureType QScroller::grabGesture(QObject *target, QScroller::ScrollerGestureType scrollGestureType = TouchGesture)

其中参数QObject *target 指需要滑动的界面
其中参数QScroller::ScrollerGestureType scrollGestureType = TouchGesture //默认的手势

QScroller::TouchGesture
The gesture recognizer will only trigger on touch events. Specifically it will react on single touch points when using a touch screen and dual touch points when using a touchpad.

QScroller::LeftMouseButtonGesture
The gesture recognizer will only trigger on left mouse button events.

QScroller::MiddleMouseButtonGesture
The gesture recognizer will only trigger on middle mouse button events.

QScroller::RightMouseButtonGesture
The gesture recognizer will only trigger on right mouse button events.
 

 如果scrollGestureType被设置为TouchGesture,手势就会触发触摸事件。如果它被设置为一个LeftMouseButtonGesture, RightMouseButtonGesture或MiddleMouseButtonGesture,它会触发相应按钮的鼠标事件。

具体使用方法很简单:

注意:此种方式不支持手指在滑动条上进行拖动

QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture);

若需要手指在滑动条上也能够触摸滑动,则需要用下面的方式

QScroller::grabGesture(ui->scrollArea->viewport(),QScroller::LeftMouseButtonGesture);

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现QScrollArea控件的鼠标滑动功能,可以通过重写QScrollArea的鼠标事件来实现。 具体步骤如下: 1. 在自定义的QScrollArea子类中重写鼠标按下、移动和释放事件。 2. 在鼠标按下事件中记录鼠标的初始位置,以便在鼠标移动事件中计算滚动偏移量。 3. 在鼠标移动事件中计算鼠标移动的距离,根据距离计算滚动偏移量,并将滚动偏移量应用到QScrollArea中。 4. 在鼠标释放事件中清除鼠标初始位置。 下面是一个示例代码,实现了在QScrollArea中鼠标滑动的功能: ```python from PyQt5.QtWidgets import QScrollArea, QWidget from PyQt5.QtCore import Qt, QPoint class MyScrollArea(QScrollArea): def __init__(self, parent=None): super(MyScrollArea, self).__init__(parent) self.setWidgetResizable(True) self.setMouseTracking(True) # 开启鼠标跟踪 self._mousePressedPos = QPoint(0, 0) self._mouseMovePos = QPoint(0, 0) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self._mousePressedPos = event.pos() def mouseMoveEvent(self, event): if event.buttons() == Qt.LeftButton: self._mouseMovePos = event.pos() diff = self._mouseMovePos - self._mousePressedPos self._mousePressedPos = self._mouseMovePos self.verticalScrollBar().setValue(self.verticalScrollBar().value() - diff.y()) self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - diff.x()) def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: self._mousePressedPos = QPoint(0, 0) ``` 其中,`_mousePressedPos`变量保存鼠标按下时的位置,`_mouseMovePos`变量保存鼠标移动时的位置。在鼠标移动事件中,计算出鼠标移动距离,并根据距离计算出滚动偏移量,然后将滚动偏移量应用到QScrollArea中。在鼠标释放事件中,清除鼠标初始位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值