How do you check if a property is undefined in qml?
This is what I am trying to do:
Button {
id: myButton
text: if (text === "undefined"){"default text"}
}
How do you check if a property is undefined in qml?
This is what I am trying to do:
Button {
id: myButton
text: if (text === "undefined"){"default text"}
}
Try:
text: text ? text : "default text"
"undefined"
is just a string representation of a reference not referencing anything, just likeNone
, orNULL
in other languages.===
is strict comparison operator, you might want to read this thread: https://stackoverflow.com/questions/523643/difference-between-and-in-javascriptThis answer throws a warning for me.
Changing
text
tomodelText
instead throws an error.This stops the Javascript execution for me; i.e. the next line isn't called.
Via Javascript
The same happens when setting it via Javascript, but is quite verbose.
Using
typeof
The
typeof
operator mutes the error and works as expected.To compare with undefined you write
text === undefined
. This will evaluate to false iftext
isnull
.If you want check if value is present (i.e., check for both
undefined
andnull
), use it as condition in if statement or ternary operator. If you need to store result of comparison as a boolean value, usevar textPresent = !!text
(though double!
might appear confusing to one reading the code).