Design approach to feeding model with data
-
Hello,
In my application I'm going to fetch some data from the internet. I'm sending a request (visiting special url) and as a result I get the xml file with a lot of data. I'm going to parse obtained xml to get the data I'm interested in. Because there will be more than one item obtained from xml they will be presented on list. I'm subclassing a QAbstractListModel as a base model for ListView, but... how to organize feeding the model with the data.
I'm not really convinced if a model should do the fetching&parsing of the xml. I would rather have a separate class which would obtain all the data and would add that data to the model. Is this approach a canonical way of implementing this kind of scenarios? I'm thinking this class, lets call it Fetcher would have a pointer to the model so that it can add data to the model, I do not see here an advantage of using signals&slots to pass this data to model. The Fetcher will use separate thread for sure, so it must be a QObject so that it can be movedToThread. And again where to put QThread? I'm pretty sure it is better to have it out of the scope of Fetcher.
Do you find any flaws in my thinking?
Regards,
Konrad. -
The approach is ok, I'd say. I would just do it the other way round: Let the model be an adapter that knows how to pull the data out of the storage class that receives the XML and parses it. The model will ping the data structure if it needs more data. You can implement some lazy loading this way.
-
That is tempting, but the xml will be fetched only once therefore the data to the model will be put only once. The QAbstractListModel has the insertRows() which suggests that the model should be feeded with the data rather than model should ask for data. On the other hand this probably been designed with the idea in mind that data to model is added by using the view (editor for example). @Volker I will think about you solution and will give it a try. I assume you have a successfull run of implementing it this way :)
-
We implemented a tree model that lazy loads data from a database via xml requests to a server (some sort of home brewn soap).
If you read that XML only once then it is perfectly ok to store all the data in the QStandardListModel. I'd use a separate class for the parsing and feeding the model.
-
Only once*
*Of courts I assume that the end user will not have view presenting data from xml opened for a year or so ;) in this situation would be better to trigger reloading it from time to time ;)
Thanks for help @Volker. I appreciate because I have not thought about the other-way-round approach.
-
The reload button would not be hard to implement (actually I'd add a slot to the parser object). Just call "QAbstractItemModel::beginResetModel() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#beginResetModel before you wipe out the old data and call "QAbstractItemModel::endResetModel() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#endResetModel after you have repopulated it.
-
Exactly.