I just learned about the model/view concept in Qt and arrived at a rather general question.
I understood how Models can be used to associate certain data types to appropriate Widgets, for example QAbstractListModel
can associate a list to a QListView
or a QComboBox
.
Special benefit arises, when multiple widgets are associated to the same data via a common model, such as a QComboBox
and several QListView
s communicating to the same, single QAbstractListModel
.
I noticed, that in the textbook demonstrations this situation is always chosen such that the widgets display the data in the same structure, e.g. the entries of the Combo and the List at position i
are both derived from the i
eth list item, rather than from the total content of the data.
My question is, generally, how would one approach the situation where the same data is to be displayed by different widgets in completely different structures, i.e. the entries (and their number) is a general function of the whole dataset?
To give a (rather complex) example, say that my data is a list of lists of integers,
my_list = [[1,2,3], [2,3,4,5]]
and I would like to display it using two widgets as follows:
QTableView
List | Length | First Item
----------+--------+-----------
[1,2,3] | 3 | 1
[2,3,4,5] | 4 | 2
QTreeView
+ [1,2,3]
+ Length
+ 3
+ First item
+ 1
+ Left-truncated Sublists
+ [2,3]
+ Length
+ 2
+ First item
+ 2
+ Left-truncated Sublists
+ [3]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 3
+ [3]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 3
+ [2,3,4,5]
+ Length
+ 4
+ First item
+ 2
+ Left-truncated Sublists
+ [3,4,5]
+ This list very nice because it contains no 2
+ Length
+ 3
+ First item
+ 3
+ Left-truncated Sublists
+ [4,5]
+ This list very nice because it contains no 2
+ Length
+ 2
+ First item
+ 4
+ Left-truncated Sublists
[5]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 5
+ [5]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 5
+ [4,5]
+ This list very nice because it contains no 2
+ Length
+ 2
+ First item
+ 4
+ Left-truncated Sublists
+ [5]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 5
+ [5]
+ This list very nice because it contains no 2
+ Length
+ 1
+ First item
+ 5
+ there are 2 lists in total
Of course I do not expect a fully coded detailled answer, but some hints or resources would be nice, how to approach this using the MVC in Qt
/PyQt
.