FileDialog QML Type
原生文件对话框。更多...
| Import Statement: | import Qt.labs.platform |
| Inherits: | |
| Status: | Deprecated since 6.9 |
自 Qt Labs Platform 6.9 起,此类型已弃用。我们强烈建议不要在新代码中使用它。
属性
- acceptLabel : string
- currentFile : url
- currentFiles : list<url>
- defaultSuffix : string
- file : url
- fileMode : enumeration
- files : list<url>
- folder : url
- nameFilters : list<string>
- options : flags
- rejectLabel : string
- selectedNameFilter
- selectedNameFilter.extensions : list<string>
- selectedNameFilter.index : int
- selectedNameFilter.name : string
详细说明
请改用QtQuick.Dialogs FileDialog 。
FileDialog 类型为原生平台的文件对话框提供了一个 QML API。

要显示文件对话框,请创建一个 FileDialog 实例,设置所需属性,然后调用open()。可使用currentFile 或currentFiles 属性来确定对话框中当前选中的文件。file 和files 属性仅在通过接受对话框完成最终选择后才会更新。
MenuItem {
text: "Open..."
onTriggered: fileDialog.open()
}
FileDialog {
id: fileDialog
currentFile: document.source
folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)
}
MyDocument {
id: document
source: fileDialog.file
}可用性
目前,以下平台支持原生平台文件对话框:
- Android
- iOS
- Linux(使用 GTK+ 平台主题运行时)
- macOS
- Windows
Qt Labs Platform 模块在没有原生实现的平台上会使用Qt Widgets 作为备用方案。因此,使用Qt Labs Platform 模块中类型的应用程序应链接到QtWidgets ,并使用QApplication 而非QGuiApplication 。
要链接到QtWidgets 库,请在您的 qmake 项目文件中添加以下内容:
QT += widgets在 `main()` 中创建 `QApplication ` 的实例:
#include <QApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}注意: Qt.labs 模块中的类型在未来版本中不保证保持兼容。
另请参阅 QtQuick.Dialogs::FileDialog 、FolderDialog 以及StandardPaths 。
属性文档
acceptLabel : string
该属性用于指定接受对话框的按钮上显示的标签文本。
当设置为空字符串时,将使用底层平台的默认标签。默认标签通常为“Open ”或“Save ”,具体取决于对话框所使用的fileMode 。
默认值为空字符串。
另请参阅 rejectLabel 。
currentFile : url
该属性保存对话框中当前选中的文件。
与file 属性不同,currentFile 属性会在用户在对话框中选择文件时实时更新,即使在最终选择完成之前也是如此。
另请参阅 file 和currentFiles 。
currentFiles : list<url>
该属性保存对话框中当前选中的文件。
与files 属性不同,currentFiles 属性会在用户于对话框中选择文件时实时更新,即使最终选择尚未确定也是如此。
另请参阅 files 和currentFile 。
defaultSuffix : string
此属性保存一个后缀,该后缀将添加到未指定后缀的选定文件中。该后缀通常用于标识文件类型(例如,“txt”表示文本文件)。
如果文件名的第一个字符是点('.'),则将其删除。
file : url
该属性存储最终被接受的文件。
与currentFile 属性不同,file 属性不会在用户通过对话框选择文件的过程中实时更新,而仅在最终选择确定后才会更新。也就是说,当用户点击OK 以确认文件时,该属性才会更新。此外,也可以通过处理accepted()信号来获取最终选择结果。
另请参阅 currentFile 和accepted()。
fileMode : enumeration
该属性保存对话框的模式。
可用值:
| 常量 | 描述 |
|---|---|
FileDialog.OpenFile | 该对话框用于选择现有文件(默认)。 |
FileDialog.OpenFiles | 该对话框用于选择多个现有文件。 |
FileDialog.SaveFile | 该对话框用于选择任意文件。该文件不必实际存在。 |
files : list<url>
该属性存储最终被接受的文件。
与currentFiles 属性不同,files 属性不会在用户于对话框中选择文件时更新,而仅在最终选择完成后更新。也就是说,当用户点击OK 以确认文件时。此外,也可以通过处理accepted()信号来获取最终选择结果。
另请参阅 currentFiles 和accepted()。
folder : url
此属性保存用于选择文件的文件夹。若要选择文件夹,请改用 FolderDialog。
另请参阅 FolderDialog 。
nameFilters : list<string>
该属性包含用于限制可选文件类型的过滤器。
FileDialog {
nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"]
}注意: *.*并非可移植的筛选条件,因为“文件扩展名决定文件类型”这一历史假设在各个操作系统上并不一致。可能存在文件名中不含点的情况(例如,Makefile )。 在原生 Windows 文件对话框中,*.*会匹配此类文件,但在其他类型的文件对话框中则可能无法匹配。因此,若要选择任意文件,最好使用*。
另请参阅 selectedNameFilter 。
options : flags
该属性包含影响对话框外观和风格的各种选项。
默认情况下,所有选项均处于禁用状态。
应在显示对话框之前设置这些选项。在对话框可见时进行设置,不能保证会立即对对话框产生影响(具体取决于选项和平台)。
可用选项:
| 常量 | 描述 |
|---|---|
FileDialog.DontResolveSymlinks | 在文件对话框中不解析符号链接。默认情况下会解析符号链接。 |
FileDialog.DontConfirmOverwrite | 若选中现有文件,则不提示确认。默认情况下会提示确认。 |
FileDialog.ReadOnly | 表示该对话框不允许创建目录。 |
FileDialog.HideNameFilterDetails | 指示文件名过滤器详细信息是否被隐藏。 |
rejectLabel : string
该属性用于存储在用于关闭对话框的按钮上显示的标签文本。
当设置为空字符串时,将使用底层平台的默认标签。默认标签通常为Cancel 。
默认值为空字符串。
另请参阅 acceptLabel 。
selectedNameFilter group
selectedNameFilter.extensions : list<string>
selectedNameFilter.index : int
selectedNameFilter.name : string
这些属性保存了当前选中的名称过滤条件。
| 名称 | 描述 |
|---|---|
| index: int | 此属性用于确定选择哪个“name filter ”。打开对话框时,将选中指定的筛选器。当用户选择另一个筛选器时,该值会随之更新。 |
| [只读]name: string | 该属性存储所选过滤器的名称。在下面的示例中,第一个过滤器的名称为"Text files" ,第二个为"HTML files" 。 |
| [只读]extensions: 列表<字符串> | 该属性存储所选过滤器的扩展名列表。在下面的示例中,第一个过滤器的扩展名列表为["txt"] ,第二个为["html", "htm"] 。 |
FileDialog {
id: fileDialog
selectedNameFilter.index: 1
nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"]
}
MyDocument {
id: document
fileType: fileDialog.selectedNameFilter.extensions[0]
}另请参阅 nameFilters 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.