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. Proxy models that "add" items
QtWS25 Last Chance

Proxy models that "add" items

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.9k 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.
  • James.EmertonJ Offline
    James.EmertonJ Offline
    James.Emerton
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you also re-implement mapFromSource ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      James.EmertonJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Did you also re-implement mapFromSource ?

        James.EmertonJ Offline
        James.EmertonJ Offline
        James.Emerton
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you show how you stack your models exactly ?

          Is it:

          • DummyRowProxyModel
          • QSortFilterProxyModel
          • YourBaseModel

          Or

          • QSortFilterProxyModel
          • DummyRowProxyModel
          • YourBaseModel

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • James.EmertonJ Offline
            James.EmertonJ Offline
            James.Emerton
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you share your implementation of DummyRowProxyModel ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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