Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add PropertyChanges to a State in a derived QML component



  • Hi all,

    I have a QML component "comp1" which defines a state "state1" and a 2nd component comp2 which derives from comp1. Now I want to add (!) a PropertyChanges definition in comp2 to the state derived from comp1 without overriding the definitions in comp1. How can I achieve that?

    comp1.qml:

    Item {
      id: item
      property color color1: "white"
      states: [
        State: {
          name: "state1"
          PropertyChanges { target: item; color1: "green" }
        }
      ]
    }
    

    comp2.qml:

    Comp1 {
      id: item
      property color color2: "black"
      states: [
        State: {
          name: "state1"
          PropertyChanges { target: item; color2: "red" }
        }
      ]
    }
    

    main.qml:

    ...
    Comp2 {
      state: "state1"
    }
    ...
    


  • @DuBu 'states' is a (javascript) array of objects, maybe you can manipulate State objects dynamically in Component.onCompleted. Loop through the array and find the object where the property 'name' is "state1". Then take the first object in the 'changes' array and manipulate it.



  • @Eeli-K I'll give it a try, but it forces me to leave the declarative way.



  • The derived component can have a private object with a state binded to outter scoped state with its own set of property changes:

    Comp1 {
      id: item
      property color color2: "black"  
    
      QtObject {
        id: internal
        state: item.state
    
        states: [
          State: {
            name: "state1"
            PropertyChanges { target: item; color2: "red" }
          }
        ]
      }
    }
    

Log in to reply