FileDialog QML Type

本地文件对话框。更多

Import Statement: import Qt.labs.platform
Inherits:

Dialog

Status: Deprecated since 6.9

自 Qt.labs.platform 6.9 起,该类型已被弃用。我们强烈建议不要在新代码中使用它。

属性

详细说明

使用QtQuick.Dialogs::FileDialog 代替。

FileDialog 类型为本地平台文件对话框提供了 QML API。

要显示文件对话框,请构建 FileDialog 的实例,设置所需的属性,然后调用open() 。currentFilecurrentFiles 属性可用于确定对话框中当前选择的文件。filefiles 属性只有在接受对话框做出最终选择后才会更新。

MenuItem {
    text: "Open..."
    onTriggered: fileDialog.open()
}

FileDialog {
    id: fileDialog
    currentFile: document.source
    folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)
}

MyDocument {
    id: document
    source: fileDialog.file
}

可用性

本地平台文件对话框目前在以下平台上可用:

  • 安卓
  • 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

该属性用于保存接受对话框的按钮上显示的标签文本。

当设置为空字符串时,将使用底层平台的默认标签。默认标签通常为OpenSave ,具体取决于对话框在哪个fileMode 中使用。

默认值为空字符串。

另请参阅 rejectLabel


currentFile : url

该属性用于保存对话框中当前选择的文件。

file 属性不同,当用户在对话框中选择文件时,甚至在最终选择之前,currentFile 属性都会更新。

另请参阅 filecurrentFiles


currentFiles : list<url>

该属性保存对话框中当前选择的文件。

files 属性不同,当用户在对话框中选择文件时,甚至在做出最终选择之前,currentFiles 属性都会被更新。

另请参阅 filescurrentFile


defaultSuffix : string

该属性包含一个后缀,会添加到未指定后缀的选定文件中。后缀通常用于表示文件类型(例如,"txt "表示文本文件)。

如果第一个字符是点('.'),则会被删除。


file : url

该属性保存最终接受的文件。

currentFile 属性不同,file 属性不会在用户在对话框中选择文件时更新,而只会在最终选择完成后更新。也就是说,当用户点击OK 接受文件时,才会更新该属性。另外,也可以处理accepted() 信号来获取最终选择。

另请参阅 currentFileaccepted()。


fileMode : enumeration

该属性表示对话框的模式。

可用值:

常量说明
FileDialog.OpenFile对话框用于选择一个现有文件(默认)。
FileDialog.OpenFiles对话框用于选择多个现有文件。
FileDialog.SaveFile对话框用于选择任何文件。文件不一定存在。

files : list<url>

该属性保存最终接受的文件。

currentFiles 属性不同,当用户在对话框中选择文件时,files 属性不会更新,只有在最终选择完成后才会更新。也就是说,当用户点击OK 接受文件时,才会更新该属性。另外,也可以处理accepted() 信号来获取最终选择。

另请参阅 currentFilesaccepted()。


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: list<string> (扩展名)。此属性包含所选过滤器的扩展名列表。在下面的示例中,第一个过滤器的扩展名列表为["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


© 2025 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.