PDA

View Full Version : QStateMachine state change problem



QTie
6th June 2011, 09:09
Hi All, I have a problem to get a QStataMachine changing state. QStateMachine has two states running and stopped. Initial state is set to running. Statemachine is started by clicking Start button and Run() method is called. There is a while loop in Run() method which stays in loop until button is pressed and statemachine is supposted to move to Stopped state. But this never happens. Thanks for a help! Here is my code:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

CreateStates();
CreateTransitions();
CreateConnections();
stateM.setInitialState(running);
}

void MainWindow::on_pushButtonStartTest_clicked()
{
stateM.start();
}

void MainWindow::CreateStates()
{
running = new QState(&stateM);
stopped = new QState(&stateM);
}

void MainWindow::CreateConnections()
{
connect(running, SIGNAL(entered()), this, SLOT(Run()));
connect(stopped, SIGNAL(entered()), this, SLOT(Test()));
}

void MainWindow::CreateTransitions()
{
running->addTransition(ui->pushButtonStopTest, SIGNAL(clicked()), stopped);
stateM.addState(running);
stateM.addState(stopped);
}

void MainWindow::Run()
{
while(stateM.configuration().contains(running))
{
//Do something. Loop here until Stop is pressed and statemachine goes to Stopped state
QCoreApplication::processEvents(QEventLoop::AllEve nts);
}

void MainWindow::Test()
{
//This is called when moved to Stopped state.
QLabel *label = new QLabel("Stopped state");
label->show();
}