QT Animation Simple Pushbutton Help
Hey all,
I'm having difficulties incorporating a simple state machine pushbutton in a widget.
The following code works fine:
Code:
int main(int argc, char *argv[])
{
QtStateMachine machine;
QtState *s1 = new QtState(machine.rootState());
s1->assignProperty(pushButton1, "text", "In s1");
QtState *s2 = new QtState(machine.rootState());
s2->assignProperty(pushButton1, "text", "In s2");
QtState *s3 = new QtState(machine.rootState());
s3->assignProperty(pushButton1, "text", "In s3");
s1->addTransition(pushButton1, SIGNAL(clicked()), s2);
s2->addTransition(pushButton1, SIGNAL(clicked()), s3);
s3->addTransition(pushButton1, SIGNAL(clicked()), s1);
pushButton1->resize(200, 200);
pushButton1->show();
machine.setInitialState(s1);
machine.start();
return a.exec();
}
But if I declare a widget in a separate file (widget.cpp and widget.h) and call it from main:
Code:
int main(int argc, char *argv[])
{
Widget w;
w.show();
return a.exec();
}
Definition within the widget:
Code:
{
QtStateMachine machine;
QtState *s1 = new QtState(machine.rootState());
s1->assignProperty(pushButton1, "text", "In s1");
QtState *s2 = new QtState(machine.rootState());
s2->assignProperty(pushButton1, "text", "In s2");
QtState *s3 = new QtState(machine.rootState());
s3->assignProperty(pushButton1, "text", "In s3");
s1->addTransition(pushButton1, SIGNAL(clicked()), s2);
s2->addTransition(pushButton1, SIGNAL(clicked()), s3);
s3->addTransition(pushButton1, SIGNAL(clicked()), s1);
machine.setInitialState(s1);
machine.start();
mainLayout->addWidget(pushButton1);
setLayout(mainLayout);
}
The button appears but no text appears. i.e. the state machine doesn't seem to work.
Any help would be deeply appreciate, Thanks.
Regards,
Pembar
Re: QT Animation Simple Pushbutton Help
Hi, that's a easy c++ issue. *s1 is deleted after the Widget::Widget scope is leaved. Define the states as member variables of the class and it will work.
Code:
class Widget
{
//...
private:
QtState *s1;
}
Widget::Widget()
{
//...
s1 = new QtState(machine.rootState());
}
Re: QT Animation Simple Pushbutton Help
The QtStateMachine are destoyed when Widget COnstructor return;
You must dinamically allocate it.
Code:
QtStateMachine* machine = new QtStateMachine(this);
Re: QT Animation Simple Pushbutton Help
Quote:
Originally Posted by
mcosta
The QtStateMachine are destoyed when Widget COnstructor return;
Ah, even better:o
Re: QT Animation Simple Pushbutton Help
God I love you guys.... Thanks much.
Regards,
Pembar