Discussion:
[Interest] Example for: QTreeView + QAbstractItemModel + external data
anton
2016-04-26 17:05:07 UTC
Permalink
Hi,

I have subclassed QAbstractItemModel,
and I want to load additionally data in the model
as soon as somebody clicks on the [+]
in the treeview to load the children of this
item form an external data ( web- app which gives me json data back).

Now:
I reimplemented hasChildren() in the model
but i am not sure about the moment to load the data.

I suppose that the rowCount() method of the model is called
when somebody expands an item in the view?
If not what is the moment when my model should ask for additional Data?

Does there exist a small example which demonstrates how to
handle this in an optimal way.
Not sure about beginInsertRows() or layoutChanged() ..
I am experimenting but i get also some crashes so an
example project would be nice.

Thanks

Anton
Björn Schäpers
2016-04-27 21:45:42 UTC
Permalink
Post by anton
Hi,
I have subclassed QAbstractItemModel,
and I want to load additionally data in the model
as soon as somebody clicks on the [+]
in the treeview to load the children of this
item form an external data ( web- app which gives me json data back).
I reimplemented hasChildren() in the model
but i am not sure about the moment to load the data.
I suppose that the rowCount() method of the model is called
when somebody expands an item in the view?
If not what is the moment when my model should ask for additional Data?
Does there exist a small example which demonstrates how to
handle this in an optimal way.
Not sure about beginInsertRows() or layoutChanged() ..
I am experimenting but i get also some crashes so an
example project would be nice.
Thanks
Anton
I think you are looking for QAbstractItemModel::canFetchMore() and fetchMore().
See also the last paragraph in http://doc.qt.io/qt-5/qabstractitemmodel.html

To create models that populate incrementally, you can reimplement fetchMore()
and canFetchMore(). If the reimplementation of fetchMore() adds rows to the
model, beginInsertRows() and endInsertRows() must be called.
anton
2016-04-30 08:08:28 UTC
Permalink
Thanks Björn,

I'll try it (actually i did the job in the rowCount() function..)
I would have preferred that the documentation be clear about this,
I mean with a detailed example :-)

Thanks

Anton
Post by Björn Schäpers
Post by anton
Hi,
I have subclassed QAbstractItemModel,
and I want to load additionally data in the model
as soon as somebody clicks on the [+]
in the treeview to load the children of this
item form an external data ( web- app which gives me json data back).
I reimplemented hasChildren() in the model
but i am not sure about the moment to load the data.
I suppose that the rowCount() method of the model is called
when somebody expands an item in the view?
If not what is the moment when my model should ask for additional Data?
Does there exist a small example which demonstrates how to
handle this in an optimal way.
Not sure about beginInsertRows() or layoutChanged() ..
I am experimenting but i get also some crashes so an
example project would be nice.
Thanks
Anton
I think you are looking for QAbstractItemModel::canFetchMore() and
fetchMore(). See also the last paragraph in
http://doc.qt.io/qt-5/qabstractitemmodel.html
To create models that populate incrementally, you can reimplement
fetchMore() and canFetchMore(). If the reimplementation of fetchMore()
adds rows to the model, beginInsertRows() and endInsertRows() must be
called.
Andy
2016-04-30 12:43:10 UTC
Permalink
Thanks Björn,
I'll try it (actually i did the job in the rowCount() function..)
I would have preferred that the documentation be clear about this,
I mean with a detailed example :-)
Slight aside:

Since you're working with QAbstractItemModel I would highly recommend using
Model Test in your debug build:

https://wiki.qt.io/Model_Test

I've written many QAbstractItemModel-derived classes, and I still find them
hard to get right. Model Test has helped catch a lot of edge cases.
Thanks
Anton
Post by Björn Schäpers
Post by anton
Hi,
I have subclassed QAbstractItemModel,
and I want to load additionally data in the model
as soon as somebody clicks on the [+]
in the treeview to load the children of this
item form an external data ( web- app which gives me json data back).
I reimplemented hasChildren() in the model
but i am not sure about the moment to load the data.
I suppose that the rowCount() method of the model is called
when somebody expands an item in the view?
If not what is the moment when my model should ask for additional Data?
Does there exist a small example which demonstrates how to
handle this in an optimal way.
Not sure about beginInsertRows() or layoutChanged() ..
I am experimenting but i get also some crashes so an
example project would be nice.
Thanks
Anton
I think you are looking for QAbstractItemModel::canFetchMore() and
fetchMore(). See also the last paragraph in
http://doc.qt.io/qt-5/qabstractitemmodel.html
To create models that populate incrementally, you can reimplement
fetchMore() and canFetchMore(). If the reimplementation of fetchMore()
adds rows to the model, beginInsertRows() and endInsertRows() must be
called.
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
---
Andy Maloney // https://asmaloney.com
twitter ~ @asmaloney <https://twitter.com/asmaloney>

Loading...