Qt在线文档:
https://doc.qt.io/qt-5/qfiledialog.html
以下为摘抄内容,版权归原作者,笔者无版权。
QFileDialog(QWidget *parent = nullptr, const QString &caption = QString(), const QString &directory = QString(), const QString &filter = QString()) |
QFileDialog(QWidget *parent, Qt::WindowFlags flags) |
原型:
QString | getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options()) |
Detailed Description
The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.
The easiest way to create a QFileDialog is to use the static functions.
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
In the above example, a modal QFileDialog is created using a static function. The dialog initially displays the contents of the "/home/jana" directory, and displays files matching the patterns given in the string "Image Files (*.png *.jpg *.bmp)". The parent of the file dialog is set to this, and the window title is set to "Open Image".
If you want to use multiple filters, separate each one with two semicolons. For example:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, QFileDialog::Options options = Options())
第一个参数parent,用于指定父组件。
第二个参数caption,是对话框的标题;
第三个参数dir ,打开路径,可以为空;
第四个参数filter ,过滤器,过滤格式为"Image Files (*.png *.jpg *.bmp)",当需要过滤多种格式是使用中间加两个下引号“;;”:
比如:"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml);;all file(*)"
举个例子:
QFileDialog dialog;
dialog.getOpenFileName(this,"打开文件","","Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml);;all file(*)");
QFileDialog 的一些其他用法可以参考另外一篇博文
https://blog.csdn.net/lxj362343/article/details/106479820
摘抄参考自: