PyQt how to make widgets to snap in a scrollarea while kinetic scrolling

Viewed 24

Here is a example of my application :

enter image description here

where you can swipe through some values using a QScroller. What I intend to do is creating snap points on the values so as to make it snap those to indicate selected values.

here is my code :

class mainWindow(QMainWindow):
def __init__(self, parent=None):
    super().__init__(parent)
    uic.loadUi("form.ui", self)

    self.scroll_area = QScrollArea(self)
    self.scroll_area.setStyleSheet(f"font-size : 90px; font-weight : bold")
    self.scroll_area.setAlignment(Qt.AlignCenter)
    self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)


    self.layoutt = QHBoxLayout(self)
    self.layoutt.addWidget(self.scroll_area)

    self.scroll_widget = QWidget(self)
    self.scroll_layout = QHBoxLayout(self.scroll_widget)
    self.scroll_area.setGeometry(QRect(100,300,900,160))

    for i in range(1,50):
        self.scroll_layout.addWidget(QLabel("0.{}   ".format(i)))
    
    self.scroll_area.setWidget(self.scroll_widget)

    global scroller
    scroller = QScroller.scroller(self.scroll_area)

    scroller.scrollerPropertiesChanged.connect(self.propsChanged)
   

    scroller.grabGesture(
        self.scroll_area.viewport(), QScroller.LeftMouseButtonGesture
    )
    QScroller.setSnapPositionsX(scroller, 0.0, 500.0)
    QScroller.setSnapPositionsY(scroller, 0.0, 500.0)

Code works but when I swipe through the values, it just continues to kinetic scrolling and does not snap to any point. I examined the QScroller doc and found setSnapPosition function. I used it in the code but it does not seem to change anything. What am I doing wrong?

0 Answers
Related