Skip to content

Instantly share code, notes, and snippets.

@chipolux
Created October 19, 2015 22:10
Show Gist options
  • Save chipolux/a21f6a2349ec0c084216 to your computer and use it in GitHub Desktop.
Save chipolux/a21f6a2349ec0c084216 to your computer and use it in GitHub Desktop.
Simple Kinetic Scroll Example Using PyQt (Only Works On Qt5)
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_())
@chipolux
Copy link
Author

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 grabbing QScroller.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment