CopperSpice API  1.9.1
QTimeLine Class Reference

The QTimeLine class provides a timeline for controlling animations. More...

Inheritance diagram for QTimeLine:
QObject

Public Types

enum  CurveShape
 
enum  Direction
 
enum  State
 

Public Signals

void finished ()
 
void frameChanged (int frame)
 
void stateChanged (QTimeLine::State newState)
 
void valueChanged (qreal value)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void resume ()
 
void setCurrentTime (int msec)
 
void setPaused (bool paused)
 
void start ()
 
void stop ()
 
void toggleDirection ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QTimeLine (int duration=1000, QObject *parent=nullptr)
 
virtual ~QTimeLine ()
 
int currentFrame () const
 
int currentTime () const
 
qreal currentValue () const
 
CurveShape curveShape () const
 
Direction direction () const
 
int duration () const
 
QEasingCurve easingCurve () const
 
int endFrame () const
 
int frameForTime (int msec) const
 
int loopCount () const
 
void setCurveShape (CurveShape shape)
 
void setDirection (Direction direction)
 
void setDuration (int duration)
 
void setEasingCurve (const QEasingCurve &curve)
 
void setEndFrame (int frame)
 
void setFrameRange (int startFrame, int endFrame)
 
void setLoopCount (int count)
 
void setStartFrame (int frame)
 
void setUpdateInterval (int interval)
 
int startFrame () const
 
State state () const
 
int updateInterval () const
 
virtual qreal valueForTime (int msec) const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

void timerEvent (QTimerEvent *event) override
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 

Properties

 currentTime
 
 curveShape
 
 direction
 
 duration
 
 easingCurve
 
 loopCount
 
 updateInterval
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Detailed Description

The QTimeLine class provides a timeline for controlling animations. This class is commonly used to animate a GUI control by calling a slot periodically.

You can construct a timeline by passing its duration in milliseconds to the QTimeLine constructor. The timeline duration describes for how long the animation will run. Then you set a suitable frame range by calling setFrameRange(). Finally connect the frameChanged() signal to a suitable slot in the widget you wish to animate (e.g., setValue() in QProgressBar). When you proceed to calling start(), QTimeLine will enter Running state, and start emitting frameChanged() at regular intervals, causing your widget's connected property's value to grow from the lower end to the upper and of your frame range, at a steady rate. You can specify the update interval by calling setUpdateInterval(). When done, QTimeLine enters NotRunning state, and emits finished().

...
progressBar = new QProgressBar(this);
progressBar->setRange(0, 100);
// Construct a 1-second timeline with a frame range of 0 - 100
QTimeLine *timeLine = new QTimeLine(1000, this);
timeLine->setFrameRange(0, 100);
connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int)));
// Clicking the push button will start the progress bar animation
pushButton = new QPushButton(tr("Start animation"), this);
connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start()));
...

You can also use QTimeLine with the Graphics View System for animations. The QGraphicsItemAnimation class implements animation of QGraphicsItems with a timeline.

By default the timeline runs once, from the beginning and towards the end, upon which you must call start() again to restart from the beginning. To make the timeline loop, you can call setLoopCount(), passing the number of times the timeline should run before finishing. The direction can also be changed, causing the timeline to run backward, by calling setDirection(). You can also pause and unpause the timeline while it is running by calling setPaused(). For interactive control, the setCurrentTime() function is provided, which sets the time position of the time line directly. Although most useful in NotRunning state, (e.g., connected to a valueChanged() signal in a QSlider,) this method can be called at any time.

The frame interface is useful for standard widgets, but QTimeLine can be used to control any type of animation. The heart of QTimeLine lies in the valueForTime() function, which generates a value between 0 and 1 for a given time. This value is typically used to describe the steps of an animation, where 0 is the first step of an animation, and 1 is the last step. When running, QTimeLine generates values between 0 and 1 by calling valueForTime() and emitting valueChanged(). By default, valueForTime() applies an interpolation algorithm to generate these value. You can choose from a set of predefined timeline algorithms by calling setCurveShape().

By default QTimeLine uses the EaseInOut curve shape. This provides a value which grows slowly, then grows steadily, and finally grows slowly. For a custom timeline, you can reimplement valueForTime(), in which case QTimeLine's curveShape property is ignored.

See also
QProgressBar, QProgressDialog, QGraphicsItemAnimation

Member Enumeration Documentation

This enum describes the default shape of QTimeLine's value curve. The default, shape is EaseInOutCurve. The curve defines the relation between the value and the timeline.

