[Qt-components] Validation of properties in the proposed Calendar API

Alan Alpert 416365416c at gmail.com
Wed Jan 16 17:14:25 CET 2013


On Wed, Jan 16, 2013 at 5:07 AM, Michael Hasselmann
<michaelh at openismus.com> wrote:
> On Wed, 2013-01-16 at 14:01 +0100, Mitch Curtis wrote:
>> Hi.
>>
>> I'm currently working on the Calendar component [1]. I'd like to consult any
>> interested parties regarding validation of its properties.
>>
>> The proposed API provides selectedDate, minimumDate and maximumDate
>> properties. The selectedDate property reflects the date currently displayed by
>> the calendar, and can be set either programmatically or through the Calendar
>> UI itself. This property should, in my opinion, be validated against dates
>> outside the range of minimumDate and maximumDate. I'm not so sure about
>> validation against "undefined", but have included that in my examples for
>> completeness.
>>
>> Since I can't validate the selectedDate property in its own onChanged handler
>> (results in binding loops), I have to take another approach. From talking with
>> a few developers, I can see two options:
>>
>> a) Have two properties (See [2]):
>>     - inputDate - Its only purpose is to be set programmatically.
>>     - selectedDate - Read-only. The result of inputDate after validation or
>> the date chosen through the UI.
>
> Why not have a separate validator component listening to the
> selectedDate property? If the validator doesn't like the current value,
> it will reset it to a valid one (or last valid one).
>
> So yes, others listening to that property will temporarily see a wrong
> value, if someone sets an incorrect date. But "temporary" should be in
> the ms range.

You can't be sure that it will go away quickly. If there are complex
bindings set on that property the extra changes would trigger
revaluations, which could keep that wrong value alive until the next
frame.

It also leads to a worse API. If temporary wrong values are exposed
then the property can't be treated as always valid by users. Anyone
using this API has to perform their own validation as well or things
could go wrong in their application.

Note that the ideal is to have a more intelligent setter, like you
would when implementing the property from C++. But there is no
facility for doing that from QML right now. If you can make the root
item of the Caldendar component a custom C++ object, that would solve
the problem.

--
Alan Alpert



More information about the Qt-components mailing list