如果 QStateMachine 中的转换成功,则从 class 发出信号

Emitting signals from class, if transition in QStateMachine was successful

我的问题如下:我需要创建 class,其中包含 QStateMachine 实例。这个 class 应该有插槽,您可以通过这些插槽 "ask" 状态机转换到另一个状态。如果转换成功,我的 class 应该发出有关它的信号。我将如何实施? Class 应该能够根据特定的插槽调用发出特定的信号。 这是 class 的一个小例子:

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject *parent = 0)
    {
        mStateMachine = new QStateMachine(this);
        QState *s1 = new QState(mStateMachine);
        QState *s2 = new QState(mStateMachine);
        QState *s3 = new QState(mStateMachine);

        s1->addTransition(); // Transition to s2
        s2->addTransition(); // Transition to s3
        s3->addTransition(); // Transition to s1

        mStateMachine->setInitialState(s1);
        mStateMachine->start();
    }

signals:
    toS1();
    toS2();
    toS3();

public slots:
    slotToS1()
    {
        /* post event to state machine about
        transition to state s1,
        if transition was successful,
        then emit toS1() signal. */
    };
    slotToS2(){ /* Similar to slotToS1 */};
    slotToS3(){ /* Similar to slotToS1 */};
private:
    QStateMachine *mStateMachine;
}

非常感谢您的帮助!

更新:
插槽代表不同种类的转换,因此外部 class(将使用 MyClass)可以 'ask' 进行某些转换。因此,插槽向状态机发送事件或信号,它查看事件或信号并(如果处于正确状态)进行此转换。我想用某些信号通知外部 class,在插槽(转换)成功之前询问。

我过去遇到过同样的问题,我发现最简单的方法是用你自己的 QState class 从 QState 继承并实现 2 个方法 QState::onEntry(QEvent * event)QState::onExit(QEvent * event).

这样您就可以在退出和进入新状态时发出您喜欢的任何信号。

这是一个例子:

文件mystate.h

#include <QState>

class MyState : public QState
{
    Q_OBJECT
public:
    explicit MyState(qint32 stateId, QState * parent = 0);

protected:
    void onEntry(QEvent * event);
    void onExit(QEvent * event);
 
signals:
    void exit(qint32 stateId);
    void enter(qint32 stateId);
  
private: 
    qint32 stateId;  
};

和文件mystate.cpp

#include "mystate.h"


MyState::MyState(qint32 stateId, QState *parent)
{
    this->stateId = stateId;
}

void MyState::onEntry(QEvent *event)
{
    emit enter(stateId);
}

void MyState::onExit(QEvent *event)
{
    emit (exit(stateId));
}
  1. 要在插槽调用上转换,您需要以某种方式将插槽绑定到 QAbstractTransition。有两种方法:

    • 使用QEventTransition并发送相关事件来触发它。

    • 使用QSignalTransition并使用内部信号触发它。

  2. 要在状态转换时发出信号,您可以将QAbstractTransition::triggeredQState::enteredQState::exited信号连接到其他信号.请记住,在 Qt 中,连接目标可以是插槽或信号。

因此,使用信号转换:

class MyClass : public QObject
{
  Q_OBJECT
  QStateMachine machine;
  QState s1, s2;
  Q_SIGNAL void s_go_s1_s2();
  Q_SIGNAL void s_go_s2_s1();
public:
  Q_SIGNAL void transitioned_s1_s2();
  Q_SIGNAL void transitioned_s2_s1();
  Q_SLOT void go_s2_s1() { emit s_go_s2_s1(); }
  Q_SLOT void go_s1_s2() { emit s_go_s1_s2(); }
  explicit MyClass(QObject *parent = 0) : QObject(parent),
    s1(&machine), s2(&machine) {
    auto s1_s2 = s1.addTransition(this, SIGNAL(s_go_s1_s2()), &s2);
    auto s2_s1 = s2.addTransition(this, SIGNAL(s_go_s2_s1()), &s1);
    machine.setInitialState(&s1);
    machine.start();
    connect(s1_s2, &QAbstractTransition::triggered, this, &MyClass:: transitioned_s1_s2);
    connect(s2_s1, &QAbstractTransition::triggered, this, &MyClass:: transitioned_s2_s1);
  }
}

使用事件转换有点困难,因为您正在使用的事件必须可由状态机克隆。核心模块的状态机只知道如何克隆 NoneTimer 事件 - 请参阅其 cloneEvent 实现。

widgets 模块添加了对各种 GUI/Widgets 事件的支持 - 请参阅此处的 cloneEvent 实现。在紧要关头,您可以将此类 GUI 事件用于您自己的目的 - 毕竟,它们被发送到不以特殊方式解释它们的普通 QObject

您可以提供自己的 cloneEvent 与其他实现链接的实现。

#include <private/qstatemachine_p.h>

class MyClass : public QObject
{
  Q_OBJECT
  QStateMachine machine;
  QState s1, s2;
  QEvent e_s1_s2, e_s2_s1;
  QEventTransition s1_s2, s2_s1;
public:
  Q_SIGNAL void transitioned_s1_s2();
  Q_SIGNAL void transitioned_s2_s1();
  Q_SLOT void go_s2_s1() { QCoreApplication::sendEvent(this, &e_s2_s1); }
  Q_SLOT void go_s1_s2() { QCoreApplication::sendEvent(this, &e_s1_s2); }
  explicit MyClass(QObject *parent = 0) : QObject(parent),
    s1(&machine), s2(&machine),
    e_s1_s2((QEvent::Type)(QEvent::User + 1)),
    e_s2_s1((QEvent::Type)(QEvent::User + 2)),
    s1_s2(this, e_s1_s2.type()),
    s2_s1(this, e_s2_s1.type()) {
    s1_s2.setTargetState(&s2);
    s2_s1.setTargetState(&s1);
    s1.addTransition(&s1_s2);
    s2.addTransition(&s2_s1);
    machine.setInitialState(&s1);
    machine.start();
    connect(&s1_s2, &QAbstractTransition::triggered, this, &MyClass::transitioned_s1_s2);
    connect(&s2_s1, &QAbstractTransition::triggered, this, &MyClass::transitioned_s2_s1);
  }
}

static const QStateMachinePrivate::Handler * last_handler = 0;

static QEvent * cloneEvent(QEvent * e) {
  if (e->type() >= QEvent::User && e->type() < QEvent::User+100) {
    return new QEvent(e->type());
  return last_handler->cloneEvent(e);
}

const QStateMachinePrivate::Handler our_handler = {
    cloneEvent
};

void registerHandler() {
  last_handler = QStateMachinePrivate::handler;
  QStateMachinePrivate::handler = &our_handler;
}
Q_CONSTRUCTOR_FUNCTION(registerHandler())

void unregisterHandler() {
  QStateMachinePrivate::handler = last_handler;

}
Q_DESTRUCTOR_FUNCTION(unregisterHandler())