在qt中创建qstate

时间:2016-04-14 06:46:48

标签: c++ qt qt4

如果我有一个像这样的简单状态机

  switch(state)
    {
    case state_1:
      doSomething();
    state = state_2;
    break;
    case state_2:
    doSomething_2();
state = state_finished;
    break;
    }

如何通过转换将这个简单的状态机转换为qstate?

1 个答案:

答案 0 :(得分:1)

这将是这样的事情:

TestClass test_class;
QStateMachine machine;

QState *state_1 = new QState(&machine);
QObject::connect(state_1, SIGNAL(entered()), &test_class, SLOT(doSomething1()));

QState *state_2 = new QState(&machine);
QObject::connect(state_2, SIGNAL(entered()), &test_class, SLOT(doSomething2()));

QFinalState *state_finished = new QFinalState(&machine);
TestClassFinishedTransition *finished_transition = 
                                new TestClassFinishedTransition(&test_class);
finished_transition->setTargetState(state_finished);
state_2->addTransition(finished_transition);

machine.setInitialState(state_1);
machine.start();

TestClass将基于QObject类。

我希望这段代码可以帮助您开始理解这个概念。但是,您必须仔细阅读documentation并阅读Qt提供的示例。