Hey all,
I'm having difficulties incorporating a simple state machine pushbutton in a widget.
The following code works fine:
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();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton *pushButton1 = new QPushButton();
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();
}
To copy to clipboard, switch view to plain text mode
But if I declare a widget in a separate file (widget.cpp and widget.h) and call it from main:
int main(int argc, char *argv[])
{
Widget w;
w.show();
return a.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Definition within the widget:
{
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);
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout;
QPushButton *pushButton1 = new QPushButton();
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);
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks