PDA

View Full Version : Is there anything like qml's behaviours in Qt?



alizadeh91
7th March 2012, 07:51
I have implemented a dial (Like Dial Component in Qt SDK) in qml and it works fine :) but now i want to implemented it in Qt. Is there any thing(class) in Qt like behaviours in Qml (I know there is QPropertyAnimation class but with this class i need to specify its start and end points and when to start animation)
Thanks Friends :)

alizadeh91
7th March 2012, 18:44
Something Just like behaviors in qml for example behavior in rotation or ....???No one knows??

wysota
7th March 2012, 18:50
QPropertyAnimation is exactly what the Behavior element is. What Behavior does is that when you request a change of property value from X to Y, it starts an animation of the property from X to Y. So there you also specify start and end values and you also tell the animation to trigger. Only that you do that implicitly and not explicitly. With Qt/C++ you need to do that explicitly when setting the value.

alizadeh91
7th March 2012, 20:11
Thanks Wysota..
By behavior qml you don't need to specify its end and start, for example in dial example of Qt SDK, there is a behavior on rotation of needle. It doesn't specify start and end point because there is not specific start and end values. I want to know if there is anything in Qt like behavior in qml or if may be QPropertyAnimation can do that without specifying start and end points???
Thanks again :)

wysota
7th March 2012, 22:24
By behavior qml you don't need to specify its end and start
Of course you do but it is done implicitly. The start value is the current value and the end value is the value being assigned to the property.

So if you have a setter in your class then you just need to create an animation for a smooth transition of the property value, like so:


int MyClass::value() const { return m_value; }

void MyClass::setValue(int x) {
if(value() == x) return;
QVariantAnimation *anim = new QVariantAnimation(this);
connect(anim, SIGNAL(valueChanged(QVariant)), this, SLOT(valuesetter_internal(QVariant)));
anim->setStartValue(value());
anim->setEndValue(x);
anim->start(QAbstractAnimation::DeleteWhenStopped);
}

// private slots:
void MyClass::valuesetter_internal(QVariant v) {
m_value = v.toInt();
emit valueChanged(m_value);
}

Then you can just call:


MyClass *obj = ...;
obj->setValue(42);
And it will animate to the value requested. You can make it more foul proof by keeping a pointer to the animation object and adjusting the animation if someone calls setValue() before the previous animation reaches the desired value.