QQmlPropertyMap how to notify QML of updates
-
@SPlatten said in QQmlPropertyMap how to notify QML of updates:
QQmlPropertyMap
QQmlPropertyMap does the binding.
The binding is dynamic - whenever a key's value is updated, anything bound to that key will be updated as well.To detect value changes made in the UI layer you can connect to the valueChanged() signal. However, note that valueChanged() is NOT emitted when changes are made by calling insert() or clear() - it is only emitted when a value is updated from QML.
-
@SPlatten said in QQmlPropertyMap how to notify QML of updates:
QQmlPropertyMap
QQmlPropertyMap does the binding.
The binding is dynamic - whenever a key's value is updated, anything bound to that key will be updated as well.To detect value changes made in the UI layer you can connect to the valueChanged() signal. However, note that valueChanged() is NOT emitted when changes are made by calling insert() or clear() - it is only emitted when a value is updated from QML.
-
@SPlatten https://doc.qt.io/qt-5.15/qqmlpropertymap.html
connect the signal
void valueChanged(const QString &key, const QVariant &value)
in your qml and then check if the key is equal to your variable name. If yes, you know it is changed. No timestamp is needed. -
@SPlatten https://doc.qt.io/qt-5.15/qqmlpropertymap.html
connect the signal
void valueChanged(const QString &key, const QVariant &value)
in your qml and then check if the key is equal to your variable name. If yes, you know it is changed. No timestamp is needed. -
@JoeCFD , the timestamp is needed so I can work out how old since the last update, if the time elapsed is greater than a specified period then the data is stale.
-
@JoeCFD The documentation for valueChanged says: Note: valueChanged() is NOT emitted when changes are made by calling insert() or clear() - it is only emitted when a value is updated from QML.
-
@SPlatten Do not calling insert() mean binding or clear() mean release binding? It makes sense that the value is not changed.
-
@SPlatten Read the example:
https://doc.qt.io/qt-5.15/qqmlpropertymap.htmlQQmlPropertyMap ownerData; ownerData.insert("name", QVariant(QString("John Smith"))); ==> will not trigger valueChanged, sort of like initialization ownerData.insert("phone", QVariant(QString("555-5555")));
write a small program to test it.
-
@SPlatten Read the example:
https://doc.qt.io/qt-5.15/qqmlpropertymap.htmlQQmlPropertyMap ownerData; ownerData.insert("name", QVariant(QString("John Smith"))); ==> will not trigger valueChanged, sort of like initialization ownerData.insert("phone", QVariant(QString("555-5555")));
write a small program to test it.
-
@JoeCFD , is it only the initial insert of an item that does not trigger the valueChanged signal ?
I will try this out tomorrow, thank you.
-
@SPlatten I haven't followed this whole thread so apologies if I have missed something, but but regarding your most recent question, could you set your
QQmlPropertyMap
as a context property in your C++ back end? You would then be able in QML to use the name that you registered the context property as. -
Might be able to try:
Item{
Property var myData: backendQqmlPropertyMap.name
OnMyDataChanged{
// Doathing
}
}Sorry for formatting, on a phone currently!