Results 1 to 1 of 1

Thread: SCXML state machines and scripting

  1. #1
    Join Date
    Apr 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default SCXML state machines and scripting

    I'm trying to learn about SCXML (http://www.w3.org/TR/scxml/) and the Qt implementation of it (http://http://doc.trolltech.com/solu...ine/scxml.html).

    I'm experimenting with the simplest state machine I can think of which has 2 states, s1, the initial state and s2. A transition from s1 to s2 occurs when some value exceeds a number.

    I've written a small Qt app that displays a button that says "hello". When the button is pushed, nClicked is incremented. A transition to state s2 should occur when nClicked > 2 and then the button label changes to "goodbye". I have forced a transition if nClicked > 4.

    I have been unable to get the transition to occur automatically. It seems that the value of nClicked is not getting to the script engine to be evaluated. When the transition is forced, the label does change and in fact if I use anything other than myClass.nClicked in the cond parameter in the scxml file, I get an error message about not being able to find the variable when the event is triggered so it appears that I have set the property correctly but the nClicked that the script knows about is not the same as the nClicked that it getting incremented. I'm obviously missing something.

    Thanks,
    Steve

    I have attached my test program and data (and the qscxml.cpp and .h file)
    I am running this on a CentOS 6.2 machine and I used
    $ qmake-qt4 -project
    $ qmake
    Edit the generated Makefile to add /usr/include/QtScript to the INCPATH and -lQtScript to LIBS.
    $ gmake

    junk.scxml:
    Qt Code:
    1. <?xml version='1.0' encoding='UTF-8'?>
    2.  
    3. <scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="s1">
    4. <state id="s1">
    5. <transition event="t" cond="myClass.nClicked > 2" target="s2"/>
    6. </state>
    7.  
    8. <state id="s2">
    9. </state>
    10. </scxml>
    To copy to clipboard, switch view to plain text mode 

    class.h:
    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. #include <iostream>
    5.  
    6. #include <QWidget>
    7. #include <QPushButton>
    8. #include <QScriptEngine>
    9. #include <QString>
    10.  
    11. #include "qscxml.h"
    12.  
    13. using namespace std;
    14.  
    15. class MyClass : public QWidget
    16. {
    17. Q_OBJECT
    18. public:
    19. MyClass(QWidget* parent = 0);
    20. int nClicked;
    21.  
    22. QScxml* sm;
    23. QScriptValue sv;
    24. public slots:
    25. void addClicks();
    26. void changeLabel() { pb->setText("Goodbye"); }
    27. private:
    28. const QString getCurrentState() const;
    29. void printStateName() const;
    30. };
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <iostream>
    2.  
    3. #include <QApplication>
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QObject>
    7. #include <QScriptEngine>
    8. #include <QString>
    9.  
    10. #include "qscxml.h"
    11. #include "class.h"
    12.  
    13. using namespace std;
    14.  
    15. MyClass::MyClass(QWidget* parent)
    16. : QWidget(parent), nClicked(0)
    17. {
    18. pb = new QPushButton("Hello", this);
    19. sm = QScxml::load("./junk.scxml");
    20. sm->registerObject(this, "myClass", true);
    21.  
    22. sv = sm->scriptEngine()->newQObject(this);
    23. sm->scriptEngine()->globalObject().setProperty("myClass", sv);
    24.  
    25. connect(pb, SIGNAL(clicked()), this, SLOT(addClicks()));
    26. connect(sm, SIGNAL(eventTriggered(QString)), this, SLOT(changeLabel()));
    27. sm->start();
    28. }
    29.  
    30. void MyClass::addClicks()
    31. {
    32. nClicked++;
    33. cout << "nclicked = " << nClicked << endl;
    34.  
    35. if (nClicked > 4)
    36. sm->postNamedEvent("t");
    37. }
    38.  
    39. int main(int argc, char* argv[])
    40. {
    41. QApplication app(argc, argv);
    42.  
    43. MyClass* w = new MyClass;
    44. w->show();
    45.  
    46. return app.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. State Machine returning to previous state
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2011, 00:39
  2. Replies: 0
    Last Post: 19th December 2010, 16:03
  3. Scripting question
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2008, 14:54
  4. Scripting engine
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2006, 11:36
  5. Scripting questions
    By fullmetalcoder in forum General Discussion
    Replies: 1
    Last Post: 22nd May 2006, 13:02

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
  •  
Qt is a trademark of The Qt Company.