Custom C++ ListModel needing explicit role names to be accessible from QML?
in the doc (for QML), we can read that:
bq. Models that do not have named roles (such as the ListModel shown below) will have the data provided via the modelData role. The modelData role is also provided for models that have only one role. In this case the modelData role contains the same data as the named role.
Also, according to the documentation, I should at least reimplement those 3 methods:
@ int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index,
int role) const;
QVariant headerData(int section,
int role = Qt::DisplayRole) const;@
What I did. When I register my custom model type into QML and try to use it, I cannot access modelData property. I get:
@Unable to assign [undefined] to QString@
However, reimplementing roleNames() as:
@QHash<int, QByteArray> myClass::roleNames() const
QHash<int, QByteArray> roles;
roles = "nodeName";
Then it works and I can access my model data either as "nodeName" or "modelData". Could someone clear this confusion for me please?