QStateMachine获取导致状态转换的事件

时间:2016-09-14 12:42:09

标签: c++ qt qstatemachine

我创建了一个QStateMachine,我必须得到导致状态转换的事件。没有任何机会进入我的位置EnterStateInit()导致此呼叫的信号。这是我的示例代码:

CreateStateMachine()
{
    QState *Init = new QState();
    QState *CheckPrecondition = new QState();
    QState *DoWork = new QState();

    Init->addTransition(this, SIGNAL(EventStart()), CheckPrecondition);
    CheckPrecondition->addTransition(this, SIGNAL(EventSuccesfulCondition()), DoWork);
    CheckPrecondition->addTransition(this, SIGNAL(EventNotSuccesfulCondition()), Init);
    DoWork->addTransition(this, SIGNAL(EventWorkDone()), Init);
    DoWork->addTransition(this, SIGNAL(EventError()), Init);

    connect(Init, SIGNAL(entered()), this, SLOT(EnterStateInit()));
    connect(CheckPrecondition, SIGNAL(entered()), this, SLOT(CheckPrecondition()));
    connect(DoWork, SIGNAL(entered()), this, SLOT(DoWork()));

    connect(Init, SIGNAL(exited()), this, SLOT(LeaveStateInit()));
    connect(CheckPrecondition, SIGNAL(exited()), this, SLOT(LeaveStateCheckPrecondition()));
    connect(DoWork, SIGNAL(exited()), this, SLOT(LeaveDoWork()));

    mModuleStateMachine.addState(Init);
    mModuleStateMachine.addState(CheckPrecondition);
    mModuleStateMachine.addState(DoWork);

    mModuleStateMachine.start();
}

EnterStateInit()
{
  /* Get Event which caused this SLOT to react */
  SetStatus();
}

1 个答案:

答案 0 :(得分:1)

QState是一个QObject。您可以自由重新实现其event()方法:)以了解正在发生的事情:

void MyState::event(QEvent * event) {
  qDebug() << event;
  QState::event(event);
}