开发者

Customize PyQt multi-touch pan gestures on QListWidget

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 us开发者_Python百科ing PyQt 4.8.3 with Python 2.6


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