[Interest] QAbstractItemModel and Qml - modelData is null

Nuno Santos nunosantos at imaginando.pt
Tue Oct 17 11:13:40 CEST 2017


Pierre,

Thanks for the tip. It worked. I got that from an example online, but not from official documentation.

IPatch extends QObject so it is basically a QObject. I have changed the cast to QObject and it is working now!

Thanks!

Best regards,

Nuno

> On 16 Oct 2017, at 22:16, Pierre-Yves Siret <py.siret at gmail.com> wrote:
> 
> 
> 2017-10-16 20:11 GMT+02:00 Nuno Santos <nunosantos at imaginando.pt <mailto:nunosantos at imaginando.pt>>:
> 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 <http://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
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org <mailto:Interest at qt-project.org>
> http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
> 
> 
> I'm gonna go out on a limb and say that the QML engine can't deal with void* type.
> What type is patch? Put it in the variant as a QObject* instead if you can.
> 
> Regards
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

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


More information about the Interest mailing list