自定義高級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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM