[Interest] QAbstractItemModel and Qml - modelData is null

Nuno Santos nunosantos at imaginando.pt
Mon Oct 16 20:11:26 CEST 2017


Hi,

I have for the first time defined a QAbstractItemModel to use on Qml because I needed to have a filtered version of this model using QSortFilterProxyModel.

I have the documentation here -> http://doc.qt.io/archives/qt-5.5/qtquick-modelviewsdata-cppmodels.html <http://doc.qt.io/archives/qt-5.5/qtquick-modelviewsdata-cppmodels.html> and followed the example given. I have defined the following roles:

enum PatchRoles {
    IdRole = Qt::UserRole + 1,
    NameRole,
    ModifiedRole,
    TagsRole,
    ModelDataRole
};

And then under data retrieval method:

QVariant IPatchManagerModel::data(const QModelIndex & index, int role) const
{
    if (index.row() < 0 || index.row() >= _patches.count())
        return QVariant();

    IPatch* patch = _patches.at(index.row());

    if (role == IdRole)
        return patch->id();
    else if (role == NameRole)
        return patch->nameLabel();
    else if (role == ModifiedRole)
        return patch->modified();
    else if (role == TagsRole)
        return patch->tags();
    else if (role == ModelDataRole)
        return qVariantFromValue((void *) patch);

    return QVariant();
}

Every role works except modelData, whenever I try to access it it gives me null on the Qml side.

Does anyone knows what am I doing wrong?

Thanks,

Regards,

Nuno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171016/d45ce010/attachment.html>


More information about the Interest mailing list