[Solved] Inserting and deleting rows with QAbstractProxyModel

  • What is the proper technique to communicate to a proxy model that some rows have been inserted/removed from the source model?

    I have a model derived from QAbstractItemModel (the source model) that contains and manage all my objects. I wrote a proxy model to only look at a small branch of the full model and to filter out a few items.

    Whenever an object is deleted or inserted the pairs beginInsertRows(...) and endInsertRows(...) is called on the source model. This work very well, no problem here.

    The problem happens when I try to communicate to the proxy model that some rows were added/removed from the source model. I simply cannot connect the proper signal slot when an addition or removal of rows is performed. Note that all the data management (addition and deletion) is done at the source model, I just want the proxy and its selection to update themselves.

    Of all the tries I did two ways were almost correct (in term of updating the display) but still crashes:

    1. Connect the signal RowsAboutToBeInserted(...), RowsAboutToBeRemoved(...), rowsInserted(...), RowRemoved(...) from the source model to my proxy model where I call beginInsertRows(), beginremoveRows(),endInsertRows() and endRemoveRows() after mapping the index to the proxy model. This crashes when the proxy call the endRemovesRows; they were already deleted by the source model.

    2. The second approach was to call endInsertRows/endRemoveRows immediately after the beginInsertRows and beginRemoveRows. This crashes when a selection was made, it seems that the selection model is not correctly updated that way.

    So what is the correct way of setting up the signal slots between source and proxy for insertion and deletion of rows.


  • Moderators

    -Afaik this is done automatically by the proxy mechanism.-
    -Did you try without your connect statements?-

    Edit: you are not using qsortfilterproxymodel. My error. But you could look in the qsortfilterproxymodel sources to find an example.

    Is there any reason why you don't use qsortfilterproxymodel?

  • Without the connect statements, adding an item shows up immediately in viewer with the source model but not for the proxy. For instance, I need to resize the proxy viewer for the new items to show up.

    Does that mean that the mapping between source and proxy is incorrect?


  • Moderators

    I guess you didn't see my edit. I was too slow correcting my mistake. qsortfilterproxymodel does it automatically, not qabstractproxymodel.

    Here is the edit again so you don't miss it ;)

    Edit: you are not using qsortfilterproxymodel. My error. But you could look in the qsortfilterproxymodel sources to find an example.
    Is there any reason why you don’t use qsortfilterproxymodel?

  • Thanks for the tip about qsortfilterproxymodel. I ended up subclassing it and implementing filterAcceptsRow as well as setting a root on the view to only see the branch of the tree that I am interested in. Working quite well so far, but had to modify the functions handling the selection from the proxy model to ensure their mapping of the QModelIndex from the proxy model to the source model. So far so good.


  • Moderators

    You're welcome.

    Please add [Solved] in front of your title using the edit link.

Log in to reply

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