[Qt-interest] Using QStateMachine in large apps (Was qscxml and Qhttp)

noam.rosenthal at nokia.com noam.rosenthal at nokia.com
Fri Oct 22 08:28:02 CEST 2010


Hi Stephen.
Yes, QScxml is meant for implementing the state machine in XML+QtScript, while scc is meant to generate QStateMachine C++ files from SCXML.
The scc one provides better support for different types of QEvent, while the QScxml one is more flexible because it uses Javascript.

To implement something like what you're looking for in QScxml, I'd look closely at the blackjack and calculator examples. 
I think that in QScxml, what you're asking for would be implemented something like this:

<scxml>
  <state id="no_selection">
    <onentry><script>myObject.something = 9;</script></onentry>
    <transition target="single_selection" event="q-signal:myObject.selectionChanged(eventType)" cond="_event.data[0]==1"></transition>
  </state>
  <state id="singla_selection">
    <onentry><script>myObject.something = 42;</script></onentry>
    <transition target="single_selection" event="q-signal:myObject.selectionChanged(eventType)" cond="_event.data[0]==2"></transition>
  </state>
</scxml>

It's a bit different from subclassing transitions - SCXML works more on the concept of events+conditions than on an object oriented concept.
But I truly urge you to take some time with the examples, they're the best documentation.

Hope this helps,
~No'am
________________________________________
From: qt-interest-bounces at trolltech.com [qt-interest-bounces at trolltech.com] On Behalf Of ext Stephen Kelly [steveire at gmail.com]
Sent: Friday, October 22, 2010 1:59 AM
To: qt-interest at trolltech.com
Subject: [Qt-interest] Using QStateMachine in large apps (Was qscxml and        Qhttp)

noam.rosenthal at nokia.com wrote:

> Actually, the problem is the build order
> You have to make install scc before you can compile the examples under
> examples/scc. That's because the stuff under examples/scc requires scc.prf
> to be installed in [QTDIR]/mkspecs/features.
>
> So:
>
> cd scc
> [QTDIR]/bin/qmake
> make install
>
> cd ..
> make

Thanks for that. I managed to build the module and build and try the
examples. They seem to work well. I tried make docs, but that didn't work,
so I tried cd doc && qdoc scxml.qocconf, which also did nothing useful (I
don't really know how to use qdoc), so I just read the doc source.

I'm new to QStateMachine and am planning to use it in kontact-mobile and
another large project. I just read through the docs a couple of days ago. I
was wondering if there was a better way to maintain the state machine than
in C++ code. I thought at first that qscxml would be the answer, and it
might be. Maybe you can clarify. It seems to aimed towards implementing the
state machine in xml+QtScript.

My aim is to implement my own transition class so that I can change states
based on selections in a QItemSelectionModel. Such a thing would be defined
in a header file which could be specified in a similar way to how they are
specified in .ui files.

Given that, I'd do something like this (and I know it's not valid scxml :)):

<statemachine>
  <state id="no selection"
         object="myObject"
         property="something"
         value="9" />
  <state id="single selection"
         object="myObject"
         property="something"
         value="42" />
  <transition type="ModelSelectionTransition"
              definition="modelselectiontranstion.h"
              from="no selection"
              to="single selection">
    <property name="mode" value="SingleSel" />
  </transition>
</statemachine>

the ModelSelectionTransition class would set up the connection to the
selectionChanged signal and would implement the logic of whether to run the
transition for a particular value in the selectionChanged.

So the above would generate code something like this:

QObject *myObject = getSomeObject();
myObject->setObjectName("myObject");

QStateMachine machine;
QState *noSel = new QState();
noSel->assignProperty(myObject, "something", 9);
QState *singleSel = new QState();
singleSel->assignProperty(myObject, "something", 42);

ModelSelectionTransition *tr = new ModelSelectionTransition(SingleSel);
tr->setTargetState(singleSel);
noSel->addTransition(tr);

where ModelSelectionTransition is something like

class ModelSelectionTransition
{

  bool eventTest(QEvent *e)
  {
    if (m_type == SingleSel)
      // if the selection model has only one item, accept it.
    if (m_type == MultiSel)
      // ...
  }

};

The engine for the scxml description would need to have a Context to inject
the myObject into it etc. It looks like QScxml::registerObject does exactly
that. What is the RightWay to use qscxml to what I'm trying to achieve?

Sorry for the brain-dump post. I haven't written any code for this yet, only
read documentation so far. Hope I got it right.

All the best,

Steve.


_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list