Search code examples
qtpyside2qtreeviewqtreewidget

Can you set the animation speed of a QTreeWidget?


You can set a QTreeWidget to animated with:

tree_widget = QtWidgets.QTreeWidget()
tree_widget.setAnimated(True)

This will make the QTreeWidgetItems animate while they collapse and expand.

Is there a way to access and edit the animation speed, and type, in the same way you would a QtCore.QVariantAnimation()?

I would like to be able to change the speed and animation type (eg, QtCore.QEasingCurve.Linear) if possible.


Solution

  • Let's track the source;

    1. When we look for animated property we can find out it is actually part of QTreeView class.
    2. So first we need to check if they provided a public method (may named set/addAnimation) to access/manipulate this property. But there aren't any. (not totally true, see the update section)
    3. Then we've to look into the source code of QTreeView. The property set at line 910 as animationsEnabled flag.
    4. When we look for where the action taken according to this flag is at line 3096 and line 3113
    5. And unfortunately these methods are part of QTreeViewPrivate class which is not part of Qt API according to the doc-string:

    W A R N I N G

    This file is not part of the Qt API. It exists purely as an implementation detail. This header file may change from version to version without notice, or even be removed.

    We mean it.

    So, I don't see a direct way to access or change it without touching and building source.


    UPDATE

    I recently came across to a widget-animation-duration property in Qt Style Sheet Reference to override built-in animation duration values with style sheets and decided to append it here. However, which widgets are supported is poorly documented. Fortunately, I was able to find related commit with help of google hacking:

    "widget-animation-duration" inurl:"code.qt.io"
    

    Diffstat

    -rw-r--r-- src/widgets/doc/snippets/code/doc_src_stylesheet.qdoc 4
    -rw-r--r-- src/widgets/doc/src/widgets-and-layouts/stylesheet.qdoc 16
    -rw-r--r-- src/widgets/itemviews/qcolumnview.cpp 6
    -rw-r--r-- src/widgets/itemviews/qtreeview.cpp 2
    -rw-r--r-- src/widgets/styles/qcommonstyle.cpp 5
    -rw-r--r-- src/widgets/styles/qstyle.cpp 9
    -rw-r--r-- src/widgets/styles/qstyle.h 1
    -rw-r--r-- src/widgets/styles/qstylesheetstyle.cpp 4
    -rw-r--r-- src/widgets/widgets/qtabbar_p.h 2
    -rw-r--r-- src/widgets/widgets/qwidgetanimator.cpp 4
    10 files changed, 41 insertions, 12 deletions

    QColumnView and QWidgetAnimator classes has these lines and I was able to change duration of QColumnView animations when I tested.

    if (const int animationDuration = style()->styleHint(QStyle::SH_Widget_Animation_Duration, 0, this)) {
        d->currentAnimation.setDuration(animationDuration);
    

    BUT: QTreeView implementation only checks the flags existence but not using it's value yet because they animate it by rendering tree to pixmap and drawing it by pixels. We can assume they'll use it because this looks like a preparation for it:

    animationsEnabled = q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, 0, q) > 0;