Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to declare a qml list containing both new, declared, and existing items?
QtWS25 Last Chance

How to declare a qml list containing both new, declared, and existing items?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmllist of objects
4 Posts 2 Posters 497 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lcoudeville
    wrote on last edited by
    #1

    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?

    jeremy_kJ 1 Reply Last reply
    0
    • L lcoudeville

      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?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @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!"

      Asking a question about code? http://eel.is/iso-c++/testcase/

      L 1 Reply Last reply
      0
      • jeremy_kJ jeremy_k

        @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!"

        L Offline
        L Offline
        lcoudeville
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          This looks like a bug. changes can either be a list of inline Change elements, or a list accessed by id 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 a State, there no need to even mention changes

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved