Concept understanding on model view mechanism
-
Hello all
I have a problem I have thread that collects data this data is needs to be stored in some structure this is no problem .
The problem I have is that this data that is collected when user open treeview im using to represent this data .
What I need to do ? to loop throw this structure and fill the treeview cells OR is I understand the model /view concept
I can always store the data in the model (this will be my structure for the data coming from the thread that collects the data)
. and then I can use the view when the user what to visualize the data in the treeview .
Is this right ?
Im using the QtreeView , and QStandardItemModel as the model GroupSortFilterProxyModel for filtring -
If you already have a data structure that you want to visualize using QTreeView, you're better off subclassing QAbstractItemModel. This way the model becomes a model of your actual data, i.e. an interface to your actual data, suitable for what is needed by QTreeView.
The alternative is to actually store your data in a QStandardItemModel, and keep that model up-to-date yourself. -
[quote author="ludde" date="1309961650"]The alternative is to actually store your data in a QStandardItemModel, and keep that model up-to-date yourself.[/quote]
As you already have your data, this is not the best way I think. Using a custom model (especially a readonly one) is really easy and much more performant than the standard item model.
-
what is custom model?
also the information i missing is how to i combine my data with the model class ,
for example if i have hastable that holds my data . how i set the model to store it and use it .
also if for example i subclass QStandardItemModel , do i need to make it single tone so it will keep persist ?
for latter use in the application ? -
Hi umen242,
I think you should read "this in the docs":http://doc.qt.nokia.com/4.7/model-view-programming.html . It describes the model view concept.
The general idea is you have a custom data structure (as you already have) and implement a model class based on a defined interfaces (QAbstractItemModel or derived) so standard views can use it. The data could then also be modified through this interface by the views. And if the content changed, the model has signals to inform the views.
QStandardItemModel is a full blown model with an own data structure inside, so all data that is displayed must be copied to that data structure. If you implement your own, you can reuse your own structure.
-
[quote author="umen242" date="1310013312"]what is custom model?[/quote]
A QAIM that you created, instead of using one of the pre-cooked models like QSIM.
[quote]
also the information i missing is how to i combine my data with the model class ,
for example if i have hastable that holds my data . how i set the model to store it and use it .
[/quote]You reimplement the nessecairy virtual methods on the model, like rowCount, data() and flags().
[quote]
also if for example i subclass QStandardItemModel , do i need to make it single tone so it will keep persist ? for latter use in the application ?
[/quote]
I don't understand this question.If your storage is a hash, you are in trouble keeping the model and the storage syncronized, because a hash does not provide the signals you need for that. You should considder using a custom class as your data store (that in turn could use a QHash in the backend). That gives you more control over what should happen if your data is modified.
-
Thanks for the response.
well i understand now . the model will have in its members the custom data class that can be singleton and its backend can store its data in QHash . great! . but now what should i do
it this Custom data object got updated each N seconds . what signal should i trigger in the model to keep it updated each N seconds?
also when i think about it . the model is set only when i like to view something . so from my Custom data object how can i check if the model is set to it could be updated .. -
umen242: first of all, don't fall into the trap to merge your model and your data store in a single class. That will lead to very cluttered, confusing API. Your data store is there to provide an interface to the data for your application, your model is there as an adaptor layer to provide access to (a part of) your data from one or more views in your UI. Those are the roles, stick to them! Of course, your data store can know of the model and work together with it. It is up to you to tell the model from your data store about which items had their contents changes, which items disapeared, and which new items were created. Most of these will ideally be done like this: tell the model that you will perform an operation, perform the operation, and then tell the model that you have performed the operation. That matches best with the signals the model needs to send to the view.
That is also the answer to your last question: you emit the right signals from the model. That way, the views know what has changed.
-
[quote author="umen242" date="1310019033"]yeah the hash was only example , it will be i guess vector with custom element in each element[/quote]
I sometimes use a combination of both: A vector that stores my data objects, and a hash that provides a lookup from a key to a vector index for quick retreival. Or the other way around: a hash that contains the data objects, and a vector that contains keys to the hash to provide an order. It depends on what kinds of updates you need, and what lookup method you'll need most. A third option is of course to put your data objects on the heap, and store pointers to these objects in the containers you need to provide easy access. In all cases, it is up to you to keep those containers in sync, of course.Edit:
And even in the last case, you still have the option of using vanilla pointers, or smart pointers to put in the containers. Choices, choices, choices...