Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStateMachine
Forum Updated to NodeBB v4.3 + New Features

QStateMachine

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 735 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • yuvaramY Offline
    yuvaramY Offline
    yuvaram
    wrote on last edited by
    #1

    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.

    Yuvaram Aligeti
    Embedded Qt Developer
    : )

    kshegunovK 1 Reply Last reply
    0
    • yuvaramY yuvaram

      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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @yuvaram

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Yuvaraj

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

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        5

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved