Results 1 to 5 of 5

Thread: Advise sought for QStateMachine implementation

  1. #1
    Join Date
    Jul 2007
    Posts
    121
    Thanks
    38
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Advise sought for QStateMachine implementation

    I never used QStateMachine, but always wanted to try. I think that following example might be just the right application to try QStateMachine on.

    Scenario:
    There is an application that implements ten different actions. Each action is invoked from the main menu and execution takes variable time, from 1 minute to several hours. Each action has unique identifier (enum) and at the end of each execution the main window (where actions are implemented) emits commandIsCompleted(int actionid) signal.

    I am in need to implement scheduling for these commands. Schedule should start at a given time and execute all ten commands sequentially, starting the new one when previous is completed.
    I am thinking of creating an instance of QStateMachine and add the QState for each of the commands.

    Upon startup statemachine subscribes to "commandIsCompleted" signal and posts the event with the action ID of the first command to the main window. On each signal statemachine should start the next state which in turn will post new event to the main window.

    So, my pseudocode looks like this

    Qt Code:
    1. enum actionId
    2. {
    3. actionOne =1,
    4. actionTwo,
    5. ...
    6. actionTen
    7. };
    8. ...
    9. ...
    10. QStateMachine sm;
    11. connect (mw, SIGNAL(commandIsCompleted(int)), &sm, SLOT(onCommand(int)));
    12.  
    13. QState* ps = nullptr;
    14. for (int i = actionId::actionOne; i <= actionId::actionTen; ++i)
    15. {
    16. QState* a = new QState();
    17. a->setProperty("actionid", i);
    18. sm.AddState(a);
    19. if (ps != nullptr)
    20. ps->addTransition(??? to s);
    21. ps = a;
    22. }
    To copy to clipboard, switch view to plain text mode 
    That's where I stop: I don't understand how to organize transitions and also how to organize the sending of the event in each action..., or even if statemachine is appropriate at all.

    I can easily implement the scheduling without statemachine, but want to understand if it can be applied to the above scenario.


    Thanks

  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: Advise sought for QStateMachine implementation

    I don't think this is a good usecase for QStateMachine. The most naive implementation of a solution to your problem is the following:

    Qt Code:
    1. class Object : public QObject {
    2. Q_OBJECT
    3. public:
    4. Object(QObject *parent = 0) : QObject(parent) { m_current = 0; }
    5. public slots:
    6. void start() { m_current = 0; execute(); }
    7. private slots:
    8. void execute() {
    9. QString method;
    10. switch(m_current) {
    11. case 0: method = "stage0"; break;
    12. case 1: method = "stage1"; break;
    13. // ...
    14. }
    15. QMetaObject::invokeMethod(this, method, Qt::QueuedConnection);
    16. }
    17. void stage0() { ... emit stageComplete(m_current++); QMetaObject::invokeMethod(this, "execute", Qt::QueuedConnection); }
    18. void stage10() { ... emit stageComplete(m_current); emit finished(); }
    19. signals:
    20. void stageComplete(int);
    21. void finished();
    22. private:
    23. int m_current; // or enum
    24. };
    To copy to clipboard, switch view to plain text mode 

    Of course stageX() doesn't have to be a single function. The point is that at the end of its execution it adds to m_current and calls invokeMethod("execute"); (or invokeMethod("stageX+1") )

    QStateMachine doesn't really offer you any benefits for such a linear flow.
    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. The following user says thank you to wysota for this useful post:

    QPlace (9th February 2013)

  4. #3
    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: Advise sought for QStateMachine implementation

    Well, a state machine has the advantage of making the process very explicit.

    The commandIsCompleted would trigger the transition into the next state and each state's entered signal would trigger running the respective task.

    It is not necessary but can help to make the intent of the code more explicit.

    Cheers,
    _

  5. #4
    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: Advise sought for QStateMachine implementation

    Quote Originally Posted by anda_skoa View Post
    Well, a state machine has the advantage of making the process very explicit.
    Well... for a linear flow you can just connect signals and slots directly. I still fail to see a benefit of using QStateMachine here. QStateMachine can't even answer the question "what state am I in?" without writing additional code.
    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. #5
    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: Advise sought for QStateMachine implementation

    The benefit is not in the behavior but in documenting the workflow very explicitly.

    Developer tools like Gammaray can then also easily introspect and display the state of the processing pipeline.

    Cheers,
    _

Similar Threads

  1. Advise sought on QEventLoop behavior
    By TorAn in forum Qt Programming
    Replies: 18
    Last Post: 9th June 2011, 11:35
  2. Advise sought on working with binary files across OSes
    By TorAn in forum General Programming
    Replies: 2
    Last Post: 19th September 2010, 06:44
  3. Replies: 4
    Last Post: 1st June 2010, 11:42
  4. advise sought on TreeView model design
    By QPlace in forum Qt Programming
    Replies: 0
    Last Post: 25th June 2009, 14:36
  5. Replies: 6
    Last Post: 25th February 2008, 21:18

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.