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

  • 相关阅读:
    CF D. Ehab and the Expected XOR Problem 贪心+位运算
    luogu 1903 [国家集训队]数颜色 / 维护队列 带修改莫队
    Test 1 T2 B 线段树合并
    CF812C Sagheer and Nubian Market 二分+贪心
    CF804B Minimum number of steps
    CF796D Police Stations BFS+染色
    CF796C Bank Hacking 细节
    k8s节点NotReady问题处理
    Elastic-Job快速入门
    Elastic-Job介绍
  • 原文地址:https://www.cnblogs.com/foohack/p/4588124.html
Copyright © 2011-2022 走看看