QML isn't updating nested C++ objects
Solved
QML and Qt Quick
-
Hi all -
I'm experimenting with nested C++ objects as follows:
class RepeatData { Q_GADGET public: Q_PROPERTY(QVector<bool> weekDaysSelected MEMBER m_weekDaysSelected) QVector<bool> m_weekDaysSelected; bool operator ==(const RepeatData &rhs) const; bool operator !=(const RepeatData &rhs) const { return !operator==(rhs); } friend struct Schedule; }; struct Schedule { Q_GADGET QML_VALUE_TYPE(schedule) QML_STRUCTURED_VALUE // to allow this Q_GADGET to be instantiated in QML. Q_PROPERTY(RepeatData repeatData MEMBER m_repeatData) public: RepeatData m_repeatData; }
My QML seems to read the inner value(s) OK, but when I try to modify them, they don't change (no error messages). Here's an example of what's not working:
onButtonClicked: (index) => { console.log("ScheduleRepeat.qml: weekDaysSelected is " + newSchedule.repeatData.weekDaysSelected) newSchedule.repeatData.weekDaysSelected[index] = !(newSchedule.repeatData.weekDaysSelected[index]) console.log("ScheduleRepeat.qml: weekday button " + index + " clicked.") console.log("ScheduleRepeat.qml: weekDaysSelected is " + newSchedule.repeatData.weekDaysSelected) }
and the output:
qml: ScheduleRepeat.qml: weekDaysSelected is true,true,true,true,true,true,true qml: ScheduleRepeat.qml: weekday button 2 clicked. qml: ScheduleRepeat.qml: weekDaysSelected is true,true,true,true,true,true,true
This QML logic was working before I introduced the internal struct.
Any idea what I'm doing wrong here?
Thanks...
-
found the problem - my inner == operator had a bug which was causing it to always return true.
In debugging this, I noticed that my == operator gets called a lot more times than I'd expect. I only have 2 of these objects, and am creating a 3rd, but I must have hit a breaking point inside my comparison function 20 or more times. Does this suggest a design flaw on my part?
Thanks...
-
M mzimmers has marked this topic as solved on