Open xzz53 opened 2 years ago
Thanks for sharing your code; it helped me progress on a similar enum vs. int related typing issue while trying to set an enum property. When I try to set my property with "my_property = MyEnum.MyVariant" in a callback, I hit "
You ~did declare "property int testprop"... did you already try "property var testprop" or "property backend.OpMode"?
I believe this has to do with enums in C not being types; they're "tags" and have type int. So the type of the enum is conflated with the type of the variant, and enums aren't really first class types. I am still looking for a workaround, but the answer might just be to give up on enums in qt and resort to stringly or intly typed code.
I changed my property that was previously my enum to be an i32:
stateMachineQt: qt_property!(i32; READ getState WRITE setState NOTIFY state_changed),
Then I implemented TryFrom for it following:
https://stackoverflow.com/questions/28028854/how-do-i-match-enum-values-with-an-integer
And I use that to validate the int in the setter with something like:
fn setState(&mut self, new_state: i32) {
QtStateMachineStateEnum::try_from(new_state).unwrap();
self.stateMachineQt = new_state;
}
This alone wouldn't provide much type safety, since I could still use the wrong enum in the QML, but I mitigated it using large random numbers for my enum variants so they ~probably won't collide with the variants of any other enums:
#[derive(Copy, Clone, Debug, Eq, PartialEq, QEnum)]
#[repr(C)]
enum QtStateMachineStateEnum {
Disabled = 13739,
Enabled = 23639,
}
Hi, I'm a new
qmetaobject-rs
user, and I'm trying to use QEnum-deriving enum as a property type. My code compiles, but at runtime qml sees the property typeObject
instead ofint
. Any hints are welcome. The minimal (broken) example and output are below.