Please nominate your Qt Champions for 2021!

qtscxml: connect to signals

  • I'm playing with qtscxml and I'm running into a problem - here is a simple state machine

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- enable-qt-mode: yes -->
    <scxml xmlns="" xmlns:qt="" version="1.0" name="KeyboardStateMachine" initial="s1">

    <state id="s1">
          <log expr="Now in s1" />
          <send event="enter_state1"/>
        <transition event="key_pressed" target="s2"/>
    <state id="s2">
            <log expr="Now in s2" />
            <send event="enter_state2"/>
        <transition event="key_released" target="s1"/>


    I want to connect to the signals enter_state1 and enter_state2 on the c++ side.

    QObject::connect(machine, SIGNAL(enter_state1()), this, SLOT(keyLedOff()));
    QObject::connect(machine, SIGNAL(enter_state1()), this, SLOT(lkeyLedOon()));

    but I get an error: QObject::connect: No such signal KeyboardStateMachine::enter_state1()

    What I'm I doing wrong here?

  • Lifetime Qt Champion

    @larry104 said:
    The normal cause for this error is
    1: forgot
    Q_OBJECT macro in a QObject subclass ( your KeyboardStateMachine)

    Using visual studio and qmake/moc.exe is not run.

    Did you check the files generated by
    that they do in fact contain such signals?

  • 1: is done
    #ifndef KEYBOARD_H
    #define KEYBOARD_H

    #include <QObject>
    #include <QHash>
    #include <QColor>
    #include <QDebug>
    #include "statemachine.h"

    class Keyboard : public QObject

    explicit Keyboard(QHash<QString, QObject*> keys, QObject *parent = 0);


    public slots:
    void ledOn();
    void ledOff();
    void keyPressed();
    void keyReleased();

    KeyboardStateMachine m_machine;
    QHash<QString, QObject
    > m_keys;

    #endif // KEYBOARD_H

    2: I'm using qt5.7.0 on ubuntu with qtcreator. My .pro file looks like this:

    QT += qml quick scxml
    CONFIG += c++11
    SOURCES += main.cpp
    STATECHARTS = statemachine.scxml
    RESOURCES += qml.qrc
    HEADERS +=

    It's interesting the statmachine.h gets only created if I run the compile from commandline. How can I tell qtcreator to run the compile of the statemachine so it finds the .h file - but that's a different issue.

    But to answer the question the statemachine.h file create when calling qscxmlc statemachine.scxml does not contain the signals -
    qscxmlc --version is: qscxmlc 1 (Qt 5.7.0)

  • Lifetime Qt Champion

    Hmm, seems to follow the docs closely so should be ok.
    STATECHARTS should make qmake run tool. ?

    I wonder how connect can possible work then if no signals generated. are they somehow exported from QML?

    Sorry, i have never used this new module so Im afraid we must
    hope for others to have better suggestions :)

  • I found the problem - to create a qt signal I need to set the following:

    <send type="qt:signal" event="enter_state1"/>

  • Lifetime Qt Champion

    good found :)

Log in to reply