我正在开发一个矢量绘图应用程序。我有一个带有4个QStates的QStateMachine:DefaultState,SelectionState,LineDrawingState,PolygonDrawingState。当用户单击相应的工具按钮时,将触发相应的转换并输入状态。用户可以在4种工具(和基础状态)中的任何一种之间切换。
这是问题所在。我必须为每个状态实例化3个转换对象,以便状态机能够从当前状态切换到任何其他状态。我的意思是我必须:
defaultState->addTransition(new SwitchModeTransition(GPM_SelectObject, selectionState) );
defaultState->addTransition(new SwitchModeTransition(GPM_DrawLine, lineDrawingState) );
defaultState->addTransition(new SwitchModeTransition(GPM_DrawPolygon, polygonDrawingState) );
...
selectionState->addTransition(new SwitchModeTransition(GPM_Default, defaultState) );
selectionState->addTransition(new SwitchModeTransition(GPM_DrawLine, lineDrawingState) );
selectionState->addTransition(new SwitchModeTransition(GPM_DrawPolygon, polygonDrawingState) );
... // and so on for every single state
这似乎非常多余。有没有更简单的方法让QStateMachine在触发相应的转换时在所有4个状态之间切换?