Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Proxy models that "add" items

    General and Desktop
    2
    6
    1840
    Loading More Posts
    • 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.Emerton
      James.Emerton last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.Emerton 1 Reply Last reply Reply Quote 0
        • James.Emerton
          James.Emerton @SGaist last edited by

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • James.Emerton
              James.Emerton last edited by

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

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • First post
                  Last post