在之前的部落格《如何在 pyqt 中實現平滑捲動的 QScrollArea》中,我們使用定時器和佇列實現了平滑捲動。但是實現程式碼還是有一點複雜,所以這篇部落格將使用 Qt 的動畫框架 QPropertyAnimation
來實現相同的功能。
捲動過程其實就是改變 QScrollBar
的 value()
的過程,Qt 自帶的 QScrollArea
之所以無法平滑捲動,就是因為捲動時在 QScrollBar
的兩個 value()
之間進行跳變。如果我們能在兩個捲動值之間進行插值,就能實現平滑捲動了,這裡通過重寫 setValue()
函數來啟動捲動動畫。
class SmoothScrollBar(QScrollBar):
""" Smooth scroll bar """
scrollFinished = pyqtSignal()
def __init__(self, parent=None):
QScrollBar.__init__(self, parent)
self.ani = QPropertyAnimation()
self.ani.setTargetObject(self)
self.ani.setPropertyName(b"value")
self.ani.setEasingCurve(QEasingCurve.OutCubic)
self.ani.setDuration(500)
self.ani.finished.connect(self.scrollFinished)
def setValue(self, value: int):
if value == self.value():
return
# stop running animation
self.ani.stop()
self.scrollFinished.emit()
self.ani.setStartValue(self.value())
self.ani.setEndValue(value)
self.ani.start()
def scrollValue(self, value: int):
""" scroll the specified distance """
value += self.value()
self.scrollTo(value)
def scrollTo(self, value: int):
""" scroll to the specified position """
value = min(self.maximum(), max(self.minimum(), value))
self.setValue(value)
def mousePressEvent(self, e):
self.ani.stop()
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
self.ani.stop()
super().mouseReleaseEvent(e)
def mouseMoveEvent(self, e):
self.ani.stop()
super().mouseMoveEvent(e)
最後需要將 QScrollArea
的預設卷軸替換為平滑捲動的 SmoothScrollBar
:
class SmoothScrollArea(QScrollArea):
""" Smooth scroll area """
def __init__(self, parent=None):
super().__init__(parent)
self.hScrollBar = SmoothScrollBar()
self.vScrollBar = SmoothScrollBar()
self.hScrollBar.setOrientation(Qt.Horizontal)
self.vScrollBar.setOrientation(Qt.Vertical)
self.setVerticalScrollBar(self.vScrollBar)
self.setHorizontalScrollBar(self.hScrollBar)
def setScrollAnimation(self, orient, duration, easing=QEasingCurve.OutCubic):
""" set scroll animation
Parameters
----------
orient: Orient
scroll orientation
duration: int
scroll duration
easing: QEasingCurve
animation type
"""
bar = self.hScrollBar if orient == Qt.Horizontal else self.vScrollBar
bar.ani.setDuration(duration)
bar.ani.setEasingCurve(easing)
def wheelEvent(self, e):
if e.modifiers() == Qt.NoModifier:
self.vScrollBar.scrollValue(-e.angleDelta().y())
else:
self.hScrollBar.scrollValue(-e.angleDelta().x())
下面是一個簡單的圖片檢視器測試程式:
# coding:utf-8
import sys
from PyQt5.QtCore import QEasingCurve, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel
class Demo(SmoothScrollArea):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.label.setPixmap(QPixmap("shoko.jpg"))
# customize scroll animation
self.setScrollAnimation(Qt.Vertical, 400, QEasingCurve.OutQuint)
self.setScrollAnimation(Qt.Horizontal, 400, QEasingCurve.OutQuint)
self.horizontalScrollBar().setValue(1900)
self.setWidget(self.label)
self.resize(1200, 800)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
測試用的圖片如下(硝子醬真可愛