你能设置 QTreeWidget 的动画速度吗?

Can you set the animation speed of a QTreeWidget?

您可以将 QTreeWidget 设置为动画:

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

这将使 QTreeWidgetItems 在折叠和展开时具有动画效果。

有没有一种方法可以像 QtCore.QVariantAnimation() 一样访问和编辑动画速度和类型?

如果可能的话,我希望能够更改速度和动画类型(例如,QtCore.QEasingCurve.Linear)。

追根溯源;

  1. 当我们寻找animated 属性时我们可以find out它实际上是QTreeView的一部分class.
  2. 所以首先我们需要检查他们是否为 access/manipulate 这个 属性 提供了一个 public 方法(可能命名为 set/addAnimation)。但是没有。 (不完全正确,请参阅更新部分)
  3. 那我们就来看看QTreeView的源码吧。 属性 在 line 910 处设置为 animationsEnabled 标志。
  4. 当我们在line 3096 and line 3113
  5. 查找根据这个flag采取的动作在哪里
  6. 不幸的是,根据文档字符串,这些方法是 QTreeViewPrivate class 的一部分,而不是 Qt API 的一部分:

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.

因此,我没有看到一种直接访问或更改它而不接触和构建源代码的方法。


更新

最近在 google 黑客的帮助下,我在 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 遇到了一个 widget-animation-duration 属性:

"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 和 QWidgetAnimator classes 有这些行,我在测试时能够更改 QColumnView 动画的持续时间。

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

但是: QTreeView 实现只检查标志是否存在但不使用它的值 yet 因为它们通过将树渲染为像素图来动画它并且按像素绘制。我们可以假设他们会使用它,因为这看起来像是为此做的准备:

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