[Qt-interest] QtStateMachine problem

mario dodiesis at gmail.com
Tue Mar 31 12:53:52 CEST 2009


Hi folks...

I cannot understand how QtStateMachine works.

I've got this class... It have to handle a socket connection (and more).
What I want is:
state closed -> state ready -> state connected


MyClass::MyClass(QObject *parent)
 : QObject(parent), m_pConnection(NULL)
{


    QtStateMachine* machine = new QtStateMachine(this);
    QtState *closed         = new QtState();
    QtState *ready          = new QtState();
    QtState *connected      = new QtState();


    closed->addTransition( new OpenTransition(this), ready );
    ready->addTransition( new ConnectTransition(this), connected );

    machine->addState(closed);
    machine->addState(ready);
    machine->addState(connected);

    machine->setInitialState(closed);

    machine->start();
}

I need to do some test before each transition:

class OpenTransition : public QtSignalTransition
{
    public:
        OpenTransition( MyClass* p ) : QtSignalTransition(p,
SIGNAL(tryOpen()))  { }

    protected:
        bool testCondition(QEvent *e) const {
            qDebug() <<"OpenTransition";
            //test goes here
            return true;
        }
 };

 class ConnectTransition : public QtSignalTransition
{
    public:
        ConnectTransition( MyClass* p ) : QtSignalTransition(p,
SIGNAL(tryConnect()))  { }

    protected:
        bool testCondition(QEvent *e) const {
            qDebug() <<"ConnectTransition";
            //test goes here
            return true;
        }
 };

Both testCondition return true cause I assume all is gone ok.

What's wrong in this code?
When I emit Myclass::tryOpen() the "OpenTransition" debug is
printed... but if I emit it again, the debug "ConnectTransition"is
printed too!

In my mind OpenTransition::testCondition() should be called ONLY if
the current state is "closed"... I should emit 100 times tryOpen() and
see only one "OpenTransition" debug.

Furthermore ConnectTransition connect itself to SIGNAL(tryConnect()),
that is never called: i should not see that debug string on my
console!

Any help?
Tnx

-- 
Mario Signorino | www.sgrunt.net



More information about the Qt-interest-old mailing list