You should only use a Q_INVOKABLE method for specific functionalities that you want to be able to call from QML. Unless you somehow want to access model data from without access to the delegate of your model, you should always use the more proper model<->delegate way of getting the data.
Since you're inheriting from QAbstractListModel, it'll work like QAbstractItemModel.
Declare roles as shown:
enum Roles {
RoleA = Qt::UserRole + 1,
RoleB = Qt::UserRole + 2
};
Override this method to allow QML access using roles:
QHash<int, QByteArray> RecordModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[RoleA] = _Ut("roleA");
roles[RoleB] = _Ut("roleB");
return roles;
}
Override this method to return data when QML tries to access:
QVariant RecordModel::data(const QModelIndex &modelIndex, int role) const
{
QVariant rv;
int index = modelIndex.row();
switch( role ) {
case RoleA:
rv = "A";
break;
case RoleB:
rv = "B";
break;
default:
DASSERT(FALSE); // Unexpected role.
break;
}
return rv;
}
Then in QML you'll simply use "roleA" and "roleB" in the delegates of the QML Element that uses this model to access the data.
References:
http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html
http://qt-project.org/doc/qt-5.0/qtcore/qabstractlistmodel.html