Results 1 to 8 of 8

Thread: QState

  1. #1
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QState

    Hi!

    I have a dialog with comboBox, couple of line edits and other stuff. In comboBox I have several entries that define what fields should be disabled and witch shouldn't be.

    What I want to achieve is simple, when current index in comboBox changes, application should go to state defined by index of newly selected item. Now everything is handled by if else statements, but i decided to use QStateMachine instead.
    When indexes in comboBox change in one direction, everything workis fine:
    state1 -> state2;
    state2 -> state3;
    state3 -> state4;
    To define two way index change I neeed to add new transactions to state machine, that will les the state machine to go back
    state1 -> state2; // UP
    state1 -> state4; // DOWN
    state2 -> state3;
    state2 -> state1;
    state3 -> state4;
    state3 -> state2;
    state4 -> state1;
    state4 -> state3;

    To this works if user uses scroll to go through all comboBox fields, but when he choose something by clicking it (e.g. index from 4 to 2) I have no transaction that can handle this "action", so I need to add more transactions... In other words I need to write ALL-TO-ALL transactions to achieve what I want.
    Is there another way of doing something like this? some kind of "stateless stateMachine" or a mechanism that can by used to "call" specific state?
    I know stat state machine is designed not to do something like I want, but it is quite easy to use and it's simpler then write own code

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QState

    Cam you show some code to demonstrate what you have? It is hard to make anything out from your description.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QState

    state.tar.bz2
    This is a simple test project that contains a state machine. Please compile this, click "State up" button, and then try to change index of comboBox and observe the behavior of checkBoxes on the right, this is the desired behavior (move from one state to any other). Then look on mainwindow.cpp file lines 76 to 94, those are needed transactions for this kind of behavior, there is a lot of code... but this is not the worst news. When I add an item to comboBox, I need to define n-1 more transitions (where n is number of items in comboBox).

    I didn't know how to add a transitions to SIGNALs attribute, so I decided to create a simple class that emits a signal depending on index of selected in comboBox index and base transitions on that signal instead.

    I know that I can use signal and slots mechanism to control the ui behavior, but this approach has something that I don't know how to implement (in simple way) in SLOT and that is... QStateMachine::setGlobalRestorePolicy(QState::Rest oreProperties);

    I hope that this description of my problem is better

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QState

    If you need a transition that evaluates signal arguments and only triggers on certain values, have a look at http://qt-project.org/doc/qt-5/qsignaltransition.html

    Cheers,
    _

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QState

    Did you try simply grouping states?

    Something alone the lines of:

    Qt Code:
    1. QState *masterState = new QState;
    2. QState *s1 = new QState(masterState);
    3. QState *s2 = new QState(masterState);
    4. QState *s3 = new QState(masterState);
    5. QState *s4 = new QState(masterState);
    6. masterState->setInitialState(s1);
    7. machine.addState(masterState);
    8. masterState->addTransition(signalEmitter, SIGNAL(received_1()), s1);
    9. masterState->addTransition(signalEmitter, SIGNAL(received_2()), s2);
    10. masterState->addTransition(signalEmitter, SIGNAL(received_3()), s3);
    11. masterState->addTransition(signalEmitter, SIGNAL(received_4()), s4);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QState

    Wysota, thanks for answer, but unfortunately, I didn't understand you or your state machine is a "one way ticket" machine

    Qt Code:
    1. QState *masterState = new QState;
    2. QState *s1 = new QState(masterState);
    3. QState *s2 = new QState(masterState);
    4. QState *s3 = new QState(masterState);
    5. QState *s4 = new QState(masterState);
    6. masterState->setInitialState(s1);
    7. machine.addState(masterState);
    8. masterState->addTransition(signalEmitter, SIGNAL(received_1()), s1);
    9. masterState->addTransition(signalEmitter, SIGNAL(received_2()), s2);
    10. masterState->addTransition(signalEmitter, SIGNAL(received_3()), s3);
    11. masterState->addTransition(signalEmitter, SIGNAL(received_4()), s4);
    To copy to clipboard, switch view to plain text mode 

    You got only one way transitions, when you go to state s1, you don't have any transition that will let you to go back from it... Or I'd simply not understand you. Please came back with a more extensive example

    anda_skoa for this example I didn't need this kind of functionality, but I'll probably need this in the future, thanks for answer

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QState

    Did you try my code? The idea is that all the child states inherit transitions from the parent state so each of the four transitions defined in the parent state should work in any of the child states. I haven't tested it but it should work, if I understood the docs correctly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QState

    I tried to get it working, but unfortunately I get stuck in initial child state

Similar Threads

  1. How to know the signal which caused a qstate transition
    By ClintEastwood in forum Qt Programming
    Replies: 6
    Last Post: 24th April 2013, 15:27
  2. QState machine using multiple QThreads
    By PeterWurmsdobler in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2012, 10:55
  3. Perform a cyclic task in a QState
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2010, 23:18
  4. Replies: 4
    Last Post: 25th March 2010, 10:32
  5. rotate in QState
    By estel in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 12:39

Tags for this Thread

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.