Proxy models that "add" items



  • In an application that makes heavy use of the Qt (v4.8.6) itemviews framework, I'd like to use proxy models for sorting (QSortFilterProxyModel) as well as adding a "dummy" row, which can be edited and will automatically insert an item in the collection.

    I have a proxy model which inherits QIdentityProxyModel and overrides rowCount() to add 1, as well as overriding data() and setData(). Everything seems to work correctly until I try to stack DummyRowProxyModel on top of QSortFilterProxyModel. That's when it crashes.

    I discovered that by overloading mapToSource() and returning QModelIndex() I can overcome the crash, but I still get a bit of odd behaviour. Specifically, when viewing the model with a QTreeView the dummy row now has child rows containing the original model data! Overriding hasChildren() to return false for the dummy row seems to have corrected this, but I'm still not able to create an edit widget for any cell on the dummy row.

    When I looked at this in a debugger I can see that it's refusing to create an editor for an invalid index, and indeed the row and column values in the index are far out of range.

    Question: Is it possible to use a proxy model to create indexes that have no counterpart in the underlying model, or must there always be a corresponding index in the source model?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you also re-implement mapFromSource ?



  • @SGaist I've based my implementation on QIdentityProxyModel, which already implements mapFromSource(). Since the dummy row doesn't exist in the source model there is no valid implementation of mapFromSource() that could return an index to the dummy row.


  • Lifetime Qt Champion

    Can you show how you stack your models exactly ?

    Is it:

    • DummyRowProxyModel
    • QSortFilterProxyModel
    • YourBaseModel

    Or

    • QSortFilterProxyModel
    • DummyRowProxyModel
    • YourBaseModel


  • The former; I don't want the dummy row affected by sorting as it should always be the last item.


  • Lifetime Qt Champion

    Can you share your implementation of DummyRowProxyModel ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.