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 Updated to NodeBB v4.3 + New Features

Setter function for internal qml object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 253 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.
  • M Offline
    M Offline
    Moisi
    wrote on 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
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on 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 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

        • Login

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