Question on model/view design: ListView with model that does not own the data
-
A QListView has a model M that inherits from QAbstractListModel. Model M represents some data D.
Standard design seems to be that M owns D. To remove an item from D, we would first call QAbstractListModel::beginRemoveRows before actually removing the item, and similarly for other operations. Therefore, all data manipulations should take place in member functions of M.
However, in our application, data D are to be manipulated not only by QListView but also by some other mechanism. Preferably that other mechanism should only see D, not M. After changing D, we can emit a signal S.
So our class M only implements functions rowCount and data, which have read access to D. There will be no calls to beginRemoveRows and similar functions.
Question: How to react to signal S so that the list view is redrawn using the latest state of D? Just call repaint???
-
A QListView has a model M that inherits from QAbstractListModel. Model M represents some data D.
Standard design seems to be that M owns D. To remove an item from D, we would first call QAbstractListModel::beginRemoveRows before actually removing the item, and similarly for other operations. Therefore, all data manipulations should take place in member functions of M.
However, in our application, data D are to be manipulated not only by QListView but also by some other mechanism. Preferably that other mechanism should only see D, not M. After changing D, we can emit a signal S.
So our class M only implements functions rowCount and data, which have read access to D. There will be no calls to beginRemoveRows and similar functions.
Question: How to react to signal S so that the list view is redrawn using the latest state of D? Just call repaint???
@Joachim-W said in Question on model/view design: ListView with model that does not own the data:
Question: How to react to signal S so that the list view is redrawn using the latest state of D? Just call repaint???
Call listView->reset()
void QAbstractItemView::reset()
Reset the internal state of the view.
Warning: This function will reset open editors, scroll bar positions, selections, etc. Existing changes will not be committed. If you would like to save your changes when resetting the view, you can reimplement this function, commit your changes, and then call the superclass' implementation.@Joachim-W said
Standard design seems to be that M owns D.
No, only a weak reference suffice. Usely it is owned by the window/widget where the list/table/tree views are ( the controller in the MVC paradigm)
-
@Joachim-W said in Question on model/view design: ListView with model that does not own the data:
Question: How to react to signal S so that the list view is redrawn using the latest state of D? Just call repaint???
Call listView->reset()
void QAbstractItemView::reset()
Reset the internal state of the view.
Warning: This function will reset open editors, scroll bar positions, selections, etc. Existing changes will not be committed. If you would like to save your changes when resetting the view, you can reimplement this function, commit your changes, and then call the superclass' implementation.@Joachim-W said
Standard design seems to be that M owns D.
No, only a weak reference suffice. Usely it is owned by the window/widget where the list/table/tree views are ( the controller in the MVC paradigm)
If D can issue signals then those signals can trigger necessary actions in M that will, in turn, updates in all attached views (can be more than one).
At the coarse level trigger something like:void M::dChanged() { beginResetModel(); // refresh/discard any cached internal state M has about D endResetModel() }If you get D to issue finer grained signals e.g., rowDeleted(uuid) , then you may be able to do finer things within M in response. This is desirable from a user view perspective.
-
J Joachim W has marked this topic as solved on