ConstantValueDescription
QTimeLine::EaseInCurve0The value starts growing slowly, then increases in speed.
QTimeLine::EaseOutCurve1The value starts growing steadily, then ends slowly.
QTimeLine::EaseInOutCurve2The value starts growing slowly, then runs steadily, then grows slowly again.
QTimeLine::LinearCurve3The value grows linearly (e.g., if the duration is 1000 ms, the value at time 500 ms is 0.5).
QTimeLine::SineCurve4The value grows sinusoidally.
QTimeLine::CosineCurve5The value grows cosinusoidally.
See also
setCurveShape()

This enum describes the direction of the timeline when in Running state.

ConstantValueDescription
QTimeLine::Forward0The current time of the timeline increases with time (i.e., moves from 0 and towards the end / duration).
QTimeLine::Backward1The current time of the timeline decreases with time (i.e., moves from the end / duration and towards 0).
See also
setDirection()

This enum describes the state of the timeline.

ConstantValueDescription
QTimeLine::NotRunning0The timeline is not running. This is the initial state of QTimeLine, and the state QTimeLine reenters when finished. The current time, frame and value remain unchanged until either setCurrentTime() is called, or the timeline is started by calling start().
QTimeLine::Paused1The timeline is paused (i.e., temporarily suspended). Calling setPaused(false) will resume timeline activity.
QTimeLine::Running2The timeline is running. While control is in the event loop, QTimeLine will update its current time at regular intervals, emitting valueChanged() and frameChanged() when appropriate.
See also
state(), stateChanged()

Constructor & Destructor Documentation

QTimeLine::QTimeLine ( int  duration = 1000,
QObject parent = nullptr 
)
explicit

Constructs a timeline with a duration of duration milliseconds. The parent is passed to the QObject constructor. The default duration is 1000 milliseconds.

QTimeLine::~QTimeLine ( )
virtual

Destroys the timeline.

Method Documentation

int QTimeLine::currentFrame ( ) const

Returns the frame corresponding to the current time.

See also
currentTime(), frameForTime(), setFrameRange()
int QTimeLine::currentTime ( ) const

Returns the value of the property.

qreal QTimeLine::currentValue ( ) const

Returns the value corresponding to the current time.

See also
valueForTime(), currentFrame()
CurveShape QTimeLine::curveShape ( ) const

Returns the value of the property.

Direction QTimeLine::direction ( ) const

Returns the value of the property.

int QTimeLine::duration ( ) const

Returns the value of the property.

QEasingCurve QTimeLine::easingCurve ( ) const

Returns the value of the property.

int QTimeLine::endFrame ( ) const

Returns the end frame, which is the frame corresponding to the end of the timeline (i.e., the frame for which the current value is 1).

See also
setEndFrame(), setFrameRange()
void QTimeLine::finished ( )
signal

This signal is emitted when QTimeLine finishes (i.e., reaches the end of its time line), and does not loop.

void QTimeLine::frameChanged ( int  frame)
signal

QTimeLine emits this signal at regular intervals when in Running state, but only if the current frame changes. frame is the current frame number.

See also
QTimeLine::setFrameRange(), QTimeLine::updateInterval
int QTimeLine::frameForTime ( int  msec) const

Returns the frame corresponding to the time msec. This value is calculated using a linear interpolation of the start and end frame, based on the value returned by valueForTime().

See also
valueForTime(), setFrameRange()
int QTimeLine::loopCount ( ) const

Returns the value of the property.

void QTimeLine::resume ( )
slot

Resumes the timeline from the current time. QTimeLine will reenter Running state, and once it enters the event loop, it will update its current time, frame and value at regular intervals.

In contrast to start(), this function does not restart the timeline before it resumes.

See also
start(), updateInterval(), frameChanged(), valueChanged()
void QTimeLine::setCurrentTime ( int  msec)
slot

Sets the value of the property to msec.

void QTimeLine::setCurveShape ( CurveShape  shape)

Sets the value of the property to shape.

void QTimeLine::setDirection ( Direction  direction)

Sets the value of the property to direction.

void QTimeLine::setDuration ( int  duration)

Sets the value of the property to duration.

void QTimeLine::setEasingCurve ( const QEasingCurve curve)

Sets the value of the property to curve.

void QTimeLine::setEndFrame ( int  frame)

Sets the end frame, which is the frame corresponding to the end of the timeline (i.e., the frame for which the current value is 1), to frame.

See also
endFrame(), startFrame(), setFrameRange()
void QTimeLine::setFrameRange ( int  startFrame,
int  endFrame 
)

Sets the timeline's frame counter to start at startFrame, and end and endFrame. For each time value, QTimeLine will find the corresponding frame when you call currentFrame() or frameForTime() by interpolating, using the return value of valueForTime().

When in Running state, QTimeLine also emits the frameChanged() signal when the frame changes.

See also
startFrame(), endFrame(), start(), currentFrame()
void QTimeLine::setLoopCount ( int  count)

Sets the value of the property to count.

void QTimeLine::setPaused ( bool  paused)
slot

