
Originally Posted by
wysota
Bind the object's property to a function call that will return the data or invoke some imperative code when contents of the model change.
This is fine, I have the QML object's property bound to the a function (I can see it hitting the breakpoint where I return a value) however, this only gets called once while the QML object is loading. From that point on, it never updates despite the data changing (this the issue QQmlPropertyMap and the likes resolved). I guess the question then boils down to, how can I update the QML object when I know data that it references has been updated?
Edit: I've had a bit of a play with this and the only solution I can come up with is binding the QML object's value a C++ object's property and provide a NOTIFY signal
C++:
{
Q_OBJECT
Q_PROPERTY(RuntimeData* data READ GetData NOTIFY modelChanged)
public:
C++:
class Runtime : public QObject
{
Q_OBJECT
Q_PROPERTY(RuntimeData* data READ GetData NOTIFY modelChanged)
public:
To copy to clipboard, switch view to plain text mode
The RuntimeData object is my QAbstractListModel derived object. In this object I've then created a Q_INVOKABLE member that retuns the data value base on an index (for now).
{
Q_OBJECT
public:
Q_INVOKABLE int GetData(int index)
{
if (index >= 0 && index < m_Data.count())
return m_Data[index].Value().toInt();
return 0;
}
class RuntimeData : public QAbstractListModel
{
Q_OBJECT
public:
Q_INVOKABLE int GetData(int index)
{
if (index >= 0 && index < m_Data.count())
return m_Data[index].Value().toInt();
return 0;
}
To copy to clipboard, switch view to plain text mode
Finally, its all working but there is a remaining issue. With this implementaion, anytime I have to update any data, I also have to emit the modelChanged signal to inform the QML to update. Hypothetically if I have 1000 data entries and 1000 QML objects bound to these, if one data item changes all QML objects will update.
Again, I feel like I must be missing something? Why is this so complicated?
Bookmarks