Results 1 to 2 of 2

Thread: Optimizing filterAcceptsRow() to filter a tree

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Optimizing filterAcceptsRow() to filter a tree

    I think you have to scan all the children... What you can do is implement a kind of cache - with each item associate the last searched expression and the return value for that expression, then there won't be any big overhead on the search as each item would be actually searched once and then the cached value will be returned. Pseudocode follows...

    Qt Code:
    1. bool itemMatchesExpression(const QModelIndex &ind, const QRegExp &exp){
    2. if(exp==cache(index).expression) return cache(index).value);
    3. cache(index).expression = exp;
    4. bool v = false;
    5. foreach(QModelIndex i, ind.children()){
    6. v = itemMatchesExpression(i, exp);
    7. if(v) break;
    8. }
    9. cache(index).value = v;
    10. return cache(index).value;
    11. }
    12.  
    13. bool Model::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{
    14. return itemMatchesExpression(index(0, sourceRow, sourceParent), expression);
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    vfernandez (4th January 2007)

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.