Results 1 to 10 of 10

Thread: limit QDirModel file type

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default limit QDirModel file type

    Can I set QDirModel to only "see" file types I specify?

  2. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: limit QDirModel file type

    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:
    1. void CustomView::hideFiles(const QModelIndex & modelIndex)
    2. {
    3. for ( int x = 0; x < dirModel->rowCount(modelIndex); ++x) {
    4. QModelIndex index = dirModel->index(x, 0, modelIndex);
    5. if ( !dirModel->isDir(index) )
    6. setRowHidden(x, modelIndex,true);
    7. }
    8. }
    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

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: limit QDirModel file type

    But how would I choose only files with, say, .clx ; .mp3 etc...?

  4. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: limit QDirModel file type

    the follow code filter the MP3 files in a custom view class.

    .
    Qt Code:
    1. void CustomView::OnlyShowMp3Files(const QModelIndex & modelIndex)
    2. {
    3. for ( int x = 0; x < model()->rowCount(modelIndex); ++x) {
    4. QModelIndex index = model()->index(x, 0, modelIndex);
    5. if ( ! model()->fileName(index).endsWith(".mp3", Qt::CaseInsensitive) )
    6. setRowHidden(x, modelIndex,true);
    7. }
    8. }
    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:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QFileSystemModel model;
    8. model.setRootPath("");
    9. QStringList filters;
    10. filters << "*.mp3" << "*.mp4" ;
    11. model.setNameFilters ( filters );
    12. model.setNameFilterDisables(false); //hide the file who dont pass the filter
    13.  
    14. QTreeView tree;
    15. tree.setModel(&model);
    16. tree.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ecanela; 24th November 2009 at 06:18. Reason: updated contents

  5. #5
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: limit QDirModel file type

    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.

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: limit QDirModel file type

    Qt Code:
    1. QFileSystemModel alarmDirModel;
    2. QStringList fileTypes;
    3.  
    4. fileTypes << ".xml" ;
    5.  
    6. alarmDirModel.setNameFilters(fileTypes);
    7. alarmDirModel.setNameFilterDisables(false);
    8. alarmDirModel.setRootPath(QDir::currentPath());
    9. ui->alarmList->setModel(&alarmDirModel);
    To copy to clipboard, switch view to plain text mode 
    Using the above code shows only the computers root path. It doesn't show the current directory.

  7. #7
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: limit QDirModel file type

    Qt Code:
    1. QDirModel alarmDirModel;
    2. QStringList fileTypes;
    3.  
    4. fileTypes << ".xml" ;
    5.  
    6. alarmDirModel.setNameFilters(fileTypes);
    7. ui->alarmList->setModel(&alarmDirModel);
    8. ui->alarmList->setRootIndex(alarmDirModel.index(QDir::currentPath()));
    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.

  8. #8
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: limit QDirModel file type

    use the following code

    [QUOTE=been_1990;124478]
    Qt Code:
    1. QDirModel alarmDirModel;
    2. QStringList fileTypes;
    3.  
    4. fileTypes << "*.xml" ;
    5.  
    6. alarmDirModel.setNameFilters(fileTypes);
    7. ui->alarmList->setModel(&alarmDirModel);
    8. ui->alarmList->setRootIndex(alarmDirModel.index(QDir::currentPath()));
    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

  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: limit QDirModel file type

    Ok, thanks for the * tip. I never really learned regular expressions.

  10. #10
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: limit QDirModel file type

    Can I also remove the extension from being displayed? Like, "all indexes that end with .xml, remove .xml"?

Similar Threads

  1. Replies: 5
    Last Post: 5th August 2009, 17:32
  2. getting a file extenstion list from a MIME type
    By roxton in forum Qt Programming
    Replies: 0
    Last Post: 10th April 2009, 18:27
  3. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. QDirModel, Model/View, extend the file onfo
    By VlJE in forum Qt Programming
    Replies: 10
    Last Post: 11th December 2006, 10:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.