为什么在构造函数中带有此指针的 QState 子类中嵌套的 QStateMachine 会导致外部状态机无法进行转换?

Why a nested QStateMachine inside subclass of QState with this pointer in constructor causes that an outer state machine can't make a transition?

有一个状态机(称为外部)。这台机器有两个状态——第一个和最后一个。第一个状态是自定义实现的。在第一个状态中创建了另一个状态机(称为内部状态机),在此示例中它什么都不做。

具有两个状态的外部状态机:

  #include <QDebug>
  #include <QCoreApplication>
  #include <QTimer>
  #include <custom_outer_state.hpp>
  #include <QFinalState>
  #include "a.hpp"

  class Functor
  {
  public:
     void operator()()
     {
        // just emits a signal
        TestObject outerTestObject;

        // create outer state machine with all states
        QStateMachine outerStateMachine;
        CustomOuterState *state1 = new CustomOuterState();
        QFinalState *state2 = new QFinalState();
        state1->addTransition(&outerTestObject, SIGNAL(testObjectSignal()), state2);
        outerStateMachine.addState(state1);
        outerStateMachine.addState(state2);
        outerStateMachine.setInitialState(state1);
        outerStateMachine.start();

        // process state machine transitions
        QCoreApplication::processEvents();
        qDebug() << &outerStateMachine << ": Outer state machine first state " <<  outerStateMachine.configuration();
        outerTestObject.testObjectSignal();
        QCoreApplication::processEvents();
        qDebug() << &outerStateMachine << ": Outer state machine second state " <<  outerStateMachine.configuration();
     }
  };

  int main(int argc, char *argv[])
  {
    QCoreApplication app(argc, argv);
    QTimer::singleShot(0, Functor());
    return QCoreApplication::exec();
  }

和自定义状态:

  #ifndef CUSTOM_OUTER_STATE_H
  #define CUSTOM_OUTER_STATE_H

  #include <QState>
  #include <QStateMachine>

  class CustomOuterState : public QState
  {
     Q_OBJECT
  public:
     virtual void onEntry(QEvent * event)
     {
        // create inner state machine
        machine = new QStateMachine();
        /*
         * some operations with the machine
         */
     }
  private:
     QStateMachine* machine;
  };

  #endif

以及刚刚发出信号的测试对象:

  #ifndef A_H
  #define A_H

  #include <QObject>

  class TestObject : public QObject
  {
     Q_OBJECT
  signals:
     void testObjectSignal();
  };

  #endif

因此此代码按预期工作 - 外部状态机从第一个状态变为最终状态:

  QStateMachine(0x7fffc00f0a20) : Outer state machine first state  QSet(CustomOuterState(0xe0a380) ) 
  QStateMachine(0x7fffc00f0a20) : Outer state machine second state  QSet(QFinalState(0xe0a460) )

但在自定义状态内部稍作更改 - 将 this(它是 QState 的子类)传递给内部状态机构造函数

  -        machine = new QStateMachine();
  +        machine = new QStateMachine(this);

导致 outer 状态机不想进行转换 - 虽然发送了转换信号,但它仍处于第一个状态

  QStateMachine(0x7fff219edcb0) : Outer state machine first state  QSet(CustomOuterState(0x1fc4380) ) 
  QStateMachine(0x7fff219edcb0) : Outer state machine second state  QSet(CustomOuterState(0x1fc4380) )

解决方案很简单 - 只需删除内部状态机,一切正常。但问题是为什么坏事会发生。

那么,为什么要将 this 作为 QState 的子类添加到 inner 状态机构造函数结果,以便 outer 状态机不想进行转换?

您正在尝试从状态的事件处理程序修改状态机的状态图。状态机不支持。由于 QStateMachine is-a QState,通过将其添加为子项,您正在修改状态图。

调用onEntry时,状态图如下所示:

onEntry 期间,您将其更改为类似于以下内容的内容。 machine 不是初始状态,它只是一个悬空的、无用的状态。即使它不是悬挂的,它仍然无法使用,因为它是在状态转换期间添加的。

因为 QStateMachine 是一个 QState,当你让它成为一个状态的直接子状态时,它就成为那个状态的子状态。如果您只想将状态用作状态机的容器,则可以在父级和状态之间插入一个干预 non-state 对象。

// https://github.com/KubaO/Whosebugn/tree/master/questions/statemachine-nested-32619103
#include <QtCore>

struct OuterState : QState
{
   QStateMachine * machine { nullptr };
   virtual void onEntry(QEvent *) Q_DECL_OVERRIDE
   {
      // through an intervening container
      auto container = new QObject(this);
      machine = new QStateMachine(container);
   }
   OuterState(QState * parent = 0) : QState(parent) {}
};

最后,调用 processEvents 的 pseudo-synchronous 代码是不必要的。您可以在进入状态时执行操作等。回想一下,由于信号是一种可调用方法,就像槽一样,您可以将信号连接到信号。事实上,既然你想实现的只是一个无条件的过渡,你还不如使用一个。

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);

   // create outer state machine with all states
   QStateMachine outerStateMachine;
   OuterState state1 { &outerStateMachine };
   QFinalState state2 { &outerStateMachine };
   state1.addTransition(&state2);
   outerStateMachine.setInitialState(&state1);
   outerStateMachine.start();

   a.connect(&state1, &QState::entered, []{ qDebug() << "state1 entered"; });
   a.connect(&state2, &QState::entered, []{ qDebug() << "state2 entered"; });
   a.connect(&state2, &QState::entered, qApp, &QCoreApplication::quit);
   return a.exec();
}

以上或多或少是 self-contained 测试用例的外观:单个文件,没有会影响当前问题的冗长内容。