QPlace
9th February 2013, 18:02
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
enum actionId
{
actionOne =1,
actionTwo,
...
actionTen
};
...
QMainWindow* mw;
...
QStateMachine sm;
connect (mw, SIGNAL(commandIsCompleted(int)), &sm, SLOT(onCommand(int)));
QState* ps = nullptr;
for (int i = actionId::actionOne; i <= actionId::actionTen; ++i)
{
QState* a = new QState();
a->setProperty("actionid", i);
sm.AddState(a);
if (ps != nullptr)
ps->addTransition(??? to s);
ps = a;
}
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
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
enum actionId
{
actionOne =1,
actionTwo,
...
actionTen
};
...
QMainWindow* mw;
...
QStateMachine sm;
connect (mw, SIGNAL(commandIsCompleted(int)), &sm, SLOT(onCommand(int)));
QState* ps = nullptr;
for (int i = actionId::actionOne; i <= actionId::actionTen; ++i)
{
QState* a = new QState();
a->setProperty("actionid", i);
sm.AddState(a);
if (ps != nullptr)
ps->addTransition(??? to s);
ps = a;
}
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