Nils
Nils

Reputation: 13777

QML / C++ Optional Properties

How can I declare a property that only exists at some point in time?

My first attempt was to implement a method getThingy() that returns a pointer to thingy if it exists and null otherwise. I would have a signal that notifies when the property changes.

Q_PROPERTY(Thingy* thingy READ thingy NOTIFY thingyChanged)

However when I access this in QML the QML run time keeps complaining:

TypeError: Cannot read property 'thingy' of null

If I want to have an optional property whats the QML way to declare it?

Upvotes: 0

Views: 1353

Answers (1)

GrecKo
GrecKo

Reputation: 7170

You can do it like that, but as you can see you can't access a property from a null value. You have to check if it's non null first.

This can be done the same as in javascript:

  • thingy ? thingy.property : defaultValue
  • thingy && thingy.property // returns null if thingy is null
  • ...

Upvotes: 2

Related Questions