Created
October 19, 2015 22:10
-
-
Save chipolux/a21f6a2349ec0c084216 to your computer and use it in GitHub Desktop.
Simple Kinetic Scroll Example Using PyQt (Only Works On Qt5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtWidgets import ( | |
QApplication, | |
QFormLayout, | |
QGridLayout, | |
QLabel, | |
QScrollArea, | |
QScroller, | |
QWidget, | |
) | |
class MainWindow(QWidget): | |
def __init__(self, parent=None): | |
super().__init__(parent) | |
scroll_area = QScrollArea() | |
layout = QGridLayout(self) | |
layout.addWidget(scroll_area) | |
scroll_widget = QWidget() | |
scroll_layout = QFormLayout(scroll_widget) | |
for i in range(200): | |
scroll_layout.addRow(QLabel('Label #{}'.format(i))) | |
scroll_area.setWidget(scroll_widget) | |
QScroller.grabGesture( | |
scroll_area.viewport(), QScroller.LeftMouseButtonGesture | |
) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
main_window = MainWindow() | |
main_window.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
QScroller
is a new class in Qt5 that makes kinetic scroll easy with sensible defaults for supported Qt platforms.One gotcha when using with a
QScrollArea
is that grabbingQScroller.TouchGesture
will likely only work if the platform supports touch in a special way. It does not appear to work correctly on Window's devices with touchscreens, but the extent of my testing is limited.QScroller.LeftMouseButtonGesture
seems to work perfectly while also allowing you to test on non-touch devices.