[Qt-qml] custom setter and getter
Johannes Zellner
johannes.zellner at nokia.com
Fri Jun 1 23:32:40 CEST 2012
Hi Aaron,
On Friday, June 01, 2012 02:09:14 AM Kennedy Aaron wrote:
> Hi,
>
> On 01/06/2012, at 6:33 AM, "Zellner Johannes (Nokia-DXM/SiliconValley)"
<johannes.zellner at nokia.com> 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.
>From a declarative way of looking at it, I would still consider it a
workaround to be forced to call a function instead of "property = value"
But I have another usecase, where the set function would not work:
Item {
property bool bar: false
readonly property bool foo: bar // whatever the syntax for readonlyis ;-)
function setFoo(bool b) {
if (foo == b || !myCondition)
return;
foo = b;
}
}
(The setFoo would override the binding on the first call, but maybe thats not
the point)
So whenever 'bar' changes it would apply to foo as well, without going through
my setFoo() function, which is not what I want.
Custom setter with conditions are imho quite common in Qt c++, so the
convention from QML side is useless, as people will break it, thus QML needs
to be able to deal with it anyway.
Maybe my example looks a bit too made up, but it was on of the first things
came to my mind after reading your mail.
Cheers,
Johannes
>
> Cheers,
>
> Aaron
>
> > Hopefully I can find some of them at the QCS ;-)
> >
> > Thanks,
> > Johannes
> > ________________________________________
> > From: qt-qml-bounces+johannes.zellner=nokia.com at qt.nokia.com
> > [qt-qml-bounces+johannes.zellner=nokia.com at qt.nokia.com] on behalf of ext
> > Harri Pasanen [harri at mpaja.com] Sent: Thursday, May 31, 2012 22:27
> > To: qt-qml at qt.nokia.com
> > 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
> >> Qt-qml at qt.nokia.com
> >> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
> >
> > _______________________________________________
> > Qt-qml mailing list
> > Qt-qml at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt-qml
> > _______________________________________________
> > Qt-qml mailing list
> > Qt-qml at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt-qml
More information about the Qt-qml
mailing list