Qt QML data model doesn't seem to work with C++

妖精的绣舞 提交于 2019-12-03 20:51:47

It turns out that there's a very simple reason that the count property of the cppModel isn't available - it's because neither QAbstractListModel nor QList<> have a count property!

I had assumed that the fact that a ListModel could be substituted with a C++-based object like a QList<> meant that they were polymorphic and that a ListView or PathView would use a count property to correctly handle them.

First, neither QAbstractListModel nor QList<> are polymorphic with a ListModel. It turns out that they're all just special-cased - a ListView knows whether it has a ListModel or a QList<> or a QAbstractListModel and has separate code paths for using each. The ListView doesn't need the nonexistent count property to manage a QList<> or a QAbstractListModel. In fact, it isn't clear to me that ListView and PathView even use ListModel's count property. The count property seems to be mostly for the QML programmer's benefit. In my example, I was using the count property to build a Path object in the PathView. My example works perfectly if I use a length property instead because QList<> DOES have a length property.

Thanks to blam and torgeirl on #qt-qml for helping me with this (neither wanted to collect the stackoverflow points by posting this answer, so I'm posting it for the benefit of the community).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!