PDA

View Full Version : Finding the current state of a QStateMachine



pherthyl
29th March 2010, 22:51
I'm trying to start using the Qt State machine framework for more things, but one of the central problems I keep running into is that I need to find out what the current state is, and do the appropriate thing.

However, there does not seem to be any way to figure out what state a QStateMachine is currently in, which is completely perplexing to me, since this seems like such a basic thing.

For example, say I have an item on a scene. The first time someone clicks on it, we are in the "list view" state and the item is selected while everything else fades out. The second time it is clicked, I perform some action. So to figure out what I should do in the click handler, I have to check the state (are we in the "list view" state or the "selected" state?). Unfortunately I can't tell without having another variable that will keep track of current state. That seems a bit stupid, since the state machine should do that.

However, perhaps I'm looking at the problem wrong, and I haven't fully transitioned my thinking to a state-based approach.

All the examples in Qt for state machines seem to be so simple that they are essentially useless (like the stoplight example). Does anyone have a good example of a state machine being used for a more realistic application? Why can't I find out what state I am currently in?

pherthyl
30th March 2010, 00:51
I see that someone else has asked this question, and the answer was to listen for entered() signals from the states.
Of course that works, but doesn't that seem kind of redundant?
If I have N states I need to write N slots like this:



void onMyState1Entered() {
m_currentState = myState1
}


That makes it very cumbersome to use large state machines. Seems like this should really be a feature of QStateMachine, no?

spawn9997
27th May 2010, 17:41
I see no one has replied to your post. If you have not received an answer yet, I found a bug report about the exact same issue from back in October 2009.
http://bugreports.qt.nokia.com/browse/QTBUG-5032

Since there can be more than one active state they have this function.

QStateMachine::configuration ()

You can compare the objects themselves if you have the list of your states handy. Another way is to name your states and keep the list of names for comparison.

(Why this function is not called activeStates() I don't know. :) )

spawn9997

leninbooter
18th July 2012, 15:14
I see no one has replied to your post. If you have not received an answer yet, I found a bug report about the exact same issue from back in October 2009.
http://bugreports.qt.nokia.com/browse/QTBUG-5032

Since there can be more than one active state they have this function.

QStateMachine::configuration ()

You can compare the objects themselves if you have the list of your states handy. Another way is to name your states and keep the list of names for comparison.

(Why this function is not called activeStates() I don't know. :) )

spawn9997

Because in Turing Machines theory, "configuration" means exactly that, the current states or state of the machine ;)