Note that there are some explanatory texts on larger screens.

plurals
  1. POQML state not changing on C++ signal
    primarykey
    data
    text
    <p>I'm struggling to understand the QML state concept.</p> <p>I have the following code:</p> <pre><code>import QtQuick 2.1 import QtQuick.Controls 1.0 Rectangle{ id: myRectangle width: 500 height: 120 state: "init" Button { id: myBtn1 text: "hello" anchors.right: parent.right anchors.rightMargin: 40 anchors.verticalCenter: parent.verticalCenter onClicked: { console.log("trying to change state to myState " myRectangle.state = "customState" } } Button { id: myBtn2 text: "bye" anchors.right: parent.right anchors.rightMargin: 40 anchors.verticalCenter: parent.verticalCenter onClicked: { myCppObject.coolFunction() } visible: false } Connections { target: myCppObject onCoolSignal: { console.log("signal triggered") console.log("state before signal: " + myRectangle.state); myRectangle.state = "init"; console.log("state after signal: " + myRectangle.state); } } states: [ State { name: "init" PropertyChanges { target: myBtn1 visible:true } PropertyChanges { target: myBtn2 visible: false } }, State { name: "customState" PropertyChanges { target: myBtn1 visible: false } PropertyChanges { target: myBtn2 visible: true } } ] } </code></pre> <p>My intension is that on click on the "myBtn1" this button should disappear and "myBtn2" should show up. Therefore I use the states as described and it so far it work! The state changes and the button with the label "bye" is visible.</p> <p>In the next step the click on the button "myBtn2" calls the function "coolFunction" of "myCppObject" which only purpose is to emit a signal called "coolSignal" - which also works! My problem is that I do get the expected output in the console which is like:</p> <pre><code>signal triggered state before signal: customState state after signal: init </code></pre> <p>but "myBtn1" stays hidden and "myBtn2" stays visible!!! So the state is not really changed!!! Any ideas what I'm not understanding about states in QML?</p> <p>btw: if I change <code>myBtn2.onClick</code> to <code>myRectangle.state = "init";</code> the state transition works!!! But I really need it to happen on a custom C++ signal!</p> <p>Thanks for any kind of help in advance.</p> <p>EDIT: I forgot to say that I don't use the source code written on the top inside the main.qml. It's inside an extra QML File called MyElem.qml which used as delegate elem for a listview. This means while running there is more than one of MyElem loaded and all of them have the same id (myRectangle). Is this the reason why I don't see any visual changes? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload