Well, I am developing a Qt application and I want use the Qt State Framework to make some animations.
First: How I can animate a group of buttons contained in a horizontal layout, into another vertical layout, by using state a transition?
Second: How I can show a widget when in a state? For example a menu: When the user clicks a button in the menu, the widget (that previously has been hidden with widget->hide()) is shown...???
This is a code example:
boxInsert = new BoxInsert(this);
boxInsert->hide ();
btn1 = new QPushButton("Introducir", this);
btn2 = new QPushButton("Informe", this);
btn3 = new QPushButton("Salir", this);
QStateMachine *machine = new QStateMachine(this);
QState *st1 = new QState(machine);
st1->setObjectName ("menuMode");
st1->assignProperty (btn1, "pos", QPointF(center - btn1->width () / 2, 20));
st1->assignProperty (btn2, "pos", QPointF(center - btn1->width () / 2, 40));
st1->assignProperty (btn3, "pos", QPointF(center - btn1->width () / 2, 60));
st1->assignProperty (boxInsert, "visible", QVariant(false));
QState *st2 = new QState(machine);
st2->setObjectName ("barMode");
st2->assignProperty (btn1, "pos", QPointF(40, 0));
st2->assignProperty (btn2, "pos", QPointF(40, 0));
st2->assignProperty (btn3, "pos", QPointF(40, 0));
st1->assignProperty (boxInsert, "visible", QVariant(true));
machine->setInitialState (st1);
QAbstractTransition *t1 = st1->addTransition (btn1, SIGNAL(clicked()), st2);
//QSequentialAnimationGroup *sq1 = new QSequentialAnimationGroup;
//sq1->addPause (250);
t1->addAnimation (new QPropertyAnimation(btn1, "pos"));
t1->addAnimation (new QPropertyAnimation(btn2, "pos"));
t1->addAnimation (new QPropertyAnimation(btn3, "pos"));
t1->addAnimation (new QPropertyAnimation(boxInsert, "visible"));
machine->start ();
It is something like when you will start machine it will change color of a button that may indicate this is clicked then perform something that is linked with your signal so its linked slot will be executed.
s0->addTransition(s1);
s1->assignProperty(ui->pushButton,"styleSheet","background-color:rgb(255,0,0);");
s1->addTransition(s2);
s2->addTransition(ui->pushButton,SIGNAL(clicked()),s0);
QStateMachine m;
m.addState(s0);
m.addState(s1);
m.addState(s2);
m.setInitialState(s0);
To make visible a widget add an state transition like this:
s1->assignProperty(MyWid,"visible", true);
and add transition in s0 for state s1.
First: Move widgets from horizontal layout to vertical using stateMachine? Don't know how to do it, really.
Second: You can implement widget with your own transparency property, that works with QGraphicsEffect:
class myWidget
{
//your methods
Q_PROPERTY(double alpha READ alpha WRITE setAlpha)
double mAlpha;
double alpha() {return mAlpha;}
void setAlpha(double a);
QGraphicsOpacityEffect* eff;
}
make QGraphicsEffect working in setAlpha() method:
void myWidget::setAlpha(double a)
{
mAlpha = a;
if(mAlpha < 0.0)
mAlpha = 0.0;
if(mAlpha > 1.0)
mAlpha = 1.0;
if(mAlpha == 0)
{
this->hide();
}
else
{
this->show();
eff->setOpacity(mAlpha);
}
this->update();
}
And, of course, set QGraphicsOpacityEffect to your widget in constructor:
eff = new QGraphicsOpacityEffect(this);
eff->setOpacity(mAlpha);
this->setGraphicsEffect(eff);
Then you can work with your alpha property in QState:
QState* s1 = new QState(mainState);
s1->assignProperty(mywidget, "alpha", 1.0);
//and so on...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With