Results 1 to 10 of 10

Thread: QSortFilterProxyModel not filtering properly

  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSortFilterProxyModel not filtering properly

    Hi,

    When i try to filter items with a QSortFilterProxyModel it doesn't work properly.I've been trying to solve this problem, but i can't figure out what i am missing.

    Qt Code:
    1. proxyModel = new QSortFilterProxyModel(this);
    2. proxyModel->setDynamicSortFilter(true);
    3. proxyModel->setSourceModel(&m_filesystem);
    4. m_ui.v_files->setModel(proxyModel); //QListView
    To copy to clipboard, switch view to plain text mode 

    and then just doing this should work, i guess:
    Qt Code:
    1. proxyModel->setFilterFixedString(m_toolbarSearch->text());
    To copy to clipboard, switch view to plain text mode 

    But it doesn't, it seems it's just filtering the first element.
    Any ideas?

    Thanks

  2. #2
    Join Date
    May 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel not filtering properly

    Try setting filterKeyColumn

  3. #3
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel not filtering properly

    I used this way:
    Qt Code:
    1. proxyModel->setFilterKeyColumn(-1);
    To copy to clipboard, switch view to plain text mode 

    But the same problem persists.

  4. #4
    Join Date
    May 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel not filtering properly

    Why don't you try giving some sample data and the expected result?

  5. #5
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel not filtering properly

    Quote Originally Posted by armonge View Post
    Why don't you try giving some sample data and the expected result?
    Fair enough. The application i'm making is a file browser, so imagine a directory with the folders' names from A to Z. If i filter for 'A' (the first element) it hides the others and only shows the folder 'A', but if i try to filter for 'B' or other, i get no results.

  6. #6
    Join Date
    May 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel not filtering properly

    For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.
    http://doc.qt.nokia.com/4.6/qsortfilterproxymodel.html

  7. #7
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel not filtering properly

    But i'm only showing one "folder level" at time... so the parent/children iissue you described shouldn't affect it.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSortFilterProxyModel not filtering properly

    Quote Originally Posted by freemind View Post
    Fair enough. The application i'm making is a file browser, so imagine a directory with the folders' names from A to Z. If i filter for 'A' (the first element) it hides the others and only shows the folder 'A', but if i try to filter for 'B' or other, i get no results.
    You are going to have to post a small compilable example of what you are actually doing.

  9. #9
    Join Date
    Jul 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel not filtering properly

    Hi again,

    app.cpp
    Qt Code:
    1. #include "app.hpp"
    2.  
    3. App::App(QWidget * p_parent): QMainWindow(p_parent)
    4. {
    5. m_ui.setupUi(this);
    6.  
    7. model = new QFileSystemModel(this);
    8. model->setRootPath(QDir::currentPath());
    9. proxyModel = new QSortFilterProxyModel(this);
    10. proxyModel->setDynamicSortFilter(true);
    11. proxyModel->setSourceModel(model);
    12. proxyModel->mapFromSource(model->index(QDir::currentPath()));
    13.  
    14. m_ui.listView->setModel(proxyModel);
    15.  
    16. m_ui.listView->setRootIndex(proxyModel->mapFromSource(model->index(QDir::currentPath())));
    17.  
    18. connect(m_ui.lineEdit, SIGNAL(textChanged(QString)), this, SLOT(filter(QString)));
    19.  
    20. }
    21.  
    22. void App::filter(QString pattern_filter)
    23. {
    24. proxyModel->setFilterFixedString(pattern_filter);
    25. }
    To copy to clipboard, switch view to plain text mode 

    app.hpp:
    Qt Code:
    1. #ifndef _HPP_APP
    2. #define _HPP_APP
    3.  
    4. #include <QFileSystemModel>
    5. #include <QMainWindow>
    6. #include <QSortFilterProxyModel>
    7. #include <QFileSystemModel>
    8. #include <QDir>
    9. #include <QTextStream>
    10. #include "ui_app.h"
    11.  
    12.  
    13. class App : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. App(QWidget * p_parent = 0);
    19.  
    20. public slots:
    21. void filter(QString );
    22.  
    23. private:
    24. Ui::MainWindow m_ui;
    25. QSortFilterProxyModel *proxyModel;
    26. QFileSystemModel *model;
    27.  
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    ui_app.h:
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading UI file 'app.ui'
    3. **
    4. ** Created: Sat Aug 7 21:35:40 2010
    5. ** by: Qt User Interface Compiler version 4.6.2
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling UI file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_APP_H
    11. #define UI_APP_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QHeaderView>
    18. #include <QtGui/QLineEdit>
    19. #include <QtGui/QListView>
    20. #include <QtGui/QMainWindow>
    21. #include <QtGui/QMenuBar>
    22. #include <QtGui/QStatusBar>
    23. #include <QtGui/QToolBar>
    24. #include <QtGui/QVBoxLayout>
    25. #include <QtGui/QWidget>
    26.  
    27. QT_BEGIN_NAMESPACE
    28.  
    29. class Ui_MainWindow
    30. {
    31. public:
    32. QWidget *centralwidget;
    33. QVBoxLayout *verticalLayout;
    34. QLineEdit *lineEdit;
    35. QListView *listView;
    36. QMenuBar *menubar;
    37. QStatusBar *statusbar;
    38. QToolBar *toolBar;
    39.  
    40. void setupUi(QMainWindow *MainWindow)
    41. {
    42. if (MainWindow->objectName().isEmpty())
    43. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    44. MainWindow->resize(686, 402);
    45. centralwidget = new QWidget(MainWindow);
    46. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    47. verticalLayout = new QVBoxLayout(centralwidget);
    48. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    49. lineEdit = new QLineEdit(centralwidget);
    50. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    51.  
    52. verticalLayout->addWidget(lineEdit);
    53.  
    54. listView = new QListView(centralwidget);
    55. listView->setObjectName(QString::fromUtf8("listView"));
    56.  
    57. verticalLayout->addWidget(listView);
    58.  
    59. MainWindow->setCentralWidget(centralwidget);
    60. menubar = new QMenuBar(MainWindow);
    61. menubar->setObjectName(QString::fromUtf8("menubar"));
    62. menubar->setGeometry(QRect(0, 0, 686, 25));
    63. MainWindow->setMenuBar(menubar);
    64. statusbar = new QStatusBar(MainWindow);
    65. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    66. MainWindow->setStatusBar(statusbar);
    67. toolBar = new QToolBar(MainWindow);
    68. toolBar->setObjectName(QString::fromUtf8("toolBar"));
    69. MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
    70.  
    71. retranslateUi(MainWindow);
    72.  
    73. QMetaObject::connectSlotsByName(MainWindow);
    74. } // setupUi
    75.  
    76. void retranslateUi(QMainWindow *MainWindow)
    77. {
    78. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    79. toolBar->setWindowTitle(QApplication::translate("MainWindow", "toolBar", 0, QApplication::UnicodeUTF8));
    80. } // retranslateUi
    81.  
    82. };
    83.  
    84. namespace Ui {
    85. class MainWindow: public Ui_MainWindow {};
    86. } // namespace Ui
    87.  
    88. QT_END_NAMESPACE
    89.  
    90. #endif // UI_APP_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "app.hpp"
    3.  
    4.  
    5. int main(int argc, char ** argv)
    6. {
    7. QApplication app(argc, argv);
    8. App myapp;
    9. myapp.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSortFilterProxyModel not filtering properly

    Your QFileSystemModel is the entire filesystem, not just the sub-tree you are displaying. The filter is applied to the top element in the model, which on Linux is just the root (not sure on Windows), decides it doesn't match and excludes everything from the model.

    You should look at QFileSystemModel::nameFilters(), QFileSystemModel::nameFilterDisables, and QFileSystemModel::setFilter() to achieve your filtering.

Similar Threads

  1. Key filtering
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2010, 09:10
  2. Filtering QSqlQueryModel
    By mourad in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2009, 09:32
  3. Event Filtering on Widgets
    By QbelcorT in forum Newbie
    Replies: 1
    Last Post: 28th November 2008, 21:27
  4. Filtering a TreeWidget
    By soul_rebel in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2008, 20:08
  5. Replies: 1
    Last Post: 10th January 2008, 14:38

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.