hah I got it to work!
The problem was one of the functions of util.cpp was not working as intended which is used to get role names. some posts before, i mentioned overriding QAbstractListModel::setRoleNames is depracted as of Qt5.. so overriding QAbstractListModel::roleNames() is the new way to go..
it all happens in the constructor of GenericModel
util.cpp - not in use anymore
@
void extractObjectProperties(const QMetaObject *object,
QStringList *list,
bool cleanup,
const char *prefix)
{
QStringList &properties = *list;
const int count = object->propertyCount();
for (int i = 0; i < count; ++i) {
QString propertyName = object->property(i).name();
if (propertyName.startsWith(prefix)) {
properties << propertyName;
}
}
if (cleanup) {
properties.replaceInStrings(prefix, "");
}
}
@
genericmodel.cpp - before, little bit modified from the example
@
template <class ModelTemplate>
GenericModel<ModelTemplate>::GenericModel(QObject *parent, bool cleanupPrefix)
: GenericModelBase(parent), m_cleanup(cleanupPrefix), m_propertyCount(0)
{
//const ModelTemplate object;
QStringList properties;
ModelTemplate tmp;
Utils::extractObjectProperties(tmp.metaObject(), &properties, m_cleanup);
m_propertyCount = properties.count();
}
...
template <class ModelTemplate>
QHash<int, QByteArray> GenericModel<ModelTemplate>::roleNames() const{
QHash<int, QByteArray> roles;
QStringList properties;
ModelTemplate tmp;
Utils::extractObjectProperties(tmp.metaObject(), &properties, m_cleanup);
for (int i = 0; i < properties.count(); ++i) {
roles[i] = properties[i].toUtf8();
//qDebug() << roles[i] << "hello";
}
return roles;
}
@
it's rather self explanatory, all the Q_PROPERTIES i defined at project.h get collected and should be returned by that function, so qml knows about the properties properly using the QAbstractListModel::roleNames()
so the huge key thing here was understanding that:
roles (cpp) = properties (qml).
or it wont work.
the function provided in the example had some sort of filter for whatever reason, i just copy/cut/pasted my version without that filter and voila, it worked. here is the proper function for the genericmodel class which got it to work for me :)
genericmodel.cpp - after
@
template <class ModelTemplate>
GenericModel<ModelTemplate>::GenericModel(QObject *parent, bool cleanupPrefix)
: GenericModelBase(parent), m_cleanup(cleanupPrefix), m_propertyCount(0)
{
ModelTemplate tmp;
m_propertyCount = tmp.metaObject()->propertyCount();
for (int i = 0; i < m_propertyCount; ++i) {
QString propertyName = tmp.metaObject()->property(i).name();
m_roles[i] = propertyName.toUtf8();
}
}
...
template <class ModelTemplate>
QHash<int, QByteArray> GenericModel<ModelTemplate>::roleNames() const{
return m_roles;
}
@
as you can see i have added also a member variable called QHash<int, > m_roles for storing.
oh btw, dont be confused about the product / project thing..
if someone in the future has a similar problem i'm glad to provide further code/explanations so dont hesitate to grave dig it up or PM me.
thanks for your help gennon