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. Setter function for internal qml object
Forum Update on Monday, May 27th 2025

Setter function for internal qml object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 253 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 28 Jun 2021, 14:52 last edited by Moisi
    #1

    Hi,

    I have a component (MyItem.qml) which is an Item with another Item with a value inside. Like this:

    Item {
    	readonly property Item wrapper: private.wrapper // this readonly property is important.
            function setValue( newValue ) { private.wrapper.value = newValue; }
    	
    	Item {
    		id: private
    		property Item wrapper: Item {
    			property int value: 42
    		}
    	}
    }
    

    Now imagine that I do not want to allow user to change the value property without using the setter function. For example:

    Item {
    	id: root
    	MyItem { id: myItem }
    	Column {
    		Button { text: "allowed"; onClicked: myItem.setValue(13) }
    		Button { text: "not allowed"; onClicked: myItem.wrapper.value = 13 } // This should not be allowed
    	}
    }
    

    Does someone have any trick or pattern to do this ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 28 Jun 2021, 19:13 last edited by
      #2

      Have you looked at property alias?

      MyItem.qml:

      Item {
          property alias text: subText.text
          Text { id: subtext }
      }
      

      TopLevel.qml:

      Item {
          MyItem { id: myItem }
          Button { onClicked: myItem.text = "clicked" }
      }
      

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Moisi
        wrote on 29 Jun 2021, 17:54 last edited by
        #3

        Thanks for your answer ^^

        I totally forgot the existance of alias ! Thank you for the remind ^^

        1 Reply Last reply
        0

        1/3

        28 Jun 2021, 14:52

        • Login

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