[Interest] Re: Reporting errors from QML objects' property setters

Jan Kundrát jkt at flaska.net
Wed Dec 5 10:35:41 CET 2012


On Monday, 3 December 2012 11:10:08 CEST, Jan Kundrát wrote:
> QML performs some sanity checks for me; for example, when 
> assigning incompatible types to a property (say, a QString into 
> a property with type of int), I get a nice error message 
> including the location in my QML file.
>
> Can I do the same for custom checks which are implemented in my 
> own setters? A trivial example is "how do I only allow 
> all-uppercase strings in my QString property"?

Hi, the best solution I was able to come up with involved calling qmlInfo(this) << "foo bar"; these errors indeed show up in the console output by default and are reported by QDeclarativeEngine's warnings signal.

Because I'm actually inheriting from QDeclarativeParserStatus, too, I was hoping for a solution where I could somehow prevent the component from being created in the first place, and I tried to do the checks from inside the componentComplete() virtual method. It looks like I cannot do that; I can report the error, but it's too late to refuse the instantiation. It looks that there's no way to e.g. enforce that a component can only be created *iff* the QML code sets a certain property. So no way to really enforce compulsory properties without explicit state checking from the C++ code (like an isOk() method).

Cheers,
Jan



More information about the Interest mailing list