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

最佳答案

QState是-a QObject。您可以自由地重新实现其event()方法:)了解发生了什么:

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

关于c++ - QStateMachine get事件导致状态转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39490731/

10-09 03:03