PDA

View Full Version : Display objects with differing column count form a TreeModel in a TableView



trixn
27th June 2016, 17:08
Hi,

i have a design problem concerning the model/view architecture in qt 5 and i could really need some help.

Let's say i want to build an application that is capable of managing media (a media library) that can be lend. I have at least two (or more) different types of Media e.g. Book, Movie-DVD, Music-CD etc...
Therefore if have the following Classes:

- Medium (Base class with some basic member variables that all media shares)

- Book
- DVD
- CD

- User (A Person that can rent the media and give it back)


I want to be able to display all Media in a plain TableView with only the data that all media has in common, e.g. Author and Title. (for example to show which media was rent by a user)
I want to be able to display a single special Type of media, e.g. DVD in a plain TableView with all it's properties derived from the media class and the additional properies from the DVD class.

Now my problem is: How do i design the model to only use a single data source but serve the data this way to a TableView.

I already got the tip to use a TreeModel for storing the data in a tree and a QSortFilterProxyModel to filter the special types for the corresponding TableViews. But how do i tie everyting together? Is this approach the right one or is there a solution that suits the problem better?

Thanks in advance for your help.

greez trixn

anda_skoa
27th June 2016, 18:11
Since you are only ever going to display tables, why on earth would you want to incur the complexity of a tree?

You only need a simple table model that sees all media objects and one that only sees the media objects for a certain type.
In the latter case you might want to consider having one model per data type instead of doing all the type related switching inside a single class.

Cheers,
_

trixn
27th June 2016, 20:26
First of all thanks for your input.

Yeah i asked that question on stackoverflow and the tip with the tree model was one of the answers. I realized this is not exactly what i need. I'm trying to do it like this now:

- Implement an QAbstractTableModel which is holding a list of Medium-Object pointers.
- use QSortFilterProxyModel derived model to filter relevant derived objects from Medium based on an enum type i introduced.

The intention of having only one model which actually holds the data is to be able to select entries in a TableView that only shows e.g. DVDs and delete them so they are also deleted in the Medium-Model. I don't want to have several lists that i have to keep synchronized.

Is that the approach to go for ?

Thanks for your answers.

anda_skoa
27th June 2016, 21:27
This is a valid approach and would almost certainly provide for your needs.

However, I would choose a different approach if I were to implement that.

First, I would store the media objects in separate lists, one for each C++ type:
- no casting is required when accessing media type specific
- no filtering needed to just see one type
- still easy to list all media by just going through all lists in any order I want

These lists would be in an object that any add/remove/change operation would go through:
- the object can emit signals when data is added/removed/changed
- it can implement overall saving/loading if that is required
- it makes data handling testable without any model API

Each model would have access to this object
- the overall model's rowCount would just be the sum of all list counts
- the detail model(s) would work on a single list
- if any model affects change, it would go through the list holder object
- the signals of the list holder object would allow each model instance to notify its view(s) of change

Cheers,
_

trixn
27th June 2016, 21:52
Thanks anda_skoa. Really helpful and to the point.

Greetz trixn