Type Conversions
Slint supports conversions between different types. Explicit conversions are required to make the UI description more robust, but implicit conversions are allowed between some types for convenience.
The following conversions are possible:
int
can be converted implicitly tofloat
and vice-versa. When converting fromfloat
toint
, the value is truncated.int
andfloat
can be converted implicitly tostring
physical-length
,relative-font-size
, andlength
can be converted implicitly to each other only in context where the pixel ratio is known.- the units type (
length
,physical-length
,duration
, …) can’t be converted to numbers (float
orint
) but they can be divided by themselves to result in a number. Similarly, a number can be multiplied by one of these unit. The idea is that one would multiply by1px
or divide by1px
to do such conversions - The literal
0
can be converted to any of these types that have associated unit. - Struct types convert with another struct type if they have the same property names and their types can be converted. The source struct can have either missing properties, or extra properties. But not both.
- Arrays generally don’t convert between each other. Array literals can be converted if the element types are convertible.
- String can be converted to float by using the
to-float
function. That function returns 0 if the string isn’t a valid number. You can check withis-float()
if the string contains a valid number