Hi,

On 01/06/2012, at 6:33 AM, "Zellner Johannes (Nokia-DXM/SiliconValley)" 
<[email protected]> wrote:

> 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 ;-)

QML does not support custom property setters and never will.  Although there 
are a handful of places where this is violated, the rule is that the value read 
from a property should always be equal to the value written to it and custom 
setters - particularly of the form you want - defeat this.

The correct way to achieve this is what a previous poster suggested - use a 
function to change the value.  This is not a workaround, it is just "the way".  
In Qt5 you can also use the readonly modifier on the property itself to ensure 
that the function is the *only* way to modify the value and thus prevent misuse 
of your API.

Cheers,

Aaron

> 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
_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

Reply via email to