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.
Code:
proxyModel->setDynamicSortFilter(true);
proxyModel->setSourceModel(&m_filesystem);
m_ui.v_files->setModel(proxyModel); //QListView
and then just doing this should work, i guess:
Code:
proxyModel->setFilterFixedString(m_toolbarSearch->text());
But it doesn't, it seems it's just filtering the first element.
Any ideas?
Thanks
Re: QSortFilterProxyModel not filtering properly
Re: QSortFilterProxyModel not filtering properly
I used this way:
Code:
proxyModel->setFilterKeyColumn(-1);
But the same problem persists.
Re: QSortFilterProxyModel not filtering properly
Why don't you try giving some sample data and the expected result?
Re: QSortFilterProxyModel not filtering properly
Quote:
Originally Posted by
armonge
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.
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
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.
Re: QSortFilterProxyModel not filtering properly
Quote:
Originally Posted by
freemind
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.
Re: QSortFilterProxyModel not filtering properly
Hi again,
app.cpp
Code:
#include "app.hpp"
{
m_ui.setupUi(this);
model = new QFileSystemModel(this);
model
->setRootPath
(QDir::currentPath());
proxyModel->setDynamicSortFilter(true);
proxyModel->setSourceModel(model);
proxyModel
->mapFromSource
(model
->index
(QDir::currentPath()));
m_ui.listView->setModel(proxyModel);
m_ui.
listView->setRootIndex
(proxyModel
->mapFromSource
(model
->index
(QDir::currentPath())));
connect(m_ui.
lineEdit,
SIGNAL(textChanged
(QString)),
this,
SLOT(filter
(QString)));
}
void App
::filter(QString pattern_filter
) {
proxyModel->setFilterFixedString(pattern_filter);
}
app.hpp:
Code:
#ifndef _HPP_APP
#define _HPP_APP
#include <QFileSystemModel>
#include <QMainWindow>
#include <QSortFilterProxyModel>
#include <QFileSystemModel>
#include <QDir>
#include <QTextStream>
#include "ui_app.h"
{
Q_OBJECT
public:
public slots:
private:
Ui::MainWindow m_ui;
QFileSystemModel *model;
};
#endif
ui_app.h:
Code:
/********************************************************************************
** Form generated from reading UI file 'app.ui'
**
** Created: Sat Aug 7 21:35:40 2010
** by: Qt User Interface Compiler version 4.6.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_APP_H
#define UI_APP_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QListView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
{
if (MainWindow->objectName().isEmpty())
MainWindow
->setObjectName
(QString::fromUtf8("MainWindow"));
MainWindow->resize(686, 402);
centralwidget
= new QWidget(MainWindow
);
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
verticalLayout
->setObjectName
(QString::fromUtf8("verticalLayout"));
lineEdit
->setObjectName
(QString::fromUtf8("lineEdit"));
verticalLayout->addWidget(lineEdit);
listView
->setObjectName
(QString::fromUtf8("listView"));
verticalLayout->addWidget(listView);
MainWindow->setCentralWidget(centralwidget);
menubar
->setObjectName
(QString::fromUtf8("menubar"));
menubar
->setGeometry
(QRect(0,
0,
686,
25));
MainWindow->setMenuBar(menubar);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
MainWindow->setStatusBar(statusbar);
toolBar
->setObjectName
(QString::fromUtf8("toolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
retranslateUi(MainWindow);
} // setupUi
{
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_APP_H
main.cpp
Code:
#include <QtGui/QApplication>
#include "app.hpp"
int main(int argc, char ** argv)
{
App myapp;
myapp.show();
return app.exec();
}
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.