Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to get this Qt state machine to work? (two way button)
Forum Updated to NodeBB v4.3 + New Features

How to get this Qt state machine to work? (two way button)

Scheduled Pinned Locked Moved Mobile and Embedded
5 Posts 2 Posters 1.9k Views 1 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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    I would want the ui->pushbutton to toggle between 2 states continuously.
    The below code compiles success but it is not doing anything.

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtGui>
    #include <QState>
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QStateMachine machine;
    
    QState *off=new QState();
    off->assignProperty((ui->pushButton),"text","Off");
    off->setObjectName("off");
    
    QState *on=new QState();
    on->setObjectName("on");
    on->assignProperty((ui->pushButton),"text","On");
    
    
    off->addTransition((ui->pushButton),SIGNAL(clicked()),on);
    on->addTransition((ui->pushButton),SIGNAL(clicked()),off);
    
    machine.addState(off);
    machine.addState(on);
    
    machine.setInitialState(off);
    machine.start();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    Edit (Gerolf): please use code markup langauge

    1 Reply Last reply
    0
    • H Offline
      H Offline
      houmingc
      wrote on last edited by
      #2

      @
      #include "mainwindow.h"

      #include "ui_mainwindow.h"

      #include <QtGui>

      #include <QState>

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {

      ui->setupUi(this);
      QStateMachine machine;

      QState *off=new QState();
      off->assignProperty((ui->pushButton),"text","Off");
      off->setObjectName("off");

      QState *on=new QState();
      on->setObjectName("on");
      on->assignProperty((ui->pushButton),"text","On");

      off->addTransition((ui->pushButton),SIGNAL(clicked()),on);
      on->addTransition((ui->pushButton),SIGNAL(clicked()),off);
      machine.addState(off);
      machine.addState(on);

      machine.setInitialState(off);
      machine.start();
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }
      @

      Edit (Gerolf): please use code markup langauge

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

        can someone write a simple switch case statemachine in Qt language? I am learning Qt state machine.

        I saw it is possible in below MovePlayer Function Member
        http://qt-project.org/doc/qt-4.8/statemachine-rogue-window-cpp.html

        1 Reply Last reply
        0
        • H Offline
          H Offline
          houmingc
          wrote on last edited by
          #4

          hi
          I follow the below workable link, they are very detail and presents beginner a clear understanding of Qt Statemachine

          http://ppkl.net/documentation/qt/statemachine-api.html
          http://doc.qt.digia.com/4.6/statemachine-api.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Hi,

            I am sure the problem is simple:

            You create the statemachine as local variable in the constructor.
            This means, when the constructor exists, the state machine is destroyed.
            make it a member of the class.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0

            • Login

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