zoukankan      html  css  js  c++  java
  • 自定义高级QFileDialog文件过滤器

    QFileDialog提供了一个函数---setproxyModel。。就是用这个来玩了。就是override filterAcceptsRow的虚函数,里面定制自己的过滤需求。返回bool   下面就是判断是否是目录,只显示目录文件夹。

     1 #ifndef PROXY_MODEL_H
     2 #define PROXY_MODEL_H
     3 
     4 #include <QSortFilterProxyModel>
     5 
     6 
     7 class FileFilterProxyModel : public QSortFilterProxyModel
     8 {
     9 public:
    10     FileFilterProxyModel(QObject* parent) :QSortFilterProxyModel(parent){}
    11 
    12 protected:
    13     virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
    14 };
    15 
    16 
    17 #endif
    #include "proxymodel.h"
    
    #include <QFileSystemModel>
    #include <QDebug>
    
    bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
    	QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    	QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
    
    	if (fileModel != NULL && fileModel->isDir(index0))
    	{
    		qDebug() << fileModel->fileName(index0);
    		return true;
    	}
    	else
    		return false;
    	// uncomment to execute default implementation
    	//return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
    }
    

      

    1 QFileDialog dialog;
    2 FileFilterProxyModel* proxyModel = new FileFilterProxyModel();
    dialog.setOption(QFileDialog::DontUseNativeDialog);
    3 dialog.setProxyModel(proxyModel); 4 dialog.exec();

    references:

    http://stackoverflow.com/questions/4893122/filtering-in-qfiledialog

    http://stackoverflow.com/questions/2101100/qfiledialog-filtering-folders

  • 相关阅读:
    ios学习- 10大iOS开发者最喜爱的类库
    Android开发之组件
    2015最新iOS学习线路图
    2015最新Android学习线路图
    2015最全iOS开发自学视频资料(基础+实战)
    linux服务器常用密令
    windows服务器入门 使用FileZilla搭建FTP服务
    windows服务器入门 php的安装
    hdu 1106
    hdu 1040 As Easy As A+B
  • 原文地址:https://www.cnblogs.com/foohack/p/4588124.html
Copyright © 2011-2022 走看看