Results 1 to 4 of 4

Thread: Reimpementing mapFromSource and mapToSource of QidentityProxyModel

  1. #1
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Reimpementing mapFromSource and mapToSource of QidentityProxyModel

    Hello,

    I'm trying to alter a QFileSystemModel by means of a subclassed QidentityProyModel. The alteration consists in removing all rows but one at a specific location in the proxy model, and I don't want any modifications in the source model.
    I tried to reimplement the mapToSource and mapFromSource methods that way:
    Qt Code:
    1. QModelIndex RootItemProxyModel::mapFromSource(const QModelIndex & sourceIdx) const
    2. {
    3. // If the proxy is not activated, use the base mapFromSource function.
    4. if (m_ActivatedProxy == false)
    5. return IdentityFileSystemProxyModel::mapFromSource(sourceIdx);
    6.  
    7. // Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
    8. if (sourceIdx.parent().internalPointer() == m_rootSrcParentIdx.internalPointer())
    9. {
    10. if (sourceIdx.row() != 0)
    11. return QModelIndex();
    12. else
    13. return createIndex(0,
    14. sourceIdx.column(),
    15. sourceModel()->sibling(m_rootSrcIdx.row(),sourceIdx.column(),m_rootSrcIdx).internalPointer());
    16. }
    17. // In any other cases, use the base mapFromSource function.
    18. return IdentityFileSystemProxyModel::mapFromSource(sourceIdx);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QModelIndex RootItemProxyModel::mapToSource(const QModelIndex & proxyIndex) const
    2. {
    3. // If the proxy is not activated, use the base mapToSource function.
    4. if (m_ActivatedProxy == false)
    5. return IdentityFileSystemProxyModel::mapToSource(proxyIndex);
    6.  
    7. // Alter the mapping so the proxy model sees only m_rootSrcIdx's row as m_rootSrcParentIdx child.
    8. QModelIndex sourceIdx = IdentityFileSystemProxyModel::mapToSource(proxyIndex);
    9. if (sourceIdx.parent().internalPointer() == m_rootSrcParentIdx.internalPointer())
    10. {
    11. if (proxyIndex.row() != 0)
    12. return QModelIndex();
    13. else
    14. return sourceModel()->sibling(m_rootSrcIdx.row(),
    15. proxyIndex.column(),
    16. m_rootSrcIdx);
    17. }
    18. // In any other cases, use the base mapToSource function.
    19. return sourceIdx;
    20. }
    To copy to clipboard, switch view to plain text mode 
    The only examples I could find about mapToSource and mapFromSource reimplemented methods were for transpose proxy models in which each Modelindex in the Source model maps to a Modelindex in the Proxy model. In my case, the Proxy model has less Modelindexes than the SourceModel. I return QModelIndex() for any ModelIndex that maps to, or from, a ModelIndex that belong to the rows I want to remove.
    I'm not sure that it is what I should return in that case. I'm not even sure that my overall strategy is correct.

    If someone experienced can put me on the right track, that would be great.
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reimpementing mapFromSource and mapToSource of QidentityProxyModel

    Let me try to understand this: you want the proxy model to filter out rows, yet you are using the QIdentityProxyModel as a base class, which works with the base assumption that it does not change the data's structure.
    Hence it being called QIdentityProxyModel.

    Wouldn't it be much easier to base this on QSortFilterProxyModel and implement filterAcceptsRow()?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Reimpementing mapFromSource and mapToSource of QidentityProxyModel

    Ideally I would use a QSortFilterProxyModel as you suggested. My problem is that other QIdentityProxyModels would be chained to that QSortFilterProxyModel for subsequent data processing in my project. According to my limited Qt experience, QSortFilterProxyModels and QIdentityProxyModels don't go well together. I reported a bug about that 9 month ago and it has not even been evaluated yet: QTBUG-32981
    [URL="https://bugreports.qt-project.org/browse/QTBUG-32981"]
    I tried to work around that by using a QIdentityProxyModel instead, so I have only QIdentityProxyModels in my proxy model chain.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reimpementing mapFromSource and mapToSource of QidentityProxyModel

    Well, my guess would be that time would best be spent on fixing the bug, but if you really need your own filter model it is probably better to base it off QAbstractProxyModel instead of making QIdentityProxyModel do the opposite of that it is intended for.

    Cheers,
    _

Similar Threads

  1. QAbstractProxyModel , mapToSource() and mapFromSource()
    By sajis997 in forum Qt Programming
    Replies: 7
    Last Post: 3rd April 2012, 11:42
  2. Replies: 0
    Last Post: 8th December 2011, 01:00
  3. Help with ::mapFromSource (Proxy Model Question)
    By SSurgnier in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2011, 18:02
  4. Replies: 0
    Last Post: 21st June 2011, 18:27
  5. Replies: 3
    Last Post: 6th February 2010, 20:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.