class FileFileterProxyModel: public QsortFilterProxyModel {
protected: virtual bool filterAcceptsRow(int sourceRow,
const QModelIndex & sourceParent) const {
QModelIndex index0 = sourceModel() -> index(sourceRow, 0, sourceParent);
QFileSystemModel * fileModel = qobject_cast < QFileSystemModel * > (sourceModel());
//I do not want to apply the filter on directions
if (fileModel == nullptr || fileModel -> isDir(index0)) {
return true;
}
auto fn = fileModel -> fileName(index0);
//QRegExp rx(".*\\.\\d\\d\\d");
//只保留.001 .002 .003 ... 等格式的数据,000数据也不需要
QRegExp rx(".*\\.[0-9]{2}[1-9]{1}");
return rx.exactMatch(fn);
}
};
void fun_test() {
//获取文件列表
QString sCurPath = QDir::currentPath();
QFileDialog myFileDialog((QWidget * ) GIS_GetMainFrameQWidget());
myFileDialog.setOption(QFileDialog::DontUseNativeDialog);
myFileDialog.setProxyModel(new FileFileterProxyModel);
myFileDialog.setNameFilter("UpdateFiles(*.*)");
myFileDialog.setFileMode(QFileDialog::ExistingFiles);
QStringList sFilePathList;
if (myFileDialog.exec() == QDialog::Accepted)
sFilePathList = myFileDialog.selectedFiles();
if (sFilePathList.size() == 0)
return false;
sFilePathList.sort();
}
网友评论