在 QListWidget 上自定义 PyQt 多点触控平移手势

发布于 2024-10-18 04:43:14 字数 314 浏览 9 评论 0原文

我在编写一个简单的记事本应用程序来自学基本的 Python/PyQt 时遇到问题。 具体来说,我想知道如何更改 QListWidget 上的多点触控平移手势灵敏度。

就像现在一样,当我用两根手指上下拖动时,似乎列表随着我用手指移动的每个像素而向上/向下移动一步。这不是我自己实现的,它似乎对于列表小部件来说是开箱即用的,

我希望移动模仿我手指的速度,即每个 x*height_of_item_in_pixels 向上/向下移动小部件项目。在不对手势系统进行重大黑客攻击的情况下,这是否可行? 我该怎么办呢?

我正在使用 PyQt 4.8.3 和 Python 2.6

I'm having a problem with a simple Notepad application I'm writing to teach myself basic Python/PyQt.
Specifically, I want to know how to change the multi-touch pan gesture sensitivity on a QListWidget.

As it is now, when I drag up and down with 2 fingers, it seems like the list is moving up/down one step for each pixel I move with my fingers. This is nothing I've implemented myself, it seems to work out of the box for list widgets

I want the movement to mimic the speed of my fingers i.e one move up/down of the widget items for every x*height_of_item_in_pixels. Is this doable without major hacking into the gesture system?
How would I go about this?

I'm using PyQt 4.8.3 with Python 2.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-10-25 04:43:15

有点晚了,但也许我可以帮助其他偶然发现这个问题的人:

解决方案是将 QListWidget 的滚动模式设置为 ScrollPerPixel (而不是默认的 < code>ScrollPerItem):

list_widget.setVerticalScrollMode(list_widget.ScrollPerPixel)

一个最小的示例:

import sys
from PyQt5 import QtWidgets

# create app
app = QtWidgets.QApplication(sys.argv)

# create list widget
list_widget = QtWidgets.QListWidget()

# populate list widget with dummy items
for index in range(100):
    list_widget.addItem(QtWidgets.QListWidgetItem('item {}'.format(index)))

# THIS IS THE IMPORTANT PART: set the scroll mode
list_widget.setVerticalScrollMode(list_widget.ScrollPerPixel)

# OPTIONAL: enable pan by mouse (and one-finger pan)
QtWidgets.QScroller.grabGesture(list_widget.viewport(), 
                                QtWidgets.QScroller.LeftMouseButtonGesture)

# show the list widget
list_widget.show()

# run the event loop
app.exec_()

注意:此示例还演示了如何使用鼠标实现平移,这对于在没有触摸屏的设备上进行测试非常有用。此外,除了默认的两指平移手势之外,鼠标平移设置还支持单指平移(至少在 Windows 上,在 Surface Pro 平板电脑上进行了测试)。

Quite a bit late, but maybe I can help others who stumble upon this question:

The solution is to set the scroll mode for the QListWidget to ScrollPerPixel (instead of the default ScrollPerItem):

list_widget.setVerticalScrollMode(list_widget.ScrollPerPixel)

A minimal example:

import sys
from PyQt5 import QtWidgets

# create app
app = QtWidgets.QApplication(sys.argv)

# create list widget
list_widget = QtWidgets.QListWidget()

# populate list widget with dummy items
for index in range(100):
    list_widget.addItem(QtWidgets.QListWidgetItem('item {}'.format(index)))

# THIS IS THE IMPORTANT PART: set the scroll mode
list_widget.setVerticalScrollMode(list_widget.ScrollPerPixel)

# OPTIONAL: enable pan by mouse (and one-finger pan)
QtWidgets.QScroller.grabGesture(list_widget.viewport(), 
                                QtWidgets.QScroller.LeftMouseButtonGesture)

# show the list widget
list_widget.show()

# run the event loop
app.exec_()

Note: This example also shows how to implement panning using the mouse, which is useful for testing on devices without touch-screen. Moreover, the mouse pan setting also enables one-finger pan, in addition to the default two-finger pan gesture (at least on Windows, tested on Surface Pro tablet).

因为看清所以看轻 2024-10-25 04:43:15

此功能很可能存在,因为多点触控事件被操作系统解释为某种类型的滚动,并且 QT 正在接收滚动操作,而不是直接接收多点触控操作。

More than likely this functionality exists because the multitouch event is being interpreted by your operating system as a scroll of some type and QT is recieving the scroll action, rather than the multitouch action directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文