Results 1 to 2 of 2

Thread: QDirModel::setNameFilters

  1. #1
    Join Date
    Jan 2006
    Location
    Wells, Somerset, UK
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Question QDirModel::setNameFilters

    Hi,

    Has anyone out there used
    Qt Code:
    1. QDirModel::setNameFilters(const QStringList &filters)
    To copy to clipboard, switch view to plain text mode 
    ?

    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:

    Qt Code:
    1. mpHostPathModel->setNameFilters(QStringList() << "*");
    To copy to clipboard, switch view to plain text mode 

    but an attempt to turn it on with:

    Qt Code:
    1. mpHostPathModel->setNameFilters(QStringList() << "*.jpg");
    To copy to clipboard, switch view to plain text mode 

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

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

    Thanks,
    Andy.

  2. #2
    Join Date
    Jan 2006
    Location
    Wells, Somerset, UK
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: QDirModel::setNameFilters

    In case anyone's interested, I found an alternative method using QSortFilterProxyModel!

    Created a class based on the proxy model
    header:

    Qt Code:
    1. class QHostProxyModel : public QSortFilterProxyModel
    2. {
    3. Q_OBJECT
    4. public:
    5. QHostProxyModel(QObject *parent = 0);
    6.  
    7. inline void setFilterList(const QStringList &filter)
    8. { mFilterList = filter; };
    9.  
    10. protected:
    11. virtual bool filterAcceptsRow(int source_row,
    12. const QModelIndex &source_parent) const;
    13.  
    14. private:
    15. QStringList mFilterList;
    16. };
    To copy to clipboard, switch view to plain text mode 

    source:

    Qt Code:
    1. QHostProxyModel::QHostProxyModel(QObject *parent)
    2. , mFilterList()
    3. {
    4. }
    5.  
    6. bool QHostProxyModel::filterAcceptsRow(int source_row,
    7. const QModelIndex &source_parent) const
    8. {
    9. QDirModel *pSourceModel = (QDirModel *)sourceModel();
    10. QModelIndex index = pSourceModel->index(source_row, 0, source_parent);
    11.  
    12. if( pSourceModel->isDir(index) )
    13. {
    14. return TRUE;
    15. }
    16.  
    17. if( mFilterList.count() == 0 )
    18. {
    19. return TRUE;
    20. }
    21.  
    22.  
    23. QString filterIndex;
    24. foreach( filterIndex, mFilterList )
    25. {
    26. QString fileType = pSourceModel->
    27. data(index).toString().right(filterIndex.length()).toLower();
    28. if( fileType == filterIndex )
    29. {
    30. return TRUE;
    31. }
    32. }
    33.  
    34. return FALSE;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Then created the model/view using:

    Qt Code:
    1. mpHostPathModel = new QDirModel;
    2. mpHostProxyModel = new QHostProxyModel(this);
    3. mpHostProxyModel->setFilterList(QStringList() << ".jpg");
    4. mpHostProxyModel->setSourceModel(mpHostPathModel);
    5. ui.mpHostDriveView->setModel(mpHostProxyModel);
    To copy to clipboard, switch view to plain text mode 

    Hope that helps anyone with similar problems,
    Andy.

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
  •  
Qt is a trademark of The Qt Company.