<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QStateMachine QML &#x2F; enums]]></title><description><![CDATA[<p dir="auto">Following <a href="https://lists.qt-project.org/pipermail/qt-qml/2010-November/001810.html" target="_blank" rel="noopener noreferrer nofollow ugc">this</a> example, I am trying to use a C++ QStateMachine in QML, exposing the current state as property:<br />
My property is an enum, as I need to use many more states in the real application.</p>
<pre><code>class Application : public QObject  {   
public:
    Q_OBJECT
    //QML_ELEMENT           // prevents from using the State in when clause of QML states at all
    QML_NAMED_ELEMENT(App)  // ok, but emits an error in QML

    enum State { OFF, ON, };  // using enum class doesn't make a difference
    Q_ENUM(State)
    
    Q_PROPERTY(Application::State state READ state NOTIFY stateChanged)

Application()  {
    auto offState = new QState(&amp;m_stateMachine);
    offState-&gt;assignProperty(&amp;m_stateMachine, g_state_id, State::OFF);
    QObject::connect(offState, &amp;QState::entered, this, &amp;Application::setState);

    auto onState = new QState(&amp;m_stateMachine);
    onState-&gt;assignProperty(&amp;m_stateMachine, g_state_id, State::ON);
    QObject::connect(onState, &amp;QState::entered, this, &amp;Application::setState);

    offState-&gt;addTransition(this, &amp;Application::toggle, onState);
    onState-&gt;addTransition(this, &amp;Application::toggle, offState);

    m_stateMachine.setInitialState(offState);
    m_stateMachine.start();
}

Application::State state() const {
    return static_cast&lt;Application::State&gt; (m_stateMachine.property("state").toInt());
}
bool isState(Application::State theState) const {
    return state() == theState;
}

signals:
   void stateChanged();
   void toggle();

private:
    QStateMachine m_stateMachine;
    State m_state;
    
    void Application::setState() {
       if (state() == m_state) return;    
       m_state = state();    
       emit stateChanged(); 
    }
};    
</code></pre>
<p dir="auto">In my RowLayout element, I want to use the state like so:</p>
<pre><code>// ... inside some RowLayout target onOffBtn is a simple Button
states : [
    State {
    	name: "off"
    	//when: app.state === 0               // Ok
        //when: app.isState(App.OFF)     // not working at all (0 in all cases)
    	when: app.state === App.OFF   // TypeError: Cannot read property 'state' of null
    	PropertyChanges { target: onOffBtn; text: "turn ON"; }
    },
    State {
    	name: "on"
    	//when: app.state === 1               // Ok
    	//when: app.isState(App.ON)       // not working at all   	when: app.state === App.ON
    	PropertyChanges { target: onOffBtn; text: "turn OFF"; }
    }
]
</code></pre>
<p dir="auto">Using the enum state from the registered type App, I get an error about the property state being null. But the code is working, the when part is evaluated correctly.<br />
Using the enum in another context (QML Button), like</p>
<pre><code>onClicked : {
     if (app.isState(App.OFF))
          console.log("app.isState(App.OFF)")
     else if (app.isState(App.ON))
          console.log("app.isState(App.ON)")

     if (app.state === App.OFF)
          console.log("app.state === App.OFF")
     else if (app.state === App.ON)
          console.log("app.state === App.ON")
}
</code></pre>
<p dir="auto">onClicked produces the expected outputs without errors or warnings.</p>
<p dir="auto">I expected a much better way coupling the C++ SM with QML, one without using enums to react on the current state in QML (not using the QML StateMachine type).</p>
<p dir="auto">Am I using the states / property binding the wrong way? What is the suggested way to handle the current C++ SM state in QML?</p>
]]></description><link>https://forum.qt.io/topic/142462/qstatemachine-qml-enums</link><generator>RSS for Node</generator><lastBuildDate>Thu, 07 May 2026 07:34:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/142462.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 Jan 2023 12:35:44 GMT</pubDate><ttl>60</ttl></channel></rss>