0

我创建了一个 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();
}
4

1 回答 1

1

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

void MyState::event(QEvent * event) {
  qDebug() << event;
  QState::event(event);
}
于 2016-09-14T13:02:06.487 回答