qt QFileDialog ::getOpenFileName 打开文件对话框

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)

原型:

QStringgetOpenFileName(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

摘抄参考自:

https://blog.csdn.net/qq_38400517/article/details/78897446

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值