0

I have created a QStateMachine and I have to get the Event which has caused a transition of the state. Isn't there any opportunity to get inside my slot EnterStateInit() the signal which caused this call. Here my sample code:

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();
}
Lehtim
  • 125
  • 1
  • 12
  • What are you trying to do exactly? – thuga Sep 14 '16 at 13:32
  • @thuga I am working at a Tool to replace a written manual. This Tool does some of the manual steps automatically and others have to be done manual. If I am entering State Init (one step has been done) I want to set the Status of this step. If something went wrong (Precondition unsuccesful or Cancel has been clicked) I am entering state Init and set the status. If I am entering state Init I have to know if something went wrong in before. Therefore I am searching for an opportunity to find out which signal has caused my SLOT EnterStateInit() to react and set the status of the step. – Lehtim Sep 15 '16 at 09:17
  • So why don't you create a new state for the error? After all, it is a different state. – thuga Sep 15 '16 at 10:15
  • @thuga Thanks I havn't thought about that. That might also work. – Lehtim Sep 15 '16 at 10:59

1 Answers1

1

A QState is-a QObject. You're free to reimplement its event() method :) To get an idea of what's going on:

void MyState::event(QEvent * event) {
  qDebug() << event;
  QState::event(event);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • May you explain to me how this reimplementation would look like? Maybe an example for a QState? I have looked at the documentation at doc.qt.io of the qobject::event() method but did not understand it completely. How do I have to implement those QEvent. Would this QEvent be the Signal causing the transition and how would it look like? – Lehtim Oct 17 '16 at 08:49
  • Use `qDebug()` to get an idea of what events come in, and act accordingly. – Kuba hasn't forgotten Monica Oct 17 '16 at 13:04