简体   繁体   中英

How do I define QML states dynamically using JavaScript

Is there any way of creating a state object dynamically using a javascript loop for instance.

Result should look like:

states: [
    State{
        name: "name1"
        when: somevar === onevar

        //stuff
    },
    State{
        name: "name2"
        when: somevar === anothervar

        //stuff
    }
]

but I want to start with an arbitrary javascript array, like

var myStatesObj = [{
    "name": "name1"
    "condition": onevar
},{
    "name": "name2"
    "condition": anothervar
}]

and use a loop to create the states QML object.

Is this possible?

I did some tests and I don't think you can dynamically modify the states list (seems like it's an object and no array internally)!?

Here my tests: create a dynamic state (that does work with a Component template)

Component {
    id: stateTemplate
    State {}
}

// then use this code to create an state object
var st = stateTemplate.createObject(parent, { name: "testSate", when: yourCondition })

try to add the state

states.push(st) // error because "states" is not an array!?
states[states.length] = st // no error but the stages object didn't change at all?

You can iterate though all the existing states and everything, but it seems the states object is not changeable or am I missing something here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM