QAbstractItemModel and external data
-
I know this has been asked before but I can't seem to find enough information on the topic in order to feel confident in my understanding here. I have some data that I would like display in a QTreeView. The model that I am creating to interface between the data and the tree-view does not, and I strongly feel that it SHOULD NOT own that data. To me it seems like the entire concept of models is specifically designed to sit between the data store and the views, rather than acting as a data-store in itself. With that said, the docs state that begin/end-InsertRows() must be called around the data modification itself so:
- I don't want to expose public versions of the begin/end methods because that smells really bad.
- I don't want to hand off the responsibility of modifying the data-store to the model itself, as I believe that breaks the separation of concerns in my application.
- I'm considering connecting the model with the code that modifies the data-store via signals so that the model can handle the begin/end at the appropriate times, as notified by signals. This feels like I'm directly fighting with the intended design of the models.
How is this supposed to be done generally? Or do most developers shove the data directly in the model and interface directly with it when making updates. This seems very limiting and quite odd to me, but maybe I haven't wrapped my head around the idea.
Thanks in advance.
Nick -
Hi and welcome to devnet,
Well owning or not depend on your use case.
That said, in your case, it seems that the model will be a wrapper on top of your data. In that case, there's a need to implement the model functions to forward the manipulations to your data structure (for example new rows insertions, removal, drag and drop, etc).
You should also add adequate APIs that allows external changes to the data structure to be propagate through the model (for example when some data is changed).