Results 1 to 4 of 4

Thread: setRecursiveFilteringEnabled() not available in qt4

  1. #1
    Join Date
    Jul 2021
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default setRecursiveFilteringEnabled() not available in qt4

    Hi everyone!

    I have implemented a Hierarchical table using the QTreeView in Qt5.
    I am using the QSortFilterProxyModel() to implement the search feature in the QTreeView. It is working okay in Qt5.

    Qt Code:
    1. proxyModel = new QSortFilterProxyModel(this);
    2. proxyModel->setSourceModel(model_t);
    3. proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    4. proxyModel->setFilterKeyColumn(0);
    5. ui->treeView->setModel(proxyModel);
    6. ui->treeView->setSortingEnabled(true);
    7. proxyModel->setRecursiveFilteringEnabled(true);
    8. ui->treeView->expandAll();
    To copy to clipboard, switch view to plain text mode 

    But now I want to run my code in the Qt4. However, I am getting the error that Qt 4 doesn't recognize "proxyModel->setRecursiveFilteringEnabled(true);" .

    Is there any alternative for "proxyModel->setRecursiveFilteringEnabled(true);" in Qt 4?

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setRecursiveFilteringEnabled() not available in qt4

    Do the recursive filtering by your own by deriving from QSortFilterProxyModel and reimplement filterAccetsRow()

  3. #3
    Join Date
    Jul 2021
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setRecursiveFilteringEnabled() not available in qt4

    Quote Originally Posted by ChristianEhrlicher View Post
    Do the recursive filtering by your own by deriving from QSortFilterProxyModel and reimplement filterAccetsRow()
    Sorry to ask you a silly question. But I couldn't understand well the working of Qsortfilterproxymodel.
    Do you think if I reimplement the filterAcceptsRow() using the following code would it work. I have to search the QtreeView based on the 0 column entries.
    Or do I have to modify the other function as well?


    Qt Code:
    1. bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
    2. const QModelIndex &sourceParent) const
    3. {
    4. QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    5. QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
    6. QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
    7.  
    8. return (sourceModel()->data(index0).toString().contains(filterRegExp())
    9. || sourceModel()->data(index1).toString().contains(filterRegExp()))
    10. && dateInRange(sourceModel()->data(index2).toDate());
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setRecursiveFilteringEnabled() not available in qt4

    You've just copied and pasted the example from the documentation. If your columns don't contain the same information, or if you only want to look at column 0, then of course it won't work. It also doesn't implement recursive filtering, so it certainly won't do what the Qt5 version does.

    In your initial post, you don't set any regular expression, so what exactly are you filtering based on? The default behavior of the proxy when there is no regexp is to accept everything.

    Remember that the default behavior of QSortFilterProxyModel is to stop recursing as soon as filterAcceptsRow() returns false for any row. So even if a child row matches, if the parent doesn't the child row will never be examined and the parent row will not be displayed.

    This is the opposite behavior that occurs when setRecursiveFilteringEnabled() is set to true. In that case, if any child row passes the test, then you want the parent to be displayed.

    So if you want to implement recursive filtering the way it works in Qt5, you have to do the recursion yourself.

    If you are using a QRegExp to hold the filter string, then your version of filterAcceptsRow() should look like this:

    *** WARNING: Untested code. Might be buggy. ***
    Qt Code:
    1. /*
    2.  * Implementation of manual recursive filtering for Qt < 5
    3.  *
    4. */
    5.  
    6. bool MySortFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex & sourceParent ) const
    7. {
    8. #if QT_VERSION < 0x050000
    9.  
    10. // Qt is less than 5, so manually perform recursion
    11.  
    12. // Default QSortFilterProxyModel behavior for an empty regexp
    13. if ( filterRegExp().isEmpty() )
    14. return true;
    15.  
    16. QModelIndex index0 = sourceModel()->index( sourceRow, 0, sourceParent );
    17.  
    18. // If column0 at the current level matches, return true
    19. if ( sourceModel()->data( index0, Qt::DisplayRole ).toString().contains( filterRegExp() ) )
    20. return true;
    21.  
    22. // The current level doesn't match but if it has children, recurse to look at them
    23. else if ( sourceModel()->hasChildren( index0 ) )
    24. {
    25. bool bResult = false;
    26. int nRows = sourceModel()->rowCount( index0 );
    27.  
    28. // Look at each child row. If any one of them matches (bResult == true) then the loop will exit
    29. for ( int nRow = 0; nRow < nRows && !bResult; ++nRow )
    30. {
    31. bResult = filterAcceptsRow( nRow, index0 );
    32. }
    33.  
    34. // Return whatever the loop result was. If any child matches, this is true, otherwise it is false.
    35. return bResult;
    36. }
    37.  
    38. // Otherwise, nothing at this level or below it matches, so the whole subtree is ignored.
    39. return false;
    40.  
    41. #else
    42. // Qt version is > 4, so use default implementation
    43. return QSortFilterProxyModel::filterAcceptsRow( sourceRow, sourceParent );
    44.  
    45. #endif
    46. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Tags for this Thread

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.