Hi anda_skoa,
Thank you for your response.

Originally Posted by
anda_skoa
The model only needs to be able to provide access to data, it doesn't have to store the data.
If you actual data allows you to query for specific data then the model can use that as well.
_
Agreed. But, my external component takes considerable time to retrieve the data. Hence, a buffer of data is maintained in model to achieve smooth scrolling.

Originally Posted by
anda_skoa
A list is a flat structure, there are not parent nodes.
_
Ok. Understood! Thanks!

Originally Posted by
anda_skoa
It depends on what you use the reset for.
When you reset the model and only provide Y elements, then that is all the data you have as far as the view is concerned.
If you want to reset the model to only see a specific subset, e.g. to make a scrollbar not work on the whole set but only on the subset, then you need to detect when you need to switch out of that "detail" mode.
E.g. some UI to "go back" to the full list, or header/footer items that trigger this return to the full mode.
If you reset for some other reason the maybe the reset is not the right way to achieve the target result.
That's difficult to answer generically, depends on what these components to and how they interact with the data.
A model can at any given times annouce that it is adding or removing rows, or announce that data of existing rows has changed.
Adding/removing can be done through protected helper methods, data change is notified via the dataChanged() signal.
Cheers,
_
My case is like this,
1. Data source is an external component (another process) that provides data in chunks (It has provided APIs to request data in chunks). Number of elements in the source can be huge (in terms of tens of thousands).
2. I have a class (say CDataFetcher) that interfaces with the external data source and fetches the data on need basis.
3. Now, CListModel is my model that has inherited from QAbstractListModel.
{
/*other methods and data members....*/
const Q_DECL_OVERRIDE;
const Q_DECL_OVERRIDE;
QHash<int ,QByteArray>roleNames() const Q_DECL_OVERRIDE;
};
class CListModel : public QAbstractListModel
{
/*other methods and data members....*/
int rowCount(const QModelIndex &parent = QModelIndex())
const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole)
const Q_DECL_OVERRIDE;
QHash<int ,QByteArray>roleNames() const Q_DECL_OVERRIDE;
QStringList myListData;
};
To copy to clipboard, switch view to plain text mode
Here myListData is the buffer of data that I am talking about. As the data can be huge, I am implementing a logic to depict myListData as ring buffer.
Now, if, user scrolls the list either by dragging up or down on the list (please note this action is not done using the scroll bar), then I can provide the data to list view that is available in myListData. If there is no data available in myListData, then,I will fetch the data using CDataFetcher.
{
/*check if the index is in with in the number of elements in myListData*/
/*If yes, return the data*/
/*else, fetch the data from local copy in CDataFetcher*/
}
{
/*This class works in a different thread*/
/*Local copy is always maintained in this class*/
/*If the copy is already used by model, then this class fetches the data and makes a local copy*/
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole)
{
/*check if the index is in with in the number of elements in myListData*/
/*If yes, return the data*/
/*else, fetch the data from local copy in CDataFetcher*/
}
QStringList CDataFetcher::FetchData()
{
/*This class works in a different thread*/
/*Local copy is always maintained in this class*/
/*If the copy is already used by model, then this class fetches the data and makes a local copy*/
}
To copy to clipboard, switch view to plain text mode
This way, though I am not storing the entire data in memory, I am achieving smooth scrolling by maintaining small chunks of data. This works fine for linear scrolling on the list.
Now, in an other case (case 3 as I mentioned in my previous post), suppose user is viewing elements that starts with alphabet "C" and user selects alphabet "Y" on the scrollbar, I should show elements that starts from "Y". Here, this behaviour is similar to contact list behaviour in iphone.
I can get list of elements that starts with "Y", but if I have to show it, (as per my design) I have to buffer it in my model. To do this, I will have to erase previous contents and store my current contents.
When I do this, list view assumes that my list starts with elements "Y" and when user scrolls up on the list to view elements that starts with "X" or "W" or "V"..., I will not get any request to get previous data
How can I fix this? Similar problem is explained in this forum.
Ideally, if I had an API like scrollToItem or JumpToIndex, I would not have ran in to this problem.
Let me know your comments on this. Please note that QT version that I am using 5.0.2
Bookmarks