Results 1 to 5 of 5

Thread: QT Animation Simple Pushbutton Help

  1. #1
    Join Date
    May 2009
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Red face 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:

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.
    Qt Code:
    1. class Widget
    2. {
    3. //...
    4. private:
    5. QtState *s1;
    6. }
    7. Widget::Widget()
    8. {
    9. //...
    10. s1 = new QtState(machine.rootState());
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT Animation Simple Pushbutton Help

    The QtStateMachine are destoyed when Widget COnstructor return;

    You must dinamically allocate it.
    Qt Code:
    1. QtStateMachine* machine = new QtStateMachine(this);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  4. The following user says thank you to mcosta for this useful post:

    Pembar (6th May 2009)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QT Animation Simple Pushbutton Help

    Quote Originally Posted by mcosta View Post
    The QtStateMachine are destoyed when Widget COnstructor return;
    Ah, even better

  6. #5
    Join Date
    May 2009
    Posts
    30
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT Animation Simple Pushbutton Help

    God I love you guys.... Thanks much.

    Regards,
    Pembar

Similar Threads

  1. A simple example of a tree model
    By YaK in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2009, 17:15
  2. Problem with setFont for a PushButton
    By arunvv in forum Newbie
    Replies: 12
    Last Post: 7th April 2008, 23:35
  3. Animation while connecting on a tcpsocket
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 26th December 2007, 10:34
  4. connecting image to a pushbutton
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 4th December 2007, 09:23
  5. Replies: 1
    Last Post: 2nd July 2007, 16:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.