QFileDialog提供了一个对话框,允许用户选择文件或目录.
QString | getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QStringList | getOpenFileNames(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QUrl | getOpenFileUrl(QWidget *parent = nullptr, const QString &caption = QString(), const QUrl &dir = QUrl(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList &supportedSchemes = QStringList()) |
QList<QUrl> | getOpenFileUrls(QWidget *parent = nullptr, const QString &caption = QString(), const QUrl &dir = QUrl(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList &supportedSchemes = QStringList()) |
QString | getSaveFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QUrl | getSaveFileUrl(QWidget *parent = nullptr, const QString &caption = QString(), const QUrl &dir = QUrl(), const QString &filter= QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList &supportedSchemes= QStringList()) |
使用示例
QString ccfile = QFileDialog::getOpenFileName(this,"选择要播放的文件","/mp4/",/**初始目录**/"视频文件 (*.flv *.rmvb *.avi *.MP4 *.mp4); 所有文件 (*.*);; "); if (!ccfile.isEmpty()) { /**在linux系统中不需要做目录的替换**/ ccfile.replace("/","\\"); }
QString | getExistingDirectory(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), QFileDialog::Options options = ShowDirsOnly) |
QUrl | getExistingDirectoryUrl(QWidget *parent = nullptr, const QString &caption = QString(), const QUrl &dir = QUrl(), QFileDialog::Options options = ShowDirsOnly, const QStringList &supportedSchemes = QStringList()) |
使用示例
QString ccdir = QFileDialog::getExistingDirectory(this,tr("选择图片目录"),"*",QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); if (!ccdir.isEmpty()) { /**在linux系统中不需要做目录的替换**/ ccdir.replace("/","\\"); QDir dir(ccdir); //设置文件过滤器 dir.setFilter(QDir::Files);//除了文件,其他的过滤掉 QStringList nameFilters; //设置文件过滤格式 nameFilters << "*.png" << "*.jpg" << "*.jpeg"<<"*.bmp"; dir.setNameFilters(nameFilters); QFileInfoList list = dir.entryInfoList();//获取文件信息列表 std::cout << " Bytes Filename" << std::endl; for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); qDebug() << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.fileName()))<<endl; } }