2

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 QListViews 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 ieth 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.

flonk
  • 3,726
  • 3
  • 24
  • 37
  • you could explain in detail the structure that is shown in the QTreeView. Do you want the following? https://imgur.com/a/m8g9RMM – eyllanesc May 30 '18 at 20:11
  • @eyllanesc Not exactly, every `+` refers to a node, the indentation gives the hierarchy. However, note that my question is not on the implementation details of this particular problem. I am asking how two widgets would *principally* derive two *structurally different* representations from the same data model. – flonk May 31 '18 at 06:13
  • Actually the model is the data itself, but the relationship between the elements, for example: `my_list = [[1,2,3], [2,3,4,5]]`, is not a model because it does not There is a relationship. If you want to have more details about the structure of the models in Qt, I recommend reading http://doc.qt.io/qt-5/model-view-programming.html. Each type of view expects a type of model, so look for the other items with respect to the model or structure they have defined – eyllanesc May 31 '18 at 06:17

1 Answers1

0

That's what viewmodels are well suited for. They are proxy models: they adapt the source model for a certain organization of display. You'd be usually deriving from QAbstractProxyModel. A simple example is provided e.g. here.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313