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. Object.defineProperty outside a JS scope

Object.defineProperty outside a JS scope

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmldefineproperty
3 Posts 2 Posters 417 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.
  • M Offline
    M Offline
    Moisi
    wrote on last edited by
    #1

    Hi,

    I'm trying to add dynamically properties to Rectangle objects.

    My code works fine except one point: I'm not able to access to my new created properties outside a Javascrip scope.

    Here is my code:

    Window
    {
    	visible: true
    	width: 1300
    	height: 800
    	title: qsTr("Tests")
    
    	id: component
    
    	Column
    	{
    		spacing: 3
    		Label { text: rectA.myValue ? rectA.myValue : "value not found" } // display "value not found"
    		Label { text: rectB.myValue ? rectB.myValue : "value not found" } // display "value not found"
    		Button
    		{
    			text: "check a"
    			onClicked: console.log(rectA.myValue) // display "This is a test"
    		}
    	}
    
    	Rectangle { id: rectA }
    	Rectangle { id: rectB }
    
    	property var objects: [rectA, rectB]
    
    	Component.onCompleted:
    	{
    		objects.forEach( (o) =>
    		{
    			let obj = Object.defineProperty(o, "myValue",
    			{
    				enumerable: true,
    				configurable: true,
    				writable: true,
    				value: "This is a test"
    			})
    			console.log(o.myValue) // display "This is a test"
    		})
    	}
    }
    

    Did I make a mistake or it's impossible to dynamically add a property to an object and read it outside a JS scope ?

    Thanks a lot ! ^^

    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      This is because it is not an QML property. This means when it is set - nothing changed for your binding at label text. Sad story, but since it is not an QML property, you cannot call o.myValueChanged();.
      But you can do another trick. Add another dummy property to yor rectangles:

      Rectangle { id: rectA; property string dummy: "" }
      

      Then add this property to label text:

      Label { text: (rectA.myValue ? rectA.myValue : "value not found") + rectA.dummy }
      

      Notify about value changed:

                  let obj = Object.defineProperty(o, "myValue",
                  {
                      enumerable: true,
                      configurable: true,
                      writable: true,
                      value: "This is a test"
                  })
                  o.dummyChanged();
      

      Not sure if it is a good idea. but it works. Personally, I prefer to add one var property to a Rectangle and extend it as simple JS object. But in this case you should also notify that this property changed:

          Column
          {
              spacing: 3
              Label { text: rectA.props.myValue || "value not found" }
              Label { text: rectB.props.myValue || "value not found" }
          }
      
          Rectangle { id: rectA; property var props: ({}) }
          Rectangle { id: rectB; property var props: ({}) }
      
          property var objects: [rectA, rectB]
      
          Component.onCompleted:
          {
              objects.forEach( (o) =>
              {
                  o.props["myValue"] = "This is a test";
                  o.propsChanged();
              })
          }
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        Moisi
        wrote on last edited by
        #3

        Oh, well, it's make sens. Thank you for your answer and your solution. =D

        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