PDA

View Full Version : limit QDirModel file type



been_1990
22nd November 2009, 23:31
Can I set QDirModel to only "see" file types I specify?

ecanela
23rd November 2009, 05:46
one way for resolve your problem is using a custom view. in the custom view only need hide the files who don´t need show.

for example. the follow code hide all the files, and only show the Dirs



void CustomView::hideFiles(const QModelIndex & modelIndex)
{
for ( int x = 0; x < dirModel->rowCount(modelIndex); ++x) {
QModelIndex index = dirModel->index(x, 0, modelIndex);
if ( !dirModel->isDir(index) )
setRowHidden(x, modelIndex,true);
}
}

or you can use a QSortFilterProxyModel to filter the QDirModel.

PS. sorry my poor english, my natural lenguage is spanish :o

been_1990
23rd November 2009, 23:11
But how would I choose only files with, say, .clx ; .mp3 etc...?

ecanela
24th November 2009, 04:23
the follow code filter the MP3 files in a custom view class.

.


void CustomView::OnlyShowMp3Files(const QModelIndex & modelIndex)
{
for ( int x = 0; x < model()->rowCount(modelIndex); ++x) {
QModelIndex index = model()->index(x, 0, modelIndex);
if ( ! model()->fileName(index).endsWith(".mp3", Qt::CaseInsensitive) )
setRowHidden(x, modelIndex,true);
}
}

another way to filter the files are using a QFileSystemModel
this a minimal and complete example.


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFileSystemModel model;
model.setRootPath("");
QStringList filters;
filters << "*.mp3" << "*.mp4" ;
model.setNameFilters ( filters );
model.setNameFilterDisables(false); //hide the file who dont pass the filter

QTreeView tree;
tree.setModel(&model);
tree.show();
return app.exec();
}

Jennie Bystrom
24th November 2009, 11:43
Take a look at the method setNameFilters in QDirModel. It lets you specify a filter for what should be shown, for example *.xml, *.mp3 and so on.
You won't need a custom view for this.

been_1990
30th November 2009, 14:35
QFileSystemModel alarmDirModel;
QStringList fileTypes;

fileTypes << ".xml" ;

alarmDirModel.setNameFilters(fileTypes);
alarmDirModel.setNameFilterDisables(false);
alarmDirModel.setRootPath(QDir::currentPath());
ui->alarmList->setModel(&alarmDirModel);
Using the above code shows only the computers root path. It doesn't show the current directory.

been_1990
30th November 2009, 14:40
QDirModel alarmDirModel;
QStringList fileTypes;

fileTypes << ".xml" ;

alarmDirModel.setNameFilters(fileTypes);
ui->alarmList->setModel(&alarmDirModel);
ui->alarmList->setRootIndex(alarmDirModel.index(QDir::currentPath ()));

When I add the line " alarmDirModel.setNameFilters(fileTypes);" nothing is shown. There are several XML files at the current directory, but not shown.

ecanela
30th November 2009, 19:03
use the following code

[QUOTE=been_1990;124478]

QDirModel alarmDirModel;
QStringList fileTypes;

fileTypes << "*.xml" ;

alarmDirModel.setNameFilters(fileTypes);
ui->alarmList->setModel(&alarmDirModel);
ui->alarmList->setRootIndex(alarmDirModel.index(QDir::currentPath ()));


only add the asterisk in the filter. You need express the filter as regular expression, "*.xml" instead of ".xml",

".xml" match only whit the file named ".xml"
*.xml" match whit files who has the suffix "xml",

another thing. the function setNameFilters, only disables the files, not hidden. (this in for posted code using the QDirModel class)

In the code when using QFileSystemModel, you need call the function setRootPath(); to populate the model.

been_1990
2nd December 2009, 19:50
Ok, thanks for the * tip. I never really learned regular expressions.

been_1990
2nd December 2009, 19:58
Can I also remove the extension from being displayed? Like, "all indexes that end with .xml, remove .xml"?