使用QFileDialog可以方便地调用当前系统的文件对话框:
(1)文件打开对话框
- QString getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
示例:
- QString fileName;
- fileName = QFileDialog::getOpenFileName(this,
- tr("Open Config"), "", tr("SCD Files (*.scd);; CID Files (*.cid)"));
-
- if (!fileName.isNull())
- {
-
- }
- else
-
(2)文件保存对话框
- QString getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
示例:
- QString fileName;
- fileName = QFileDialog::getSaveFileName(this,
- tr("Open Config"), "", tr("Config Files (*.ifg)"));
-
- if (!fileName.isNull())
- {
-
- }
- else
-
设置文件过滤的示例
"Image Files (*.png *.jpg *.bmp)" //多个文件使用空格分隔
<span style="font-stretch: normal;font-size:14px; line-height: normal; font-family: Arial; white-space: normal; border-collapse: separate; orphans: 2; widows: 2;"></span><pre class=" cpp" name="code" style="white-space: pre-wrap; word-wrap: break-word;">"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" //多个过滤使用两个分号分隔