If paused is true, the timeline is paused, causing QTimeLine to enter Paused state. No updates will be signaled until either start() or setPaused(false) is called. If paused is false, the timeline is resumed and continues where it left.

See also
state(), start()
void QTimeLine::setStartFrame ( int  frame)

Sets the start frame, which is the frame corresponding to the start of the timeline (i.e., the frame for which the current value is 0), to frame.

See also
startFrame(), endFrame(), setFrameRange()
void QTimeLine::setUpdateInterval ( int  interval)

Sets the value of the property to interval.

void QTimeLine::start ( )
slot

Starts the timeline. QTimeLine will enter Running state, and once it enters the event loop, it will update its current time, frame and value at regular intervals. The default interval is 40 ms (i.e., 25 times per second). You can change the update interval by calling setUpdateInterval().

The timeline will start from position 0, or the end if going backward. If you want to resume a stopped timeline without restarting, you can call resume() instead.

See also
resume(), updateInterval(), frameChanged(), valueChanged()
int QTimeLine::startFrame ( ) const

Returns the start frame, which is the frame corresponding to the start of the timeline (i.e., the frame for which the current value is 0).

See also
setStartFrame(), setFrameRange()
State QTimeLine::state ( ) const

Returns the state of the timeline.

See also
start(), setPaused(), stop()
void QTimeLine::stateChanged ( QTimeLine::State  newState)
signal

This signal is emitted whenever QTimeLine's state changes. The new state is newState.

void QTimeLine::stop ( )
slot

Stops the timeline, causing QTimeLine to enter NotRunning state.

See also
start()
void QTimeLine::timerEvent ( QTimerEvent event)
overrideprotectedvirtual

Reimplemented from QObject::timerEvent()

void QTimeLine::toggleDirection ( )
slot

Toggles the direction of the timeline. If the direction was Forward, it becomes Backward, and vice versa.

See also
setDirection()
int QTimeLine::updateInterval ( ) const

Returns the value of the property.

void QTimeLine::valueChanged ( qreal  value)
signal

QTimeLine emits this signal at regular intervals when in Running state, but only if the current value changes. The passed parameter of value is the current value and a number between 0.0 and 1.0.

See also
QTimeLine::setDuration(), QTimeLine::valueForTime(), QTimeLine::updateInterval
qreal QTimeLine::valueForTime ( int  msec) const
virtual

Returns the timeline value for the time msec. The returned value, which varies depending on the curve shape, is always between 0 and 1. If msec is 0, the default implementation always returns 0.

Reimplement this function to provide a custom curve shape for your timeline.

See also
CurveShape, frameForTime()

Property Documentation

QTimeLine::currentTime

This property holds the current time of the time line.

When QTimeLine is in Running state, this value is updated continuously as a function of the duration and direction of the timeline. Otherwise, it is value that was current when stop() was called last, or the value set by setCurrentTime().

By default, this property contains a value of 0.

PropertiesClass Methods
read currentTime
write setCurrentTime
QTimeLine::curveShape

This property holds the shape of the timeline curve.

The curve shape describes the relation between the time and value for the base implementation of valueForTime().

If you have reimplemented valueForTime(), this value is ignored.

By default, this property is set to EaseInOutCurve.

See also
valueForTime()
PropertiesClass Methods
read curveShape
write setCurveShape
QTimeLine::direction

This property holds the direction of the timeline when QTimeLine is in Running state. This direction indicates whether the time moves from 0 towards the timeline duration, or from the value of the duration and towards 0 after start() has been called.

By default, this property is set to Forward.

PropertiesClass Methods
read direction
write setDirection
QTimeLine::duration

This property holds the total duration of the timeline in milliseconds. Changing the duration does not cause the current time to be reset to zero or the new duration. You also need to call setCurrentTime() with the desired value.

By default this value is 1000 which is 1 second. It can be changed by either passing a duration to the QTimeLine constructor or by calling setDuration(). The duration must be larger than 0.

PropertiesClass Methods
read duration
write setDuration
QTimeLine::easingCurve

Specifies the easing curve that the timeline will use. If both easing curve and curveShape are set, the last set property will override the previous one. (If valueForTime() is reimplemented it will override both)

PropertiesClass Methods
read easingCurve
write setEasingCurve
QTimeLine::loopCount

This property holds the number of times the timeline should loop before it is finished. A loop count of 0 means the timeline will loop forever. By default, this property contains a value of 1.

PropertiesClass Methods
read loopCount
write setLoopCount
QTimeLine::updateInterval

This property holds the time in milliseconds between each time QTimeLine updates its current time. When updating the current time, QTimeLine will emit valueChanged() if the current value changed, and frameChanged() if the frame changed. By default, the interval is 40 ms, which corresponds to a rate of 25 updates per second.

PropertiesClass Methods
read updateInterval
write setUpdateInterval