Results 1 to 5 of 5

Thread: Display objects with differing column count form a TreeModel in a TableView

  1. #1
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Display objects with differing column count form a TreeModel in a TableView

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display objects with differing column count form a TreeModel in a TableView

    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,
    _

  3. #3
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Display objects with differing column count form a TreeModel in a TableView

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display objects with differing column count form a TreeModel in a TableView

    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,
    _

  5. #5
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Display objects with differing column count form a TreeModel in a TableView

    Thanks anda_skoa. Really helpful and to the point.

    Greetz trixn

Similar Threads

  1. Replies: 1
    Last Post: 5th May 2014, 09:10
  2. How to get count of items in QListView column?
    By qt-closer in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2010, 15:40
  3. Using a TreeModel with a treeView and tableView
    By sheik482 in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2010, 04:25
  4. QTableView row/column count [SOLVED]
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 14th May 2009, 12:10
  5. Need to make QLCDNumber display count up/down
    By JimDaniel in forum Newbie
    Replies: 2
    Last Post: 29th September 2007, 02:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.