Emitting signals from class, if transition in QStateMachine was successful

北城余情 提交于 2019-12-04 16:43:00
  1. To transition on a slot call, you need to somehow bind the slot to a QAbstractTransition. There are two ways of doing it:

    • Use a QEventTransition and send a relevant event to trigger it.

    • Use a QSignalTransition and use an internal signal to trigger it.

  2. To emit signals on state transitions, you can connect the QAbstractTransition::triggered or QState::entered or QState::exited signals to other signals. Remember, in Qt a connection target can be either a slot or a signal.

Thus, using signal transitions:

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);
  }
}

Using event transitions is a bit harder, since the events you're using must be cloneable by the state machine. The core module's state machine only knows how to clone the None and Timer events - see its cloneEvent implementation.

The widgets module adds support for various GUI/Widgets events - see the cloneEvent implementation there. You could, in a pinch, use such GUI events for your own purposes - after all, they are sent to a plain QObject that doesn't interpret them in a special way.

You can provide your own cloneEvent implementation that links with the others.

#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())

I have had the same problem in the past and I have found the easiest way was to inherit fom QState with your own QState class and implement 2 methods called QState::onEntry(QEvent * event) QState::onExit(QEvent * event)

This way you are able to emit any signal you like when you exit and when you enter a new state.

Here is and example:

file 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;

    };

And file 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));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!