[SOLVED] ListView and dynamically creatable delegate
-
Hi!
How to create a delegate to the item only when it is really needed?
My current solution: delegate: OwnDelegate {}
This solution doesn't work. The problem is that this solution causes calling of fetchMore() function of the model. In other words it tries to build a delegate to every item of the model when initializing ListView. There can be over 30 000 items in the model.
More information about the datastructure of the model in this "thread":http://qt-project.org/forums/viewthread/15275/
-
I would do this in C++ with a class inheriting from QAbstractListModel. See http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#qabstractitemmodel for an example.
-
Hi,
See ListView's property "cacheBuffer":http://doc.qt.nokia.com/4.7-snapshot/qml-listview.html#cacheBuffer-prop . You got delegate instances only for visible items in the list.
-
My model is inherited from QAbstractListModel.
I tried cacheBuffer but this doesn't solve the problem. Despite of cacheBuffer fetchMore() function is still called. It seems that ListView wants fetch all the items before ListView can be shown.
I don't want that fetchMore() function is called before it is really needed.
-
Which function calls fetchMore() ? Is it something from the model or something from ListView?
-
QAbstractItemView calls it
QAbstractItemModel documentation says:
"If canFetchMore() returns true, QAbstractItemView will call fetchMore(). However, the fetchMore() function is only called when the model is being populated incrementally." -
Problem solved
It is embarrasing to admit that it was all my mistake.
The problem was that I hadn't defined height to the delegate. That's why unlimited number of items could fit to the listview.
I certainly have learned my lesson.