PDA

View Full Version : Animation not working on transition between states



vfernandez
2nd January 2010, 10:49
I'm taking a look at the new animation framework as well as QStateMachine and I'm trying to make a QGraphicsView zoom in when I select an item and zoom out when no item is selected. I want to animate the zoom effect.

The animation is working fine when zooming in but it's not working when zooming out. It just zooms out suddenly. It's strange because the setZoom() slot I've added to my QGraphicsView subclass gets called but the zoom factor just jumps from 2 to 1 without intermediate values.


QStateMachine machine;
QState *globalOverview = new QState(&machine);
globalOverview->assignProperty(&view, "zoom", qreal(1.0));
machine.setInitialState(globalOverview);

QPropertyAnimation *animation = new QPropertyAnimation(&view, "zoom");
animation->setDuration(250);
animation->setEasingCurve(QEasingCurve::InOutQuad);
machine.addDefaultAnimation(animation);

for(int i = 0; i < 100; i++) {
ExtensionItem *item = new ExtensionItem(i);
layout.addItem(item);
scene.addItem(item);

QState *state = new QState(&machine);
state->assignProperty(&view, "zoom", qreal(2.0));

globalOverview->addTransition(item, SIGNAL(selected()), state);
state->addTransition(item, SIGNAL(selected()), globalOverview);
state->addTransition(&scene, SIGNAL(selectionCleared()), globalOverview);
}

machine.start();

Any idea why it isn't working?

numbat
2nd January 2010, 11:51
As far as I know, QPropertyAnimation uses the read value of the property as the start value (this is reasonable since the old state may not have an assigned value for the property). Since your code never updates m_zoom and always return 1, the animation is from 1 to 1.


void ExtensionView::setZoom(qreal zoom)
{
QTransform oldT = transform();
QTransform newT(zoom, oldT.m12(), oldT.m13(),
oldT.m21(), zoom, oldT.m23(),
oldT.m31(), oldT.m32(), oldT.m33());
setTransform(newT);

m_zoom = zoom; // Need to put this in!!!

qDebug() << __FUNCTION__ << zoom;
}

vfernandez
2nd January 2010, 16:01
D'oh! What a newbie mistake. Thanks!