Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bindings Question in Qt
Qt 6.11 is out! See what's new in the release blog

Bindings Question in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 815 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.
  • carles.sole.grauC Offline
    carles.sole.grauC Offline
    carles.sole.grau
    wrote on last edited by
    #1

    Hi everybody,
    I see a different behavior when you binds a simple var and an array.
    For example, with this QML code:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        property var testArray: [1,2,3,4,5]
        property var test: 1
    
       Button {
           text: "click me"
           property var testArrayHierachy: testArray
           property var testHierachy
           onClicked: {
               console.log(test);
               testHierachy = 9;
               console.log(test);
               //When Array
               console.log(testArray);
               testArrayHierachy[0] = 9;
               console.log(testArray);
           }
       }
    }
    

    When I click the button the results are:

    qml: 1
    qml: 1
    qml: [1,2,3,4,5]
    qml: [9,2,3,4,5]

    Why when it's just a variable if you modify a value of one of the two binded items the other one does not change and when it's an array the both items are modified. It's for the same reason that in JavaScript in functions an array as a parameter is passed as a reference?
    And, this is the expected result? Or it's a bug?

    Thank you very much.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      This is more of how arrays work in Javascript. It is question of pass-by-value or pass-by-reference. If u are assigning the individual values they works as it is pass-by-reference. If you are setting the value of Array itself, then it will act like pass-by-value.

      Just try with additional stuff like the follows.

         ```
      

      onClicked: {
      console.log(test);
      testHierachy = 9;
      console.log(test);
      //When Array
      console.log(testArray);
      testArrayHierachy[0] = 9;
      console.log(testArray);
      testArrayHierachy = [10,20,30,40,50]
      console.log(" OLD="+testArray);
      console.log(" New="+testArrayHierachy);
      }

      Now number changes by reference. But assigning the array to testArrayHierachy new array values does not change the testArray.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      2

      • Login

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