Results 1 to 17 of 17

Thread: Custom QAbstractProxyModel

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Custom QAbstractProxyModel

    Hello
    I have a question about QAbstractProxyModel. Suppose there is model and view.
    I want to use a proxy between model and view which multiplies a column of data in model by a value. Is it possible?
    Thanks

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

    Default Re: Custom QAbstractProxyModel

    Yes, of course.
    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:

    alizadeh91 (3rd December 2012)

  4. #3
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Should i re-implement specific methods for that?
    Last edited by alizadeh91; 3rd December 2012 at 15:43.

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

    Default Re: Custom QAbstractProxyModel

    Quote Originally Posted by alizadeh91 View Post
    Should i re-implement specific methods for that?
    Yes, you need to implement all pure virtual methods and reimplement those virtual methods that govern the behaviour you want to change. Note that if you're not used to proxy models and you only wish to change data returned by the base model, consider subclassing QSortFilterProxyModel instead of QAbstractProxyModel.
    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.


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

    alizadeh91 (3rd December 2012)

  7. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Thanks for useful hints,
    That's true, i can use QSortFilterProxyModel. But in this case what methods should i re-implement?

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

    Default Re: Custom QAbstractProxyModel

    Quote Originally Posted by alizadeh91 View Post
    But in this case what methods should i re-implement?
    If you wish to modify values returned by the model, reimplement data().
    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.


  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Since you don't want to change the structure of the data, I'd suggest to have a look at QIdentityProxyModel.

    So you'll only have to reimplement data and can forward any call that is not the display role and for the column which you want to change.

    Cheers,
    _

  10. #8
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Here is what i want to do, see if can suggest me best way:
    I have a view and a custom header with comboBox for every column.
    In the comboBox there are some units(e.g kilogram,gram, pound, ...). if user change the unit, the content of that column have to be changed according to unit.
    Since just the unit of data is changed i don't want to change the model. I just want to display its equivalent in desired unit.
    So i thought best way to implement this is proxy. may be if i use a proxy in model i can fix the model but just change the displaying data in proxy.
    Is that true?
    Is there better solutions?

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

    Default Re: Custom QAbstractProxyModel

    Quote Originally Posted by alizadeh91 View Post
    So i thought best way to implement this is proxy. may be if i use a proxy in model i can fix the model but just change the displaying data in proxy.
    Is that true?
    Is there better solutions?
    Hmmm... are you even reading what we have been writing here? Posts #6 and #7 already contain all the answers you need.
    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.


  12. #10
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Thanks wysota, Yes i'm reading! i just want to clear the problem!

  13. #11
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    If you wish to modify values returned by the model, reimplement data().
    Thanks again wysota, I've implemented the data() and it's ok except that the table doesn't update itself when i modify values returned by the model. I've even
    try emit dataChanged() signal but i'm getting a weird error:
    Qt Code:
    1. error: C2662: 'QAbstractItemModel::dataChanged' : cannot convert 'this' pointer from 'const TCustomProxyModel' to 'QAbstractItemModel &'
    2. Conversion loses qualifiers
    To copy to clipboard, switch view to plain text mode 

    (TCustomProxyModel: my custom proxy model)

    Just another thing: when i click on table the table update itself!!!
    Last edited by alizadeh91; 15th December 2012 at 10:22.

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

    Default Re: Custom QAbstractProxyModel

    You can't emit dataChanged() from data() which is a const method. Invalidate the proxy model when its data changes (that is when you perform action that will cause the proxy to return different values).
    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.


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

    alizadeh91 (15th December 2012)

  16. #13
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Invalidate is a little slow wysota!!!

    Sorry: that was my fault because i put invalidate in data() method which cuased a infinite loop!!!
    Last edited by alizadeh91; 15th December 2012 at 11:55.

  17. #14
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    Hi wysota:
    I've implemented it with QSortFilterProxyModel and reimplemented the data() method and it works fine . Thanks.
    But there is another thing: I want to modify and even change the values which return to model when user edit in view. For example suppose i have a model and this model is shown in a view. when user want to modify a value in view, the value before get to model have to pass a filter in proxy model. How can i do that? Do i have to reimplement setData() ? if so, how?
    Thanks.

    Do i have to do this with delegate? I mean modify data returned to model in setModelData() method? I'll prefer to do this in proxy model! if is possible?!
    Last edited by alizadeh91; 25th December 2012 at 12:06.

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

    Default Re: Custom QAbstractProxyModel

    The filter doesn't influence editing the data. If you want, you can reimplement setEditorData() and setModelData() of the delegate.
    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.


  19. #16
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QAbstractProxyModel

    But wysota i have implemented it in this way in proxy model: this->sourceModel->setData(..); Is this way is a right way?! any way it works perfect!

  20. The following user says thank you to alizadeh91 for this useful post:

    Mosi (5th April 2013)

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

    Default Re: Custom QAbstractProxyModel

    Quote Originally Posted by alizadeh91 View Post
    But wysota i have implemented it in this way in proxy model: this->sourceModel->setData(..); Is this way is a right way?! any way it works perfect!
    I meant that this is the default implementation of setData() in a proxy.
    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 subclass QAbstractProxyModel
    By skycrestway in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2010, 00:35
  2. QAbstractProxyModel to do a Tree
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2008, 18:31
  3. QAbstractProxyModel lessThan [SOLVED]
    By killerwookie99 in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2008, 20:42
  4. Help with QAbstractProxyModel
    By killerwookie99 in forum Qt Programming
    Replies: 9
    Last Post: 12th September 2008, 23:13
  5. QAbstractProxyModel and QTreeView
    By niko in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2008, 22:17

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.