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



  • 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



  • @
    #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



  • 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



  • 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



  • 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.


Log in to reply
 

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