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 automatically update a property?

how to automatically update a property?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 5 Posters 622 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I have a display element whose visibility depends on the length of a JS array. If the array has 2 elements, my display should be true; if not, it would be false.

    Here's how I'd like to do it:

    Rectangle {
    	id: valvesPane
    	visible: (equipmentEdit.spaceAssignments.length === 2) // doesn't update
    

    I've created a workaround -- whenever the JS array is modified, I call a function:

    function processSpaceSelection(spaceUuid, add) {
    	if (add) {
    		if (spaceAssignments.length === 2) {
    			valvesPane.visible = true
    		}
    	}
    	else {
    		valvesPane.visible = false
    	}
    }
    

    This works, but seems kind of cumbersome. Is there a better way to do this?

    Thanks...

    sierdzioS J.HilkJ 2 Replies Last reply
    0
    • L lemons

      @J-Hilk Note: If you use .push(), add a .slice(0) to trigger the onChanged() signal:

      Button {
          text: "push item and force array update"
          onClicked: {
              myArray.push("a")
              myArray = myArray.slice(0)
          }
      }
      

      EDIT:
      Just noticed it also works with simple re-assignment.
      I always used the shallow copy with .slice(0).

      Button {
          text: "push item and force array update"
          onClicked: {
              myArray.push("a")
              myArray = myArray
          }
      }
      

      This would also work, but I don't like to use concat for readability reasons:

      Button {
          onClicked: {
              myArray = myArray.concat("a")
          }
      }
      

      P.S.: I still don't know why QML does not pick up .push() automatically.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #5

      @lemons said in how to automatically update a property?:

      myArray = myArray

      calling myArrayChanged() would also work.

      L 1 Reply Last reply
      2
      • mzimmersM mzimmers

        Hi all -

        I have a display element whose visibility depends on the length of a JS array. If the array has 2 elements, my display should be true; if not, it would be false.

        Here's how I'd like to do it:

        Rectangle {
        	id: valvesPane
        	visible: (equipmentEdit.spaceAssignments.length === 2) // doesn't update
        

        I've created a workaround -- whenever the JS array is modified, I call a function:

        function processSpaceSelection(spaceUuid, add) {
        	if (add) {
        		if (spaceAssignments.length === 2) {
        			valvesPane.visible = true
        		}
        	}
        	else {
        		valvesPane.visible = false
        	}
        }
        

        This works, but seems kind of cumbersome. Is there a better way to do this?

        Thanks...

        sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        @mzimmers your first code should work. Are you sure a property change signal is emitted when spaceAssignments is modified?

        (Z(:^

        mzimmersM 1 Reply Last reply
        1
        • mzimmersM mzimmers

          Hi all -

          I have a display element whose visibility depends on the length of a JS array. If the array has 2 elements, my display should be true; if not, it would be false.

          Here's how I'd like to do it:

          Rectangle {
          	id: valvesPane
          	visible: (equipmentEdit.spaceAssignments.length === 2) // doesn't update
          

          I've created a workaround -- whenever the JS array is modified, I call a function:

          function processSpaceSelection(spaceUuid, add) {
          	if (add) {
          		if (spaceAssignments.length === 2) {
          			valvesPane.visible = true
          		}
          	}
          	else {
          		valvesPane.visible = false
          	}
          }
          

          This works, but seems kind of cumbersome. Is there a better way to do this?

          Thanks...

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @mzimmers it should work,

          take this example:

          Window {
              id:root
              width: 640
              height: 480
              visible: true
              title: qsTr("Hello World")
          
              property var myArray: []
              onMyArrayChanged: console.log(myArray)
              Column{
                  Button{
                      id:btn1
                      onClicked: myArray += ["a"]
                  }
                  Text {
                      id: name
                      text: myArray.length
                  }
              }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          L 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @mzimmers it should work,

            take this example:

            Window {
                id:root
                width: 640
                height: 480
                visible: true
                title: qsTr("Hello World")
            
                property var myArray: []
                onMyArrayChanged: console.log(myArray)
                Column{
                    Button{
                        id:btn1
                        onClicked: myArray += ["a"]
                    }
                    Text {
                        id: name
                        text: myArray.length
                    }
                }
            }
            
            L Offline
            L Offline
            lemons
            wrote on last edited by lemons
            #4

            @J-Hilk Note: If you use .push(), add a .slice(0) to trigger the onChanged() signal:

            Button {
                text: "push item and force array update"
                onClicked: {
                    myArray.push("a")
                    myArray = myArray.slice(0)
                }
            }
            

            EDIT:
            Just noticed it also works with simple re-assignment.
            I always used the shallow copy with .slice(0).

            Button {
                text: "push item and force array update"
                onClicked: {
                    myArray.push("a")
                    myArray = myArray
                }
            }
            

            This would also work, but I don't like to use concat for readability reasons:

            Button {
                onClicked: {
                    myArray = myArray.concat("a")
                }
            }
            

            P.S.: I still don't know why QML does not pick up .push() automatically.

            GrecKoG 1 Reply Last reply
            0
            • L lemons

              @J-Hilk Note: If you use .push(), add a .slice(0) to trigger the onChanged() signal:

              Button {
                  text: "push item and force array update"
                  onClicked: {
                      myArray.push("a")
                      myArray = myArray.slice(0)
                  }
              }
              

              EDIT:
              Just noticed it also works with simple re-assignment.
              I always used the shallow copy with .slice(0).

              Button {
                  text: "push item and force array update"
                  onClicked: {
                      myArray.push("a")
                      myArray = myArray
                  }
              }
              

              This would also work, but I don't like to use concat for readability reasons:

              Button {
                  onClicked: {
                      myArray = myArray.concat("a")
                  }
              }
              

              P.S.: I still don't know why QML does not pick up .push() automatically.

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #5

              @lemons said in how to automatically update a property?:

              myArray = myArray

              calling myArrayChanged() would also work.

              L 1 Reply Last reply
              2
              • sierdzioS sierdzio

                @mzimmers your first code should work. Are you sure a property change signal is emitted when spaceAssignments is modified?

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #6

                @sierdzio said in how to automatically update a property?:

                @mzimmers your first code should work. Are you sure a property change signal is emitted when spaceAssignments is modified?

                spaceAssignments is just a JS array defined (in another file) like this:

                    property var spaceAssignments: [] 
                

                Does QML build in a signal for when this is changed? I didn't think so.

                1 Reply Last reply
                0
                • GrecKoG GrecKo

                  @lemons said in how to automatically update a property?:

                  myArray = myArray

                  calling myArrayChanged() would also work.

                  L Offline
                  L Offline
                  lemons
                  wrote on last edited by
                  #7

                  @GrecKo said in how to automatically update a property?:

                  @lemons said in how to automatically update a property?:

                  myArray = myArray

                  calling myArrayChanged() would also work.

                  Thanks for pointing it out, I was thinking too complicated.
                  Most likely because QtCreator does not auto-suggest it :D

                  But why doesn't Qt automatically call the signal on .push()?

                  @mzimmers the signal should be there, but not automatically emitted if you use push() on the array:

                  Window {
                      id: app
                      width: 640
                      height: 480
                      visible: true
                  
                      Dummy {
                          id: dummyComponent
                          // Dummy.qml
                          // Item { property var myArray: [] }
                      }
                  
                      Column {
                          Rectangle {
                              color: "red"
                              width: 100
                              height: 100
                              visible: dummyComponent.myArray.length === 2
                          }
                  
                          Button {
                              text: "push item and force array update"
                              onClicked: {
                                  dummyComponent.myArray.push("a")
                                  dummyComponent.myArrayChanged()
                              }
                          }
                          Text {
                              text: dummyComponent.myArray.length
                          }
                      }
                  }
                  
                  1 Reply Last reply
                  1
                  • mzimmersM mzimmers has marked this topic as solved on
                  • mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #8

                    Thanks for all the suggestions, everyone. I decided that emitting the signal directly was the most straightforward way to go. I didn't realize that any of those possibilities even existed...I guess I haven't found the documentation that explains this.

                    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