Results 1 to 14 of 14

Thread: Custom proxy model issue

  1. #1
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Custom proxy model issue

    Greetings.

    I have a custom AbstractTableModel, which receives data really fast. I buffer them and emit a beginInsertRows/endInsertRows for a few items at a time.
    In my custom view, in rowsInserted, i continually update a label which shows the rowCount.
    Until now, i used a basic QSortFilterProxyModel, no problems.
    Recently though, i had to switch to a custom one, derived from QSortFilterProxyModel. I basically followed the Custom Sort/Filter Model example.
    I have the following problem:
    I set the dynamic sort/filter flag on and set a custom filter. The items keep arriving, and not only the number representing the number of items does not refresh correctly, in the view the number of rows seems to grow, then shrink, grow again, etc.
    Note: those did not happen when i used a simple QSortFilterProxyModel.
    If all the data is fully loaded, no more row inserts happen, the filtering works fast and correctly.

    What could be the problem?

    Thank you.

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

    Default Re: Custom proxy model issue

    What do you mean by a custom filter? Did you reimplement filterAccepts*() methods?

  3. #3
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    Hello. The filterAcceptsRow method, i did.
    The options provided by filterAcceptsColumn i don't need it yet, but i'll reimplement it, when i do. (basically i was thinking that i should use that instead of setting the column visibility in the view, to regulate which columns are visible, but i haven't decided yet).

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

    Default Re: Custom proxy model issue

    So what is in your filterAcceptsRow()?

  5. #5
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    The following:
    I have an "AdvancedFilter" class, which contains a list of "and" filters and a list of "or" filters. Each one of these filters contain the column ID, the action and the value.
    Basically, one AdvancedFilter looks like this:
    "name has 'xy' and date < '11-02-2008' or age > 18"
    andList:
    { nameId, FilterAction_Has, "xy"} { dateId, FilterAction_LessThan, '11-02-2008' }
    orList:
    { ageId, FilterAction_MoreThan, "18"}

    Qt Code:
    1. for (it = andList.begin(); it != andList.end(); it++)
    2. {
    3. index = sourceModel()->index(sourceRow, (*it).m_column, sourceParent);
    4. switch ((*it).m_action)
    5. {
    6. case FilterAction_Has:
    7. if (sourceModel()->data(index).toString().contains((*it).m_value) == false)
    8. accept = false;
    9. break;
    To copy to clipboard, switch view to plain text mode 

    This is a big switch, for all possible actions. Then i do the same for orList, with the difference that if the data fits the filter pattern, then i set accept to true.
    Finally i return accept.

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

    Default Re: Custom proxy model issue

    Do you invalidate the filter after changing "ands" and "ors"?

  7. #7
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    Yes. I have a setFilter method, where i pass an AdvancedFilter and set the currentFilter in my proxy model to that one, afterwards i call invalidate.

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

    Default Re: Custom proxy model issue

    Disable filtering (make filterAcceptsRow return true for every index) and see if the application starts behaving correctly. If so, the problem must be within the filtering method. In that case post the whole method here.

  9. #9
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    Yes, it does, so here it is:


    Qt Code:
    1. bool AdvancedSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    2. {
    3. bool accept = true;
    4. const QList<Filter>& andList = m_currentFilter.andList();
    5. const QList<Filter>& orList = m_currentFilter.orList();
    6. QList<Filter>::const_iterator it;
    7. QModelIndex index;
    8.  
    9. for (it = andList.begin(); it != andList.end(); it++)
    10. {
    11. index = sourceModel()->index(sourceRow, (*it).m_column, sourceParent);
    12. switch ((*it).m_action)
    13. {
    14. case FilterAction_Has:
    15. if (sourceModel()->data(index).toString().contains((*it).m_value) == false)
    16. accept = false;
    17. break;
    18. case FilterAction_Is:
    19. if ((sourceModel()->data(index).toString() == (*it).m_value.pattern()) == false)
    20. accept = false;
    21. break;
    22. case FilterAction_LessThan:
    23. if ((sourceModel()->data(index).toDouble() < (*it).m_value.pattern().toDouble()) == false)
    24. accept = false;
    25. break;
    26. case FilterAction_MoreThan:
    27. if ((sourceModel()->data(index).toDouble() > (*it).m_value.pattern().toDouble()) == false)
    28. accept = false;
    29. break;
    30. default:
    31. break;
    32. }
    33. }
    34.  
    35. for (it = orList.begin(); it != orList.end(); it++)
    36. {
    37. index = sourceModel()->index(sourceRow, (*it).m_column, sourceParent);
    38. switch ((*it).m_action)
    39. {
    40. case FilterAction_Has:
    41. if (sourceModel()->data(index).toString().contains((*it).m_value))
    42. accept = true;
    43. break;
    44. case FilterAction_Is:
    45. if ((sourceModel()->data(index).toString() == (*it).m_value.pattern()))
    46. accept = true;
    47. break;
    48. case FilterAction_LessThan:
    49. if ((sourceModel()->data(index).toDouble() < (*it).m_value.pattern().toDouble()))
    50. accept = true;
    51. break;
    52. case FilterAction_MoreThan:
    53. if ((sourceModel()->data(index).toDouble() > (*it).m_value.pattern().toDouble()))
    54. accept = true;
    55. break;
    56. default:
    57. break;
    58. }
    59. }
    60. return accept;
    61. }
    To copy to clipboard, switch view to plain text mode 

    I know it could be better, i.e. to use QString in the filter and convert it to QRegExp if needed, not the other way around, also, the if's themselves are not quite needed.
    But i don't really see, what can cause that behaviour that i described previously...

  10. #10
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    Any ideas fresh in the morning perhaps?

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

    Default Re: Custom proxy model issue

    The code seems ok, provided those two for loops work as you expect them to work. Which version of Qt are you using? I remember there were some issues about missing items related to the proxy back around Qt 4.2.

    BTW. Your and-or mechanism is a bit ambigous. Does X and Y or Z mean (X and Y) or Z or X and (Y or Z)? Because you can only represent the first one using your approach. Storing everything in a tree-like structure would be more flexible. You could then even mix ands and ors - (A and (B or C)) or (D and E).

  12. #12
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    I am using 4.3.2.

    ( About the implementation, you are absolutely right. Using a tree with and/or as nodes and the filters themselves as leaves, then using inorder traversing to interpret the advanced filter would be a much better approach, and one i will definitely consider.

    However, sadly, i dont think this influences the problem which i described. )

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

    Default Re: Custom proxy model issue

    I suggest you insert some qDebug() statements here and there and use them to pinpoint what exactly happens. If you know what happens, it'll be easier to find a solution.

  14. #14
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Custom proxy model issue

    I will most certainly try Wysota, though i dont really know what to watch out for.

    Thanks for your assistance, i appreciate it.

Similar Threads

  1. Custom Model Help PLz
    By Simz in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2007, 15:28
  2. Help with getting my custom model working
    By thomaspu in forum Qt Programming
    Replies: 19
    Last Post: 29th July 2007, 18:35
  3. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54
  4. Model and Proxy
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2006, 21:05
  5. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 11:03

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.