Can I set QDirModel to only "see" file types I specify?
Printable View
Can I set QDirModel to only "see" file types I specify?
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
Code:
{ for ( int x = 0; x < dirModel->rowCount(modelIndex); ++x) { if ( !dirModel->isDir(index) ) setRowHidden(x, modelIndex,true); } }
or you can use a QSortFilterProxyModelto filter the QDirModel.
PS. sorry my poor english, my natural lenguage is spanish :o
But how would I choose only files with, say, .clx ; .mp3 etc...?
the follow code filter the MP3 files in a custom view class.
.
Code:
{ for ( int x = 0; x < model()->rowCount(modelIndex); ++x) { 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.
Code:
#include <QtGui> int main(int argc, char *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(); }
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.
Using the above code shows only the computers root path. It doesn't show the current directory.Code:
QFileSystemModel alarmDirModel; QStringList fileTypes; fileTypes << ".xml" ; alarmDirModel.setNameFilters(fileTypes); alarmDirModel.setNameFilterDisables(false); ui->alarmList->setModel(&alarmDirModel);
Code:
QDirModel alarmDirModel; QStringList fileTypes; fileTypes << ".xml" ; alarmDirModel.setNameFilters(fileTypes); ui->alarmList->setModel(&alarmDirModel);
When I add the line " alarmDirModel.setNameFilters(fileTypes);" nothing is shown. There are several XML files at the current directory, but not shown.
use the following code
[QUOTE=been_1990;124478]Code:
QDirModel alarmDirModel; QStringList fileTypes; fileTypes << "*.xml" ; alarmDirModel.setNameFilters(fileTypes); ui->alarmList->setModel(&alarmDirModel);
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.
Ok, thanks for the * tip. I never really learned regular expressions.
Can I also remove the extension from being displayed? Like, "all indexes that end with .xml, remove .xml"?