PDA

View Full Version : Converting to Model/View from hard-coded trees and tables



d_stranz
6th March 2012, 18:46
I started to reply to the thread "Multiple model to single view", then decided that it would be better to post as a separate thread.

I am going to face a similar problem soon, when I convert from a hard-coded QTreeWidget / QTableWidget to a model-based QTreeView / QTableView. My current hard-coded table displays variable content, depending on what the user has selected from a tree widget-based summary (of the results of analysis of experimental data). If you click a top-level node in the the table displays 3 things: a composite summary of all results contained under that node, followed by summaries of each second-level node, with each second-level node followed by the details at the third level. The table is essentially a highly detailed expansion of the tree.

If the user clicks into a second level node, the high level summary is skipped, and only the second and third levels are displayed. If a third level is clicked, then only a single line (the details of the specific leaf in the tree) is displayed.

No matter what level is clicked, the table also has a block of information at the very top that describes the experiment meta-information (data file name, instrument, operator, etc.)

In addition to populating the table, clicking on the tree also triggers updates of graphical displays of the results (using QwtPlot). Multiple selection is permitted, so there could be several "sub-trees" of summary and detail displayed in the table.

In the current implementation, each level in the tree points to somewhere in a nested data structure. Each level in the data structure is a different C++ class. So for example, TopLevel contains a vector of SecondLevel instances, and each of these contains a vector of ThirdLevel instances. Each level contains additional class instances that hold the summary information for that level.

Converting this into a tree-like QAbstractItemModel should be pretty easy. I would implement the tree display with a QSortFilterProxyModel that displayed only the minimal information that is in the tree. The problem would then be, how to display the selected results in the table? I think a QSortFilterProxyModel might also work, but the logic to decide which items are displayed where will be very complex and would depend heavily on interaction with the selection model.

The current table also has visual formatting (horizontal rows of minimum height cells with grey background) to serve as separators between various sections of the table.

So, possible solutions I have thought about include:

1 - A single Model, with SortFilter proxies for the tree and table
2 - A Model for each level in the hierarchy, and customized SortFilter proxies for the various view
3 - A Model for each level in the hierarchy, and implementing multiple table views using splitters within a scrolling frame
4 - something different from each of these

Right now, solution 1 seems to be the most "doable".

From my point of view, this problem is no different from a database report that has summary and detail lines in it. How is that problem handled within the Model/View architecture? Are there examples anywhere that illustrate how to flatten a hierarchical data structure into a table?

Thanks in advance for any advice / pointers.