Results 1 to 4 of 4

Thread: Finding the current state of a QStateMachine

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Finding the current state of a QStateMachine

    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?

  2. #2
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Finding the current state of a QStateMachine

    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:

    Qt Code:
    1. void onMyState1Entered() {
    2. m_currentState = myState1
    3. }
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Finding the current state of a QStateMachine

    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

  4. #4
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Finding the current state of a QStateMachine

    Quote Originally Posted by spawn9997 View Post
    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

Similar Threads

  1. QStateMachine
    By piotr.dobrogost in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2009, 09:17
  2. QStateMachine, not getting entered()
    By jimiq in forum Qt Programming
    Replies: 10
    Last Post: 19th November 2009, 11:17
  3. Finding the 'current' dialog
    By zarkzervo in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2009, 09:20
  4. QStateMachine and signals
    By rossm in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2009, 11:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.