Results 1 to 10 of 10

Thread: custom model: subclass the item or the model, or store the actual data elsewhere?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    For simplicity your list of Person objects should be stored in the model object and use the standard model interface or custom extensions to modify it. The two cannot get out of sync this way and the data is stored only once. In this example your model would be a subclass of QAbstractTableModel and implement a row/column interface to display the table required by QTableView. When set as the model on a QListView you use setModelColumn() to select the name column for display.

    If you already have a PopulationModel object managing a list of Person objects then you are essentially adding the QAbstractTableModel interface to it. Ensure that model actions emit the relevant signals and the views will follow changes correctly.

  2. #2
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    Quote Originally Posted by ChrisW67 View Post
    For simplicity your list of Person objects should be stored in the model object and use the standard model interface or custom extensions to modify it. The two cannot get out of sync this way and the data is stored only once. In this example your model would be a subclass of QAbstractTableModel and implement a row/column interface to display the table required by QTableView. When set as the model on a QListView you use setModelColumn() to select the name column for display.

    If you already have a PopulationModel object managing a list of Person objects then you are essentially adding the QAbstractTableModel interface to it. Ensure that model actions emit the relevant signals and the views will follow changes correctly.
    Currently my Person object inherits from QStandardItem and is stored in a QStandardItemModel. The primary interface exists without difficulty in a QTreeView at the moment, but I'm attempting to implement some tabular views (summaries, sortable filters, aggregations) of the data and struggle to do so without duplication. For example, my single QStandardItem has to get spawned into multiple items to span an entire row. Perhaps I'm doing this wrong?

    Santosh's idea to add a layer of abstraction and leave the model as an interface to an underlying list seems to make sense but also sounds very complicated. I've avoided delving into the QAbstractItemModel thus far because of the number of functions and classes that are required to pull off a good implementation.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    Your "Person" object contains three pieces of information and is possibly better modelled as three separate QStandardItems in three columns.

    Presenting a part of a tree as a table can be trivial or very complicated depending on exactly what is required. You can use QAbstractProxyModel instances to rearrange and summarise data to some degree.

    If you can get your hands on a copy of "C++ GUI Programming with Qt4" (2nd Ed is current but maybe the 1st ed is adequate) then the chapter on Item Views and implementing a custom model are very enlightening. Tables are fairly straightforward, trees a bit more tricky.

  4. #4
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    I have a copy of that book sitting on my desk right now - a great resource! :-) Their model-view chapter is especially good and far superior to anything I saw online, but I get a little hazy on the particulars.

    My tree basically contains five branches, each with its children having a unique type - say, Person, Place, and Thing to follow the earlier example. I intend to display the entire Person branch as a table, which seems like it should be straightforward. QComboBox, for example, makes displaying lists from a tree model very simple by assigning the model with QComboBox::setModel() and choosing my branch with QComboBox::setRootModelIndex(), but I don't see a similar implementation for a proxy model, even though that is what I'm shooting for.

    I see that perhaps I've been thinking about the model concept backwards (tree --> table versus table --> tree). Structurally, though, the tree makes the most sense for my model

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    QTableView::setRootIndex() does for the QTableView what you describe for the QComboBox. Set your tree model on the QTableView and then setRootIndex() to the QModelIndex of the parent of the Person entries.

  6. The following user says thank you to ChrisW67 for this useful post:

    Phlucious (14th February 2013)

  7. #6
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: custom model: subclass the item or the model, or store the actual data elsewhere?

    Thanks! It hadn't occurred to me that that might be a view-based concept.

    I'll see about rewiring my brain to approach this from a more model-based perspective (sub-classing QAbstractTableModel), rather than the item-based approach (sub-classing QStandardItem) I've been following up to now. Sounds like that'll flex a little more gracefully as I scale up my application.

Similar Threads

  1. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  2. Replies: 0
    Last Post: 1st February 2010, 11:00
  3. Replies: 1
    Last Post: 3rd June 2009, 19:08
  4. Default Item View classes and returning data from model
    By xerionn in forum Qt Programming
    Replies: 8
    Last Post: 23rd April 2009, 19:50
  5. Custom model item deletion (Qt4.4)
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2008, 14:35

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.