Can I set QDirModel to only "see" file types I specify?
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
Qt Code:
{ for ( int x = 0; x < dirModel->rowCount(modelIndex); ++x) { if ( !dirModel->isDir(index) ) setRowHidden(x, modelIndex,true); } }To copy to clipboard, switch view to plain text mode
or you can use a QSortFilterProxyModelto filter the QDirModel.
PS. sorry my poor english, my natural lenguage is spanish
Last edited by ecanela; 23rd November 2009 at 05:52. Reason: updated contents
But how would I choose only files with, say, .clx ; .mp3 etc...?
the follow code filter the MP3 files in a custom view class.
.
Qt Code:
{ for ( int x = 0; x < model()->rowCount(modelIndex); ++x) { if ( ! model()->fileName(index).endsWith(".mp3", Qt::CaseInsensitive) ) setRowHidden(x, modelIndex,true); } }To copy to clipboard, switch view to plain text mode
another way to filter the files are using a QFileSystemModel
this a minimal and complete example.
Qt 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(); }To copy to clipboard, switch view to plain text mode
Last edited by ecanela; 24th November 2009 at 06:18. Reason: updated contents
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.Qt Code:
QFileSystemModel alarmDirModel; QStringList fileTypes; fileTypes << ".xml" ; alarmDirModel.setNameFilters(fileTypes); alarmDirModel.setNameFilterDisables(false); ui->alarmList->setModel(&alarmDirModel);To copy to clipboard, switch view to plain text mode
Qt Code:
QDirModel alarmDirModel; QStringList fileTypes; fileTypes << ".xml" ; alarmDirModel.setNameFilters(fileTypes); ui->alarmList->setModel(&alarmDirModel);To copy to clipboard, switch view to plain text mode
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]Qt Code:
QDirModel alarmDirModel; QStringList fileTypes; fileTypes << "*.xml" ; alarmDirModel.setNameFilters(fileTypes); ui->alarmList->setModel(&alarmDirModel);To copy to clipboard, switch view to plain text mode
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.
Last edited by ecanela; 30th November 2009 at 19:12. Reason: updated contents
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"?
Bookmarks