QStateMachine



  • HI,

    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    

    {
    glayout = new QGridLayout(this);
    button = new QPushButton("Button");
    lb_1 = new QLabel("Hiii");

    glayout->addWidget(button,0,1);
    glayout->addWidget(lb_1,1,1);
    
    QStateMachine machine;
       QState *s1 = new QState();
       QState *s2 = new QState();
       QState *s3 = new QState();
    
          s1->addTransition(button, SIGNAL(clicked()), s2);
          s2->addTransition(button, SIGNAL(clicked()), s3);
          s3->addTransition(button, SIGNAL(clicked()), s1);
    
          s1->assignProperty(lb_1, "text", "In state s1");
          s2->assignProperty(lb_1, "text", "In state s2");
          s3->assignProperty(lb_1, "text", "In state s3");
    
          machine.addState(s1);
          machine.addState(s2);
          machine.addState(s3);
          machine.setInitialState(s1);
          machine.start();
    

    }

    In the above i am trying to change the label for each state change.
    But unable to get required output.


  • Qt Champions 2016

    @yuvaram

    Without checking other things, machine goes out of scope after the constructor finishes. You should fix that.



  • Yuvaraj

    QStateMachine machine; is local object. Allocate the object on heap

    QStateMachine *machine = new QStateMachine;
    Rest of the things should work fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.