PyQt通過動畫實現(xiàn)平滑滾動的QScrollArea
前言
在之前的博客《如何在 pyqt 中實現(xiàn)平滑滾動的 QScrollArea》中,我們使用定時器和隊列實現(xiàn)了平滑滾動。但是實現(xiàn)代碼還是有一點復(fù)雜,所以這篇博客將使用 Qt 的動畫框架 QPropertyAnimation 來實現(xiàn)相同的功能。
實現(xiàn)過程
SmoothScrollBar
滾動過程其實就是改變 QScrollBar 的 value() 的過程,Qt 自帶的 QScrollArea 之所以無法平滑滾動,就是因為滾動時在 QScrollBar 的兩個 value() 之間進(jìn)行跳變。如果我們能在兩個滾動值之間進(jìn)行插值,就能實現(xiàn)平滑滾動了,這里通過重寫 setValue() 函數(shù)來啟動滾動動畫。
""" 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)
SmoothScrollArea
最后需要將 QScrollArea 的默認(rèn)滾動條替換為平滑滾動的 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())
測試
下面是一個簡單的圖片查看器測試程序:
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_()
最后
至此平滑滾動的實現(xiàn)方式就已介紹完畢了,更多自定義小部件可以參見 PyQt-Fluent-Widgets
到此這篇關(guān)于PyQt通過動畫實現(xiàn)平滑滾動的QScrollArea的文章就介紹到這了,更多相關(guān)PyQt QScrollArea內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python圖形化界面基礎(chǔ)篇之如何使用彈出窗口和對話框
對于Python程序員來說,處理彈出窗口似乎并不是一個常見的任務(wù),這篇文章主要給大家介紹了關(guān)于Python圖形化界面基礎(chǔ)篇之如何使用彈出窗口和對話框的相關(guān)資料,需要的朋友可以參考下2024-03-03
Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python函數(shù)實現(xiàn)學(xué)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
python?字符串模糊匹配Fuzzywuzzy的實現(xiàn)
本文主要介紹了python?字符串模糊匹配Fuzzywuzzy的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
widows下安裝pycurl并利用pycurl請求https地址的方法
今天小編就為大家分享一篇widows下安裝pycurl并利用pycurl請求https地址的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
詳細(xì)解讀Python的web.py框架下的application.py模塊
這篇文章主要介紹了Python的web.py框架下的application.py模塊,作者深入分析了web.py的源碼,需要的朋友可以參考下2015-05-05
Django JSONField的自動轉(zhuǎn)換思路詳解(django自定義模型字段)
如果想實現(xiàn)JSONField的自動轉(zhuǎn)換,可以使用Django REST framework的JSONField,或者自定義一個字段類并覆蓋from_db_value()和get_prep_value()方法來實現(xiàn)這個功能,這篇文章主要介紹了Django JSONField的自動轉(zhuǎn)換(django自定義模型字段)問題,需要的朋友可以參考下2023-06-06

