How can I detect ignored/rejected posted QEvent to QStateMachine

989 views Asked by At

I need to see if a custom posted event to the state machine was accepted or not. My approach is to subclass QStateMachine. See http://doc.qt.nokia.com/latest/statemachine-api.html section Events, Transitions and Guards

I'm wondering is there is not something I missed in the code below. Is there not another/better approach?

Basically here is the same as per the Qt Doc:

bool MyStateTransition::eventTest(QEvent *e)
{
    if (e->type() != QEvent::Type(QEvent::User+1)) // MyEvent
        return false;

    MyEvent *se = static_cast<MyEvent*>(e);

    if(m_value != se->value)
    {
        se->setRejected(true);
        return false;
    }
    qDebug() << "MyStateTransition::eventTest() - Transition " << m_value << " accepting event " << se->value;
    se->setRejected(false);
    return true;
}

And the simplest way I could find to far to detect the reject is this:

void MyStateMachine::endSelectTransitions(QEvent *event)
{
    if (event->type() != QEvent::Type(QEvent::User+1)) return;

    MyEvent *se = static_cast<MyEvent*>(event);
    if(se->rejected())
        emit eventRejected(se);
    else 
        //Not really needed since we can use triggered() which will fire after
        emit eventAccepted(se); 

}
1

There are 1 answers

0
Derick Schoonbee On BEST ANSWER

Ok, finally answering my own question: It works but is basically a hack since not officially documented.