PyQt通过动画实现平滑滚动的QScrollArea

 更新时间:2023年01月28日 10:12:22   作者:之一Yo  
这篇文章主要为大家详细介绍了PyQt如何使用Qt的动画框架 QPropertyAnimation来实现平滑滚动的QScrollArea,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的可以学习一下

前言

在之前的博客《如何在 pyqt 中实现平滑滚动的 QScrollArea》中,我们使用定时器和队列实现了平滑滚动。但是实现代码还是有一点复杂,所以这篇博客将使用 Qt 的动画框架 QPropertyAnimation 来实现相同的功能。

实现过程

SmoothScrollBar

滚动过程其实就是改变 QScrollBar 的 value() 的过程,Qt 自带的 QScrollArea 之所以无法平滑滚动,就是因为滚动时在 QScrollBar 的两个 value() 之间进行跳变。如果我们能在两个滚动值之间进行插值,就能实现平滑滚动了,这里通过重写 setValue() 函数来启动滚动动画。

   """ 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 的默认滚动条替换为平滑滚动的 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_()

最后

至此平滑滚动的实现方式就已介绍完毕了,更多自定义小部件可以参见 PyQt-Fluent-Widgets

到此这篇关于PyQt通过动画实现平滑滚动的QScrollArea的文章就介绍到这了,更多相关PyQt QScrollArea内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python和scikit-learn创建混淆矩阵的示例详解

    使用Python和scikit-learn创建混淆矩阵的示例详解

    这篇文章主要介绍了使用Python和scikit-learn创建混淆矩阵的示例详解,该示例包括生成数据集、为数据集选择合适的机器学习模型、构建、配置和训练它,最后解释结果,即混淆矩阵,需要的朋友可以参考下
    2022-06-06
  • python3使用matplotlib绘制散点图

    python3使用matplotlib绘制散点图

    这篇文章主要为大家详细介绍了python3使用matplotlib绘制散点图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • python进阶之魔术方法详解

    python进阶之魔术方法详解

    这篇文章主要为大家介绍了python进阶之魔术方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • python实现全排列代码(回溯、深度优先搜索)

    python实现全排列代码(回溯、深度优先搜索)

    今天小编就为大家分享一篇python实现全排列代码(回溯、深度优先搜索),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 详解python变量的命名和使用

    详解python变量的命名和使用

    变量名只能包含字母、数字和下划线,本文主要介绍了详解python变量的命名和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12
  • Python中flatten( ),matrix.A用法说明

    Python中flatten( ),matrix.A用法说明

    这篇文章主要介绍了Python中flatten( ),matrix.A用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 单利模式及python实现方式详解

    单利模式及python实现方式详解

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保 某一个类只有一个实例存在.这篇文章主要介绍了单利模式及python实现方式及Python单例模式的4种实现方法,需要的朋友可以参考下
    2018-03-03
  • python新手学习使用库

    python新手学习使用库

    在本篇文章里小编给大家整理的一篇关于python新手学习使用库的相关方法和知识点,需要的朋友们参考下。
    2020-06-06
  • 树莓派用python中的OpenCV输出USB摄像头画面

    树莓派用python中的OpenCV输出USB摄像头画面

    这篇文章主要为大家详细介绍了树莓派用python中的OpenCV输出USB摄像头画面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Python使用Tkinter实现转盘抽奖器的步骤详解

    Python使用Tkinter实现转盘抽奖器的步骤详解

    这篇文章主要介绍了Python使用Tkinter实现转盘抽奖器,,本文分场景通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01

最新评论