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 bind properties to an object's internal properties?
Qt 6.11 is out! See what's new in the release blog

How to bind properties to an object's internal properties?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 394 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    siman
    wrote on last edited by
    #1

    I want to bind rect.width to attr.width so that when I change attr.width, rect.width also changes. However, when I click the button, rect.width does not change. Only when attr is set to {"width": 110, "height": 50} can it be successful, but my attr is a large object and I don't want to change the entire attr.

    Window {
        width: 740
        height: 480
        visible: true
        property var attr : {"width": 100, "height": 50}
        id: root
    
        Rectangle{
            id: rect
            width: attr.width
            height: attr.height
            color: '#ff0000'
        }
    
        Button {
            x: 200
            text: "Ok"
            onClicked: {
                //fail, rect.width not changed
                attr.width += 10
                
                // success, rect.width changed
                // attr = {"width": 110, "height": 50}
                
                console.log('width', rect.width, attr.width)
            }
        }
    }
    
    
    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi @siman, and welcome!

      Unfortunately, you cannot bind to the properties of a raw JavaScript object. You need to use a QtObject instead. When a QObject's property changes, it emits a signal to update other bindings. Raw JavaScript objects cannot emit signals.

      Replace property var attr : {"width": 100, "height": 50} with this:

      property QtObject attr: QtObject {
          property int width: 100
          property int height: 50
      }
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      S 1 Reply Last reply
      3
      • JKSHJ JKSH

        Hi @siman, and welcome!

        Unfortunately, you cannot bind to the properties of a raw JavaScript object. You need to use a QtObject instead. When a QObject's property changes, it emits a signal to update other bindings. Raw JavaScript objects cannot emit signals.

        Replace property var attr : {"width": 100, "height": 50} with this:

        property QtObject attr: QtObject {
            property int width: 100
            property int height: 50
        }
        
        S Offline
        S Offline
        siman
        wrote on last edited by
        #3

        @JKSH I see, I understand now. Thank you so much.

        1 Reply Last reply
        0
        • S siman has marked this topic as solved on

        • Login

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