0
votes

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();
}
1
What are you trying to do exactly? - thuga
@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
So why don't you create a new state for the error? After all, it is a different state. - thuga
@thuga Thanks I havn't thought about that. That might also work. - Lehtim

1 Answers

1
votes

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