Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Inserting and deleting rows with QAbstractProxyModel
Forum Update on Monday, May 27th 2025

[Solved] Inserting and deleting rows with QAbstractProxyModel

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 6.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abgeos
    wrote on 24 Sept 2011, 00:39 last edited by
    #1

    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.

    Thanks

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eddy
      wrote on 24 Sept 2011, 05:18 last edited by
      #2

      -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?

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • A Offline
        A Offline
        abgeos
        wrote on 24 Sept 2011, 05:28 last edited by
        #3

        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?

        Thanks.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Eddy
          wrote on 24 Sept 2011, 05:35 last edited by
          #4

          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?

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • A Offline
            A Offline
            abgeos
            wrote on 25 Sept 2011, 23:42 last edited by
            #5

            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.

            Thanks

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Eddy
              wrote on 26 Sept 2011, 08:48 last edited by
              #6

              You're welcome.

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

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0

              1/6

              24 Sept 2011, 00:39

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved