Unable to change bools in C++ struct
-
@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?
-
@mzimmers what's the issue on this output? We don't know which field is which. Also why are you using m_maintenance in QML? That's not the name of your property.
-
OK, guys - here's a minimal repeatable example:
// schedule.h #pragma once #include <QObject> #include <QMetaType> #include <QtQml/qqmlregistration.h> #include <QtQmlIntegration/QtQmlIntegration> struct Schedule { Q_GADGET QML_VALUE_TYPE(schedule) QML_STRUCTURED_VALUE Q_PROPERTY(bool maintenance MEMBER m_maintenance) public: bool m_maintenance; Q_INVOKABLE Schedule() {} bool operator ==(const Schedule &rhs) const {return true;} };
// Main.qml import QtQuick import QtQuick.Controls import QtQuick.Layouts import QtQuick.Window Window { id: mainWindow width: 640 height: 480 visible: true property schedule newSchedule: ({ }) Switch { onClicked: { console.log("switch is " + checked) newSchedule.maintenance = checked console.log("newSchedule.maintenance is " + newSchedule.maintenance) } } }
Output:
qml: switch is true qml: newSchedule.maintenance is false qml: switch is false qml: newSchedule.maintenance is false qml: switch is true qml: newSchedule.maintenance is false qml: switch is false qml: newSchedule.maintenance is false
I can provide the cmake file as well if desired, but it doesn't contain anything unusual.
-
OK, guys - here's a minimal repeatable example:
// schedule.h #pragma once #include <QObject> #include <QMetaType> #include <QtQml/qqmlregistration.h> #include <QtQmlIntegration/QtQmlIntegration> struct Schedule { Q_GADGET QML_VALUE_TYPE(schedule) QML_STRUCTURED_VALUE Q_PROPERTY(bool maintenance MEMBER m_maintenance) public: bool m_maintenance; Q_INVOKABLE Schedule() {} bool operator ==(const Schedule &rhs) const {return true;} };
// Main.qml import QtQuick import QtQuick.Controls import QtQuick.Layouts import QtQuick.Window Window { id: mainWindow width: 640 height: 480 visible: true property schedule newSchedule: ({ }) Switch { onClicked: { console.log("switch is " + checked) newSchedule.maintenance = checked console.log("newSchedule.maintenance is " + newSchedule.maintenance) } } }
Output:
qml: switch is true qml: newSchedule.maintenance is false qml: switch is false qml: newSchedule.maintenance is false qml: switch is true qml: newSchedule.maintenance is false qml: switch is false qml: newSchedule.maintenance is false
I can provide the cmake file as well if desired, but it doesn't contain anything unusual.
@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.
-
@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.
@GrecKo bingo!
I'd just stubbed out that comparison function to satisfy the compiler, but you were absolutely right. In my app, I'd neglected to include the maintenance member in my comparison function. Seems to work fine now.
Kind of has me wondering -- if I set my compiler options to no optimization, I wonder if it would have worked...
Anyway, thanks to EVERYONE for the help on this.
-
M mzimmers has marked this topic as solved on