How to trigger a timeline created with Design Studio from C++?
Unsolved
QML and Qt Quick
-
I have created simple timeline in Design Studio, which scales an image up and down. I now want to trigger the start from C++.
Here is my QML code
Timeline { id: timeline animations: [ TimelineAnimation { id: timelineAnimation duration: 1000 loops: 0 running: true to: 1000 from: 0 } ] endFrame: 1000 startFrame: 0 enabled: true KeyframeGroup { target: image property: "scale" Keyframe { value: 1.25 frame: 100 } Keyframe { value: 1 frame: 1000 } }
I have found this documenation
https://doc.qt.io/qt-5/qtimeline.html
and from what I understand I first have to expose the scale property, which is then triggered by the timeline which is created in C++.. I have written this function.
#include <QObject> #include <QTimeLine> #include <qqml.h> class Scaler: public QObject { Q_OBJECT Q_PROPERTY(QVariant scale WRITE setScale NOTIFY scaleSet) QML_ELEMENT QVariant m_scale; public: explicit Scaler(QObject *parent = nullptr); void setScale(QVariant scale); signals: void scaleChanged(QVariant scale); };
#include "Scaler.h" Scaler::Scaler(QObject *parent) : QObject(parent) { } void Scaler::setScale(QVariant scale) { if (m_scale == scale) return; m_scale = scale; emit scaleChanged(m_scale); }
Now I want to use the timeline from the example
Scaler scaler; QTimeLine *timeLine = new QTimeLine(1000, engine.rootContext()); timeLine->setFrameRange(0, 100); connect(timeLine, &QTimeLine::frameChanged, scaler, &Scaler::setScale(1.25))
but I am not sure how to use the connect function. I want the cale to go from 1 to 1.25 betwenn 0 and 100 ms and back to 1 from 100 to 1000.