Sorry for top-posting. Thanks for your input, I am aware of several workarounds but all of them are simply just workarounds. The property should basically look from the outside as any other property. Having JS function calls is certainly the best way to go without the ability of custom setters, at least that is my impression, thats why I would like to hear some input from the QML guys on this topic ;-)
Hopefully I can find some of them at the QCS ;-) Thanks, Johannes ________________________________________ From: [email protected] [[email protected]] on behalf of ext Harri Pasanen [[email protected]] Sent: Thursday, May 31, 2012 22:27 To: [email protected] Subject: Re: [Qt-qml] custom setter and getter Personally I'm not too fond of "hidden" magic in setters/getters. So I'd just make it explicit, something like: Foo { id: foo property bool checked: false property bool _someCondition: (a< b) function setChecked(onOff) { if (_someCondition) foo.checked = onOff } } MouseArea { onClicked: foo.setChecked(!foo.checked) } On 05/31/2012 07:21 PM, Johannes Zellner wrote: > Hi, > > maybe the question was already raised and I just missed that. I did not find > anything so far. > > Is there a way in QML to create custom setters or getters to properties? > > The issue I am facing is a, to be able to control the setting of a custom > property, based on the component internal state. > The snippet is not complete nor working, but should show the issue: > > Foo { > id: foo > property bool checked: false > property bool _someCondition: (a< b) > } > > Rectangle { > color: foo.checked ? "green" : "red" > } > > MouseArea { > onClicked: foo.checked = !foo.checked > } > > So clicking on the MouseArea toggles the foo.checked, but the actual > foo.checked should be only changed if foo._someCondition is true. So the > Rectangle's color binding should only be evaluated in case the foo.checked > really changes. Resetting the property after doing some onCheckedChanged is > not what I want. > I only found a clean solution with creating a custom setter here, which is of > course possible when creating a custom c++ class for Foo{}. > > Would be nice to avoid this, any ideas? Maybe there is a plain simple solution > and my QML knowledge is just too limited :-) > > Thanks, > Johannes > _______________________________________________ > Qt-qml mailing list > [email protected] > http://lists.qt.nokia.com/mailman/listinfo/qt-qml _______________________________________________ Qt-qml mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt-qml _______________________________________________ Qt-qml mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt-qml
