QT文件对话框设置代理

2023-06-06  本文已影响0人  雯饰太一
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();
}
上一篇下一篇

猜你喜欢

热点阅读