Results 1 to 7 of 7

Thread: QSortFilterPoxyModel not filtering

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QSortFilterPoxyModel not filtering

    Hi there, I have a QListView that displays pdf files from a Directory. To achieve this, I linked the QListView to the QFileSystemModel. My problem is that I need to be able to search for a file in the QListView using a pattern string, so I employed the use of a QSortFilterProxyModel. The QSortFilterProxyModel works fine except that it doesn't filter when I search for a particular file. Please see my code below.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->listView_Main->setFlow(QListView::TopToBottom);
    8. ui->listView_Main->setViewMode(QListView::ListMode);
    9. ui->listView_Main->setWrapping(true);
    10. ui->listView_Main->setResizeMode(QListView::Adjust);
    11. ui->listView_Main->setUniformItemSizes(true);
    12.  
    13. QString root_path = "C:/Users/C5248134/Desktop/Projects/build-Ithala-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug/Documents/";
    14.  
    15. proxyModel = new QSortFilterProxyModel(this);
    16.  
    17. f_model = new QFileSystemModel(this);
    18. f_model->setFilter(QDir::Files);
    19.  
    20. proxyModel->setSourceModel(f_model);
    21. proxyModel->setFilterRole(Qt::DisplayRole | Qt::DecorationRole);
    22.  
    23. if(f_model->rootDirectory().exists())
    24. qDebug() << "Dir Found" <<endl;
    25. else
    26. qDebug() << "Dir not found" <<endl;
    27.  
    28. ui->listView_Main->setModel(proxyModel);
    29. ui->listView_Main->setRootIndex(proxyModel->mapFromSource(f_model->setRootPath(root_path)));
    30. }
    To copy to clipboard, switch view to plain text mode 

    Search button clicked slot.
    Qt Code:
    1. void MainWindow::on_btnSearch_clicked()
    2. {
    3. QString patternStr = ui->leSearch->text();
    4. QFileInfoList fileInfoObj = f_model->rootDirectory().entryInfoList();
    5.  
    6. proxyModel->setFilterFixedString(patternStr);
    7. //ui->listView_Main->setRootIndex();
    8. }
    To copy to clipboard, switch view to plain text mode 
    When I type-in a search string and then click on the search button the entire QListView goes blank. Please see screenshots of how my app looks before and after I search for a file.
    Beforescreen2.jpg AfterBlankPic.jpg

  2. #2
    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: QSortFilterPoxyModel not filtering

    If you look at your filter role definition, you will see that you are effectively filtering on Qt::DecorationRole as Qt::DisplayRole is 0.

    And the decoration is the icon, which won't match any string.

    You might want to filter on the DisplayRole or the FileNameRole

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortFilterPoxyModel not filtering

    Quote Originally Posted by anda_skoa View Post
    You might want to filter on the DisplayRole or the FileNameRole
    _
    I tried that and it didn't work. According to the documentation, the role is by default Qt:isplayRole

  4. #4
    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: QSortFilterPoxyModel not filtering

    Quote Originally Posted by ayanda83 View Post
    I tried that and it didn't work.
    Tried with one of the other filter methods?

    Quote Originally Posted by ayanda83 View Post
    According to the documentation, the role is by default Qt:isplayRole
    But you set it to DecorationRole, which is not a string.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortFilterPoxyModel not filtering

    Quote Originally Posted by anda_skoa View Post
    Tried with one of the other filter methods?


    But you set it to DecorationRole, which is not a string.

    Cheers,
    _
    I set it to both DecorationRole and DisplayRole so that I can display both the file icon and the file name. I also tried to display the file name only by setting it to DisplayRole only and that didn't work either.

    If you look at my slot below, is it suppose to do what I want it to do? Because I am not too sure.

    Qt Code:
    1. void MainWindow::on_btnSearch_clicked()
    2. {
    3. QString patternStr = ui->leSearch->text();
    4. proxyModel->setFilterFixedString(patternStr);
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: QSortFilterPoxyModel not filtering

    Quote Originally Posted by ayanda83 View Post
    I set it to both DecorationRole and DisplayRole so that I can display both the file icon and the file name.
    No.
    First, the filter role has obviously nothing to do with display.
    Second, the filter role is only a single role.
    Third, a binary OR between two non-flag values is seldomly a valid value.
    Fourth, in you case you where lucky as one of the value is 0 so the result of the OR is the second value.

    So no, you did not set both DecorationRole and DisplayRole, as this is impossible and by sheer luck you only set DecorationRole.

    Quote Originally Posted by ayanda83 View Post
    I also tried to display the file name only by setting it to DisplayRole only and that didn't work either.
    And you also tried with FileNameRole?

    Quote Originally Posted by ayanda83 View Post
    If you look at my slot below, is it suppose to do what I want it to do? Because I am not too sure.

    Qt Code:
    1. void MainWindow::on_btnSearch_clicked()
    2. {
    3. QString patternStr = ui->leSearch->text();
    4. proxyModel->setFilterFixedString(patternStr);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Yes, that looks reasonable.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortFilterPoxyModel not filtering

    I set the display role to Qt:isplay only and the program does filter certain strings. For example the string "TW-" is filtered perfectly fine but other strings don't filter or they crash the program. I am pretty sure the problem is with line 29 in my constructor but I am not really sure what I need to do there.

Similar Threads

  1. Filtering iterators
    By akiross in forum Qt Programming
    Replies: 18
    Last Post: 2nd September 2011, 00:08
  2. Event filtering
    By Cruz in forum Qt Programming
    Replies: 6
    Last Post: 28th March 2011, 12:35
  3. Key filtering
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2010, 09:10
  4. Filtering QSqlQueryModel
    By mourad in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2009, 09:32
  5. Filtering a TreeWidget
    By soul_rebel in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2008, 20:08

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.