Results 1 to 15 of 15

Thread: Proxy model, index mapping.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proxy model, index mapping.

    Quote Originally Posted by wysota View Post
    You can't reimplement mapFromSource() and mapToSource() for QSortFilterProxyModel because it needs its own implementation of those methods. Subclass QAbstractProxyModel, do the mapping there and then apply your proxy on a filter proxy model or the other way round.
    So you're saying that if you want to support both sorting -and- source/proxy mapping, you need two layers of proxy model? One to handle the mapping and another handle sorting?

    In my case I have a model with many virtual columns, only a few of which need to be shown in any one table view. In each view, the order of the columns is different. In one view I'd like to support sorting.

    I derived from QSortFilterProxyModel and reimplemented mapToSource / mapFromSource and filterAcceptsColumn. All worked fine until I turned on sorting and clicked a column header. As you state, it blew up because the internal pointer in the proxy was invalid.

    OK, so I need a plain vanilla QSortFilterProxyModel with my filterAcceptsColumn method. The table uses this one. Then I need a QAbstractProxyModel that implements my mapping, and I set this as the source model for the table's proxy. Finally, I set my base model as the source model for the mapping proxy.

    Everything should cascade up and down through the proxy layers between the base model and the view, right?

    Then, what happens with the table's item selection model? Does it need to use the middle proxy instead of the base model as its source?

    Wish this were better documented somewhere. The Qt docs and examples are just too basic.
    Last edited by d_stranz; 21st November 2009 at 22:05. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Proxy model, index mapping.

    Quote Originally Posted by d_stranz View Post
    So you're saying that if you want to support both sorting -and- source/proxy mapping, you need two layers of proxy model? One to handle the mapping and another handle sorting?
    Sorting is also mapping and you can only have one mapping at a time, so yes - you need two layers.

    Everything should cascade up and down through the proxy layers between the base model and the view, right?
    Yes, that's correct.

    Then, what happens with the table's item selection model? Does it need to use the middle proxy instead of the base model as its source?
    The selection model always operates on the model set directly on the view.

    Wish this were better documented somewhere. The Qt docs and examples are just too basic.
    That's what we're here for.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    d_stranz (22nd November 2009)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Proxy model, index mapping.

    OK, thanks. I implemented the three layers of models, and it's almost working.

    The Qt documentation for QAbstractProxyModel is wrong - it implies that to derive a custom proxy, you need only implement the mapTo/FromSource methods. There's a bunch more pure virtual methods in QAbstractItemModel that also need to be implemented.

    I've probably done some of that wrong, because I don't know whether mapping needs to be done (in the index() method, for example) or not, because I don't know who is calling them and in what direction the information is flowing.

    Thanks again for the help.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Proxy model, index mapping.

    Quote Originally Posted by d_stranz View Post
    The Qt documentation for QAbstractProxyModel is wrong - it implies that to derive a custom proxy, you need only implement the mapTo/FromSource methods. There's a bunch more pure virtual methods in QAbstractItemModel that also need to be implemented.
    Technically speaking it's not wrong. As classes are inheriting other classes, you'd have to understand the sentence from the docs as "to subclass QAbstractProxyModel you have to implement x and y apart from things you have to implement from QAbstractItemModel".

    I've probably done some of that wrong, because I don't know whether mapping needs to be done (in the index() method, for example) or not, because I don't know who is calling them and in what direction the information is flowing.
    The proxy has to return all data (apart from mapToSource()) based on itself. So index() has to return index in the proxy space. A crude implementation of index() for flat models would be:

    Qt Code:
    1. QModelIndex MyProxy::index(int row, int column, const QModelIndex &parent = QModelIndex()) const {
    2. if(parent.isValid() || row<0 || column <0 || row>=rowCount() || column>=columnCount())
    3. return QModelIndex();
    4. return createIndex(row, column, 0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    A less crude implementation would probably have something more useful than "0" as the last parameter to createIndex().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to filter duplicates from a proxy model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 14th October 2016, 10:21
  2. Replies: 1
    Last Post: 18th November 2009, 23:21
  3. Replies: 3
    Last Post: 31st March 2008, 21:23
  4. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  5. Model and Proxy
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2006, 21:05

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
  •  
Qt is a trademark of The Qt Company.