c++ - How to animate two values using QTimeLine -
i have qgraphicsitem
want animate size change. because of this, need vary both height
, width
of object on time. have used qtimeline
in past single-variable animations , use here two-variable if possible. however, don't see qtimeline::setframerange()
works 2 variables.
how can accomplish this? there better qt class this?
since animations have attached objects, graphics item qgraphicsobject
, can expose relevant properties qt property system.
since you're animating amounts qsizef
, simplest way expose single property , use qpropertyanimation
.
in general, if have animate multiple properties in parallel, set them individual qpropertyanimation
s. run in parallel using qparallelanimationgroup
.
it's of course possible use qtimeline
, have interpolate between endpoints manually, basing on frame number, example. why bother.
below complete example shows how animate 3 properties @ once: pos
, size
, rotation
.
main.cpp
#include <qgraphicsscene> #include <qgraphicsview> #include <qpropertyanimation> #include <qparallelanimationgroup> #include <qsequentialanimationgroup> #include <qgraphicsobject> #include <qpainter> #include <qapplication> class item : public qgraphicsobject { q_object qreal m_pen; qsizef m_size; q_property(qsizef size read size write setsize notify newsize) q_signal void newsize(); qreal width() const { return m_size.width() + m_pen; } qreal height() const { return m_size.height() + m_pen; } public: explicit item(qgraphicsitem *parent = 0) : qgraphicsobject(parent), m_pen(5.0), m_size(50.0, 50.0) {} qsizef size() const { return m_size; } void setsize(const qsizef & size) { if (m_size != size) { m_size = size; update(); emit newsize(); } } qrectf boundingrect() const { return qrectf(-width()/2, -height()/2, width(), height()); } void paint(qpainter *p, const qstyleoptiongraphicsitem *, qwidget *) { p->setpen(qpen(qt::black, m_pen)); p->drawellipse(qpointf(0,0), width()/2, height()/2); p->setpen(qpen(qt::red, m_pen)); p->drawline(0, 0, 0, height()/2); } }; void animate(qobject * obj) { qparallelanimationgroup * group = new qparallelanimationgroup(obj); qpropertyanimation * pos = new qpropertyanimation(obj, "pos", obj); qpropertyanimation * size = new qpropertyanimation(obj, "size", obj); qpropertyanimation * rot= new qpropertyanimation(obj, "rotation", obj); pos->setduration(3000); pos->setloopcount(-1); pos->seteasingcurve(qeasingcurve::inoutcubic); pos->setstartvalue(qpointf(-50, -50)); pos->setendvalue(qpointf(50, 50)); size->setduration(1500); size->setloopcount(-1); size->seteasingcurve(qeasingcurve::inoutelastic); size->setstartvalue(qsizef(100, 100)); size->setendvalue(qsizef(100, 30)); rot->setduration(1000); rot->setloopcount(-1); rot->setstartvalue(0.0); rot->setendvalue(360.0); group->addanimation(pos); group->addanimation(size); group->addanimation(rot); group->start(); } int main(int argc, char *argv[]) { qapplication a(argc, argv); qgraphicsscene s; qgraphicsview v(&s); item * item = new item; s.additem(item); v.setrenderhint(qpainter::antialiasing); v.setscenerect(-125, -125, 300, 300); v.show(); animate(item); return a.exec(); } #include "main.moc"
Comments
Post a Comment