Unable to change bools in C++ struct
-
Hi all -
Just ran across weird behavior -- my attempts to set boolean members of a C++ struct don't work in my app.
struct Schedule { Q_GADGET QML_VALUE_TYPE(schedule) QML_STRUCTURED_VALUE Q_PROPERTY(QString name MEMBER m_name) Q_PROPERTY(bool maintenance MEMBER m_maintenance) public: QString m_name = QString(); bool m_maintenance = false; ...
and
ColumnLayout { id: scheduleScreen property schedule newSchedule: ({}) Component.onCompleted: { newSchedule.maintenance = true console.log("ScheduleScreen.qml: newSchedule.maintenance is " + newSchedule.maintenance) } ...
produces this output:
qml: ScheduleScreen.qml: newSchedule.maintenance is false
Same behavior with another bool value in my struct, but changing other values (such as name) works.
I also tried reducing this to a minimal example, but that worked (ugh).
Any ideas where I went wrong with this? Qt 6.5.3, Windows.
Thanks...
-
@mzimmers said in Unable to change bools in C++ struct:
bool operator ==(const Schedule &rhs) const {return true;}
If you remove this non-sensical
operator==
your code works as expected.My guess is that the engine modifies a temp Schedule internally, checks the equality and assign it only if it's different. As it's always equal it won't modify it.
-
@JoeCFD said in Unable to change bools in C++ struct:
newSchedule.m_maintenance = true
Doesn't change anything. Plus I'm trying to use my defined properties for access to the struct.
I added a few more property modifications, and they all seem to work except for the bool:
Component.onCompleted: { console.log("ScheduleScreen.qml: newSchedule is " + newSchedule) newSchedule.name = "xxx" newSchedule.m_maintenance = true newSchedule.timeOffset = 55 newSchedule.timeFrame = ScheduleEnums.TIMEFRAME_DAILY; console.log("ScheduleScreen.qml: newSchedule is " + newSchedule) }
produces this:
qml: ScheduleScreen.qml: newSchedule is Schedule({00000000-0000-0000-0000-000000000000}, , {00000000-0000-0000-0000-000000000000}, false, false, , START_ACTION_TURN_ON, END_ACTION_TURN_OFF, START_WHEN_TIME_OF_DAY, END_WHEN_TIME_OF_DAY, 09:00:00.000, 17:00:00.000, TIMEFRAME_WEEKLY, REPEAT_EVERY_WEEK, , , , 0, ) qml: ScheduleScreen.qml: newSchedule is Schedule({00000000-0000-0000-0000-000000000000}, xxx, {00000000-0000-0000-0000-000000000000}, false, false, , START_ACTION_TURN_ON, END_ACTION_TURN_OFF, START_WHEN_TIME_OF_DAY, END_WHEN_TIME_OF_DAY, 09:00:00.000, 17:00:00.000, TIMEFRAME_DAILY, REPEAT_EVERY_WEEK, , , , 55, )
-
@mzimmers said in Unable to change bools in C++ struct:
I also tried reducing this to a minimal example, but that worked (ugh).
This really means you need to start from such a good situation, or from a bad situation, and work towards the other. Till you can produce a "this works, and with this tiny change that does not work".
-
@GrecKo this:
Component.onCompleted: { var tempSchedule = newSchedule console.log("ScheduleScreen.qml: newSchedule is " + newSchedule) tempSchedule.name = "xxx" tempSchedule.m_maintenance = true tempSchedule.timeOffset = 55 tempSchedule.timeFrame = ScheduleEnums.TIMEFRAME_DAILY; newSchedule = tempSchedule console.log("ScheduleScreen.qml: tempSchedule is " + tempSchedule) console.log("ScheduleScreen.qml: newSchedule is " + newSchedule) }
Produces this:
qml: ScheduleScreen.qml: newSchedule is Schedule({00000000-0000-0000-0000-000000000000}, , {00000000-0000-0000-0000-000000000000}, false, false, , START_ACTION_TURN_ON, END_ACTION_TURN_OFF, START_WHEN_TIME_OF_DAY, END_WHEN_TIME_OF_DAY, 09:00:00.000, 17:00:00.000, TIMEFRAME_WEEKLY, REPEAT_EVERY_WEEK, , , , 0, ) qml: ScheduleScreen.qml: tempSchedule is Schedule({00000000-0000-0000-0000-000000000000}, xxx, {00000000-0000-0000-0000-000000000000}, false, false, , START_ACTION_TURN_ON, END_ACTION_TURN_OFF, START_WHEN_TIME_OF_DAY, END_WHEN_TIME_OF_DAY, 09:00:00.000, 17:00:00.000, TIMEFRAME_DAILY, REPEAT_EVERY_WEEK, , , , 55, ) qml: ScheduleScreen.qml: newSchedule is Schedule({00000000-0000-0000-0000-000000000000}, xxx, {00000000-0000-0000-0000-000000000000}, false, false, , START_ACTION_TURN_ON, END_ACTION_TURN_OFF, START_WHEN_TIME_OF_DAY, END_WHEN_TIME_OF_DAY, 09:00:00.000, 17:00:00.000, TIMEFRAME_DAILY, REPEAT_EVERY_WEEK, , , , 55, )
So...no change. Really odd, isn't it?
-
OK, so I discovered something, but I don't understand it. I stepped through this in the debugger, and confirmed the problem is in the C++ side:
Then, for lack of anything else to try, I removed the initializer in my struct:
// bool m_maintenance = false; // before bool m_maintenance; // now
and...
For another test, I added this line to my c'tor:bool m_maintenance = false;
And got the original (bad) result.
I also "changed polarity" (initialize to true; try to set to false) with the same (bad) result.
So...this problem just got a little bigger. I'm not willing to release code to the field that creates structs with uninitialized values; I need to figure out what's going on here.
EDIT: this occurs on 6.6 as well as 6.5.3.
ANY ideas, no matter how far-fetched, are welcome. Thanks...
-
@JoeCFD interesting article, though my issue is with modification, not initialization.
Regarding the use of structs: I suppose I could use a class, but I'd be astonished if that made a difference. I'm deliberately avoiding using QObject here, too.
-
@JoeCFD said in Unable to change bools in C++ struct:
@mzimmers bool m_maintenance; // now
is undefined.I realize that, and that's not acceptable. But currently I have the options of leaving it undefined, or initializing it and (somehow) making it QML-immutable in the process.
I can't even modify it if I initialize it in QML, like this:
property schedule newSchedule: ({ "maintenance": false })