How to declare a qml list containing both new, declared, and existing items?
-
I'm writing a state machine in qml that shares some PropertyChanges objects over the different states. To avoid duplicate code, I store the shared PropertyChanges instances as private properties that can be mentioned several times. Items that are only used once should be declared as part of the qml list/array.
What I'm trying to do
Button { property PropertyChanges xxx: PropertyChanges { target: grs1 color: 'red' restoreEntryValues: true } states: [ State { name: "State A" changes: [ xxx ] } State { name: "State B" changes: [ xxx, PropertyChanges { target: grs1, text: "state B!" } ] } ] }
However this code doesn't work and generates the warning/error message
Expected token `,'
The question is, is it possible to declare a list containing both new defined objects and existing instances? How does that syntax look?
-
@lcoudeville said in How to declare a qml list containing both new, declared, and existing items?:
changes: [ xxx, PropertyChanges { target: grs1, text: "state B!" } ]
However this code doesn't work and generates the warning/error message Expected token `,'
Is there no line number reported?
Try changing:
target: grs1, text: "state B!"
to:target: grs1; text: "state B!"
-
@jeremy_k, the reported line number points to the line where the PropetyChanges is inline defined:
changes: [ xxx, PropertyChanges { target: grs1, text: "state B!" } ]
Changing to
changes: [ xxx, PropertyChanges { target: grs1; text: "state B!" } ]
did not help :-(
The first '{' on that line is highlighted by qt creator.
-
This looks like a bug.
changes
can either be a list of inline Change elements, or a list accessed byid
or a property. The error message changes depending on the order. With an id first, it complains about missing a,
for inline elements. With an inline element first, it complains about a missing{
for ids.import QtQuick 2.15 import QtQuick.Window 2.15 Window { MouseArea { anchors.fill: parent; onClicked: text.nextState() } Text { id: text anchors.centerIn: parent text: "click to change" PropertyChanges { id: a; target: text; color: 'red' } PropertyChanges { id: b; target: text; text: "State B" } state: "a" states: [ State { name: "a" changes: [ PropertyChanges { target: text; text: "State A" }, PropertyChanges { target: text; color: "blue" } ] }, State { name: "b" changes: [ a, b ] }, State { name: "c" // The next line causes a syntax error changes: [ PropertyChanges{ target: text; text: "State C" }, a ] } ] function nextState() { for (var i = 0; i < states.length; i++) { if (state === states[i].name) { state = states[(i + 1) % states.length].name; return; } } } } }
That said, I don't think I have ever seen a code base attempt to reuse a
PropertyChange
in this way. Organizing into base states that contain the common changes, and extended states that customize is usually the way it's done. With a*Change
inline in aState
, there no need to even mentionchanges