Hey all,

I'm having difficulties incorporating a simple state machine pushbutton in a widget.

The following code works fine:

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4.  
  5. QPushButton *pushButton1 = new QPushButton();
  6. QtStateMachine machine;
  7.  
  8. QtState *s1 = new QtState(machine.rootState());
  9. s1->assignProperty(pushButton1, "text", "In s1");
  10.  
  11. QtState *s2 = new QtState(machine.rootState());
  12. s2->assignProperty(pushButton1, "text", "In s2");
  13.  
  14. QtState *s3 = new QtState(machine.rootState());
  15. s3->assignProperty(pushButton1, "text", "In s3");
  16.  
  17. s1->addTransition(pushButton1, SIGNAL(clicked()), s2);
  18. s2->addTransition(pushButton1, SIGNAL(clicked()), s3);
  19. s3->addTransition(pushButton1, SIGNAL(clicked()), s1);
  20.  
  21. pushButton1->resize(200, 200);
  22. pushButton1->show();
  23.  
  24. machine.setInitialState(s1);
  25. machine.start();
  26.  
  27. return a.exec();
  28. }
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:
Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4.  
  5.  
  6. Widget w;
  7. w.show();
  8.  
  9. return a.exec();
  10. }
To copy to clipboard, switch view to plain text mode 

Definition within the widget:

Qt Code:
  1. Widget::Widget(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4. QVBoxLayout *mainLayout = new QVBoxLayout;
  5. QPushButton *pushButton1 = new QPushButton();
  6.  
  7. QtStateMachine machine;
  8.  
  9. QtState *s1 = new QtState(machine.rootState());
  10. s1->assignProperty(pushButton1, "text", "In s1");
  11.  
  12. QtState *s2 = new QtState(machine.rootState());
  13. s2->assignProperty(pushButton1, "text", "In s2");
  14.  
  15. QtState *s3 = new QtState(machine.rootState());
  16. s3->assignProperty(pushButton1, "text", "In s3");
  17.  
  18. s1->addTransition(pushButton1, SIGNAL(clicked()), s2);
  19. s2->addTransition(pushButton1, SIGNAL(clicked()), s3);
  20. s3->addTransition(pushButton1, SIGNAL(clicked()), s1);
  21.  
  22. machine.setInitialState(s1);
  23. machine.start();
  24.  
  25. mainLayout->addWidget(pushButton1);
  26. setLayout(mainLayout);
  27. }
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