Where to save the state of QWidget animations for use in QStyle draw functions?

524 views Asked by At

A QWidget as a paintEvent function which is responsible of his drawing. To implement correctly this function, a QStyle object is used to represent each component and a QStyleOption object to save the status of the control.

E.g: A custom ScrollBar implement his paintEvent, which call drawComplexControl with the option "CC_ScrollBar". Then, QProxyStyle could be extended to change the appearance of the scroll-bar.

When the user hover the slider, paintEvent is called which apply the new "hovered" appearance, which state is saved in the QStyleOption::state. But for now a day widgets, this state should not be updated instantly, but with a smooth transition (animation) over some 100-500 milliseconds. In order to animate the widget with this transition some values are needed, like the current state of the animation (a qreal/QColor?) for each part of the scroll: top arrow, bottom arrow or slider.


After this "long" introduction, my question come:

Is there a variable somewhere to set the state of this animation? I could extend QStyleOption with this new value, but the current implementation already seem to include animation, I am unable to found where this transition state is saved.

I am looking for a canonical answer.

Note: To avoid "possible duplicate of...", even if slightly related, this is NOT a question about how to use QAnimation or creating custom Widgets.

1

There are 1 answers

4
Kuba hasn't forgotten Monica On BEST ANSWER

The style animations are derived from the private QStyleAnimation (#include "qstyleanimation_p.h"), and they are QAbstractAnimation and thus QObject. For example, the scrollbar style animation is the QScrollbarStyleAnimation.

Here's how the Windows style's drawControl gets the pointer to the animation:

if (QProgressStyleAnimation *animation = 
  qobject_cast<QProgressStyleAnimation*>(d->animation(opt->styleObject)))

The animations for various style objects are managed by the style PIMPL's animation, startAnimation and stopAnimation methods. The base PIMPL that defines these methods is QCommonStylePrivate (#include <private/qcommonstyle_p.h>).

The way you'd use it in your own style would be to:

  1. Derive your style from QCommonStyle, use the PIMPL idiom, and derive your pimpl from QCommonStylePrivate. I've documented the Qt's PIMPL idiom to make it easier.

  2. Reuse one of the existing style animation classes, or use derive your own from QStyleAnimation.

  3. Leverage the QCommonStyle PIMPL's methods to manage the animations. It's on you to create the animation instance first, though.