PDA

View Full Version : QDirModel::setNameFilters



Smudge
7th July 2006, 11:44
Hi,

Has anyone out there used
QDirModel::setNameFilters(const QStringList &filters)?

I have been trying to set a basic toggle filter on my View to display directories and jpegs in a given path, turning the filter off seems to work with:


mpHostPathModel->setNameFilters(QStringList() << "*");

but an attempt to turn it on with:


mpHostPathModel->setNameFilters(QStringList() << "*.jpg");

Causes the view to reset to the root directory with only a few directories available :confused:

Has anyone used it with success, can anyone give examples of how to use it as the helptext is pretty vague.

Thanks,
Andy.

Smudge
10th July 2006, 14:47
In case anyone's interested, I found an alternative method using QSortFilterProxyModel!

Created a class based on the proxy model
header:


class QHostProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
QHostProxyModel(QObject *parent = 0);

inline void setFilterList(const QStringList &filter)
{ mFilterList = filter; };

protected:
virtual bool filterAcceptsRow(int source_row,
const QModelIndex &source_parent) const;

private:
QStringList mFilterList;
};

source:


QHostProxyModel::QHostProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
, mFilterList()
{
}

bool QHostProxyModel::filterAcceptsRow(int source_row,
const QModelIndex &source_parent) const
{
QDirModel *pSourceModel = (QDirModel *)sourceModel();
QModelIndex index = pSourceModel->index(source_row, 0, source_parent);

if( pSourceModel->isDir(index) )
{
return TRUE;
}

if( mFilterList.count() == 0 )
{
return TRUE;
}


QString filterIndex;
foreach( filterIndex, mFilterList )
{
QString fileType = pSourceModel->
data(index).toString().right(filterIndex.length()) .toLower();
if( fileType == filterIndex )
{
return TRUE;
}
}

return FALSE;
}

Then created the model/view using:



mpHostPathModel = new QDirModel;
mpHostProxyModel = new QHostProxyModel(this);
mpHostProxyModel->setFilterList(QStringList() << ".jpg");
mpHostProxyModel->setSourceModel(mpHostPathModel);
ui.mpHostDriveView->setModel(mpHostProxyModel);


Hope that helps anyone with similar problems,
Andy.