QFileDialog 类提供允许用户选择文件 (或目录) 的对话框。 更多...
头: | #include <QFileDialog> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
继承: | QDialog |
enum | AcceptMode { AcceptOpen, AcceptSave } |
enum | DialogLabel { LookIn, FileName, FileType, Accept, Reject } |
enum | FileMode { AnyFile, ExistingFile, Directory, ExistingFiles } |
enum | Option { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseNativeDialog, ReadOnly, …, DontUseCustomDirectoryIcons } |
flags | Options |
enum | ViewMode { Detail, List } |
|
|
QFileDialog (QWidget * parent , Qt::WindowFlags flags ) | |
QFileDialog (QWidget * parent = nullptr, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString()) | |
virtual | ~QFileDialog () |
QFileDialog::AcceptMode | acceptMode () const |
QString | defaultSuffix () const |
QDir | directory () const |
QUrl | directoryUrl () const |
QFileDialog::FileMode | fileMode () const |
QDir::Filters | filter () const |
QStringList | history () const |
QAbstractFileIconProvider * | iconProvider () const |
QAbstractItemDelegate * | itemDelegate () const |
QString | labelText (QFileDialog::DialogLabel label ) const |
QStringList | mimeTypeFilters () const |
QStringList | nameFilters () const |
void | open (QObject * receiver , const char * member ) |
QFileDialog::Options | options () const |
QAbstractProxyModel * | proxyModel () const |
bool | restoreState (const QByteArray & state ) |
QByteArray | saveState () const |
void | selectFile (const QString & filename ) |
void | selectMimeTypeFilter (const QString & filter ) |
void | selectNameFilter (const QString & filter ) |
void | selectUrl (const QUrl & url ) |
QStringList | selectedFiles () const |
QString | selectedMimeTypeFilter () const |
QString | selectedNameFilter () const |
QList<QUrl> | selectedUrls () const |
void | setAcceptMode (QFileDialog::AcceptMode mode ) |
void | setDefaultSuffix (const QString & suffix ) |
void | setDirectory (const QString & directory ) |
void | setDirectory (const QDir & directory ) |
void | setDirectoryUrl (const QUrl & directory ) |
void | setFileMode (QFileDialog::FileMode mode ) |
void | setFilter (QDir::Filters filters ) |
void | setHistory (const QStringList & paths ) |
void | setIconProvider (QAbstractFileIconProvider * provider ) |
void | setItemDelegate (QAbstractItemDelegate * delegate ) |
void | setLabelText (QFileDialog::DialogLabel label , const QString & text ) |
void | setMimeTypeFilters (const QStringList & filters ) |
void | setNameFilter (const QString & filter ) |
void | setNameFilters (const QStringList & filters ) |
void | setOption (QFileDialog::Option option , bool on = true) |
void | setOptions (QFileDialog::Options options ) |
void | setProxyModel (QAbstractProxyModel * proxyModel ) |
void | setSidebarUrls (const QList<QUrl> & urls ) |
void | setSupportedSchemes (const QStringList & schemes ) |
void | setViewMode (QFileDialog::ViewMode mode ) |
QList<QUrl> | sidebarUrls () const |
QStringList | supportedSchemes () const |
bool | testOption (QFileDialog::Option option ) const |
QFileDialog::ViewMode | viewMode () const |
virtual void | setVisible (bool visible ) override |
void | currentChanged (const QString & path ) |
void | currentUrlChanged (const QUrl & url ) |
void | directoryEntered (const QString & directory ) |
void | directoryUrlEntered (const QUrl & directory ) |
void | fileSelected (const QString & file ) |
void | filesSelected (const QStringList & selected ) |
void | filterSelected (const QString & filter ) |
void | urlSelected (const QUrl & url ) |
void | urlsSelected (const QList<QUrl> & urls ) |
QString | getExistingDirectory (QWidget * parent = nullptr, const QString & caption = QString(), const QString & dir = QString(), QFileDialog::Options options = ShowDirsOnly) |
QUrl | getExistingDirectoryUrl (QWidget * parent = nullptr, const QString & caption = QString(), const QUrl & dir = QUrl(), QFileDialog::Options options = ShowDirsOnly, const QStringList & supportedSchemes = QStringList()) |
void | getOpenFileContent (const QString & nameFilter , const std::function<void (const QString &, const QByteArray &)> & fileOpenCompleted , QWidget * parent = nullptr) |
QString | getOpenFileName (QWidget * parent = nullptr, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QStringList | getOpenFileNames (QWidget * parent = nullptr, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QUrl | getOpenFileUrl (QWidget * parent = nullptr, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList & supportedSchemes = QStringList()) |
QList<QUrl> | getOpenFileUrls (QWidget * parent = nullptr, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList & supportedSchemes = QStringList()) |
QString | getSaveFileName (QWidget * parent = nullptr, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options()) |
QUrl | getSaveFileUrl (QWidget * parent = nullptr, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = nullptr, QFileDialog::Options options = Options(), const QStringList & supportedSchemes = QStringList()) |
void | saveFileContent (const QByteArray & fileContent , const QString & fileNameHint , QWidget * parent = nullptr) |
virtual void | accept () override |
virtual void | changeEvent (QEvent * e ) override |
virtual void | done (int result ) override |
QFileDialog 类使用户能够遍历文件系统,以选择一个或多个文件 (或目录)。
创建 QFileDialog 的最轻松方式是使用静态函数,譬如 getOpenFileName ().
fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
在以上范例中,使用静态函数创建模态 QFileDialog。对话框初始显示 /home/jana 目录的内容,并显示与字符串 Image files (*.png *.jpg *.bmp) 中给定模式匹配的文件。文件对话框父级被设为 this ,且窗口标题被设为 Open Image。
若想要使用多个过滤器,分隔每过滤器采用 two 分号。例如:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
可以创建自己的 QFileDialog 无需使用静态函数。通过调用 setFileMode (),可以指定用户必须在对话框中选择什么:
QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile);
在以上范例中,文件对话框的模式被设为 AnyFile ,意味着用户可以选择任何文件,甚至指定不存在的文件。此模式对于创建 Save As (另存为) 文件对话框很有用。使用 ExistingFile 若用户必须选择现有文件,或 Directory 若只能选择目录。见 QFileDialog::FileMode 枚举了解模式的完整列表。
The fileMode 特性包含对话框的操作模式;这指示用户期望选择什么类型的对象。使用 setNameFilter () 以设置对话框的文件过滤器。例如:
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
在以上范例中,把过滤器设为
"Images (*.png *.xpm *.jpg)"
。这意味着仅文件具有扩展名
png
,
xpm
,或
jpg
会展示在 QFileDialog 中。可以应用多个过滤器通过使用
setNameFilters
()。使用
selectNameFilter
() 以选择给出的某一过滤器作为文件对话框默认过滤器。
文件对话框有 2 种查看模式: List and Detail . List 将当前目录内容呈现为文件和目录名称列表。 Detail 显示文件和目录名称列表,但每个名称旁边还提供额外信息 (譬如:文件大小和修改日期)。设置模式采用 setViewMode ():
dialog.setViewMode(QFileDialog::Detail);
创建自己的文件对话框时,需要使用的最后一个重要函数是 selectedFiles ().
QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
在以上范例中,创建并展示模态文件对话框。若用户点击 OK,选中文件被放入
fileName
.
可以设置对话框的工作目录采用 setDirectory ()。可以选择当前目录中的每个文件使用 selectFile () 函数。
The 标准对话框 范例展示如何使用 QFileDialog 及其它内置 Qt 对话框。
By default, a platform-native file dialog is used if the platform has one. In that case, the widgets that would otherwise be used to construct the dialog are not instantiated, so related accessors such as layout () 和 itemDelegate () return null. Also, not all platforms show file dialogs with a title bar, so be aware that the caption text might not be visible to the user. You can set the DontUseNativeDialog option or set the AA_DontUseNativeDialogs application attribute to ensure that the widget-based implementation is used instead of the native dialog.
另请参阅 QDir , QFileInfo , QFile , QColorDialog , QFontDialog ,和 标准对话框范例 .
常量 | 值 |
---|---|
QFileDialog::AcceptOpen
|
0
|
QFileDialog::AcceptSave
|
1
|
常量 | 值 |
---|---|
QFileDialog::LookIn
|
0
|
QFileDialog::FileName
|
1
|
QFileDialog::FileType
|
2
|
QFileDialog::Accept
|
3
|
QFileDialog::Reject
|
4
|
This enum is used to indicate what the user may select in the file dialog; that is, what the dialog returns if the user clicks OK.
常量 | 值 | 描述 |
---|---|---|
QFileDialog::AnyFile
|
0
|
文件的名称,无论它是否存在。 |
QFileDialog::ExistingFile
|
1
|
单个现有文件的名称。 |
QFileDialog::Directory
|
2
|
目录的名称。显示文件和目录两者。然而,本机 Windows 文件对话框不支持在目录选取器中显示文件。 |
QFileDialog::ExistingFiles
|
3
|
零个或多个现有文件的名称。 |
另请参阅 setFileMode ().
影响对话框行为的选项。
常量 | 值 | 描述 |
---|---|---|
QFileDialog::ShowDirsOnly
|
0x00000001
|
只展示目录。默认情况下,展示文件和目录两者。
此选项才有效在 Directory 文件模式。 |
QFileDialog::DontResolveSymlinks
|
0x00000002
|
不解析符号链接。默认情况下,解析符号链接。 |
QFileDialog::DontConfirmOverwrite
|
0x00000004
|
不要求确认,若选中现有文件。默认情况下,请求确认。
此选项才有效,若 acceptMode is AcceptSave 。此外,它不可以用于 macOS 的本机文件对话框。 |
QFileDialog::DontUseNativeDialog
|
0x00000008
|
不使用平台本机文件对话框,而是使用由 Qt 提供的基于 Widget 的对话框。
默认情况下,展示本机文件对话框,除非使用子类化的 QFileDialog 包含 Q_OBJECT 宏,全局 AA_DontUseNativeDialogs 应用程序特性有设置 (或平台没有要求类型的本机对话框)。 要使选项有效,必须设置它,在改变对话框的其它属性 (或展示对话框) 之前。 |
QFileDialog::ReadOnly
|
0x00000010
|
只读模型指示。 |
QFileDialog::HideNameFilterDetails
|
0x00000020
|
指示是否隐藏文件名过滤器细节。 |
QFileDialog::DontUseCustomDirectoryIcons
|
0x00000040
|
始终使用默认目录图标。
某些平台允许用户设置不同图标,但自定义图标查找在网络 (或可移动驱动器) 中可能导致显著性能问题。 设置这将启用 DontUseCustomDirectoryIcons 选项在 iconProvider (). 该枚举值在 Qt 5.2 添加。 |
Options 类型是 typedef 对于 QFlags <Option>。它存储 Option 值的 OR 组合。
另请参阅 options and testOption .
此枚举描述文件对话框的视图模式;也就是说,显示每个文件的什么信息。
常量 | 值 | 描述 |
---|---|---|
QFileDialog::Detail
|
0
|
Displays an icon, a name, and details for each item in the directory. |
QFileDialog::List
|
1
|
Displays only an icon and a name for each item in the directory. |
另请参阅 setViewMode ().
此特性保持对话框的接受方式。
动作模式定义对话框是用于打开文件还是保存文件。
默认情况下,此特性被设为 AcceptOpen .
访问函数:
QFileDialog::AcceptMode | acceptMode () const |
void | setAcceptMode (QFileDialog::AcceptMode mode ) |
另请参阅 AcceptMode .
Suffix added to the filename if no other suffix was specified.
This property specifies a string that is added to the filename if it has no suffix yet. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).
If the first character is a dot ('.'), it is removed.
访问函数:
QString | defaultSuffix () const |
void | setDefaultSuffix (const QString & suffix ) |
此特性保持对话框的文件模式。
The file mode defines the number and type of items that the user is expected to select in the dialog.
默认情况下,此特性被设为 AnyFile .
This function sets the labels for the FileName and Accept DialogLabel s. It is possible to set custom text after the call to setFileMode().
访问函数:
QFileDialog::FileMode | fileMode () const |
void | setFileMode (QFileDialog::FileMode mode ) |
另请参阅 FileMode .
This property holds the various options that affect the look and feel of the dialog.
默认情况下,所有选项是被禁用的。
选项 (尤其 DontUseNativeDialog 选项) 应该设置,在改变对话框属性 (或展示对话框) 之前。
设置选项当对话框可见时,不保证会立即影响对话框 (从属选项和平台)。
设置选项在改变其它特性后,可能导致这些值不起作用。
访问函数:
QFileDialog::Options | options () const |
void | setOptions (QFileDialog::Options options ) |
另请参阅 setOption () 和 testOption ().
This property holds the URL schemes that the file dialog should allow navigating to.
Setting this property allows to restrict the type of URLs the user can select. It is a way for the application to declare the protocols it supports to fetch the file content. An empty list means that no restriction is applied (the default). Support for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
访问函数:
QStringList | supportedSchemes () const |
void | setSupportedSchemes (const QStringList & schemes ) |
This property holds the way files and directories are displayed in the dialog.
默认情况下,
Detail
mode is used to display information about files and directories.
访问函数:
QFileDialog::ViewMode | viewMode () const |
void | setViewMode (QFileDialog::ViewMode mode ) |
另请参阅 ViewMode .
构造文件对话框采用给定 parent 和小部件 flags .
[explicit]
QFileDialog::
QFileDialog
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
directory
= QString(), const
QString
&
filter
= QString())
构造文件对话框采用给定 parent and caption that initially displays the contents of the specified directory . The contents of the directory are filtered before being shown in the dialog, using a semicolon-separated list of filters specified by filter .
[virtual noexcept]
QFileDialog::
~QFileDialog
()
销毁文件对话框。
[override virtual protected]
void
QFileDialog::
accept
()
重实现: QDialog::accept ().
[override virtual protected]
void
QFileDialog::
changeEvent
(
QEvent
*
e
)
重实现: QWidget::changeEvent (QEvent *event).
[signal]
void
QFileDialog::
currentChanged
(const
QString
&
path
)
When the current file changes for local operations, this signal is emitted with the new file name as the path 参数。
另请参阅 filesSelected ().
[signal]
void
QFileDialog::
currentUrlChanged
(const
QUrl
&
url
)
When the current file changes, this signal is emitted with the new file URL as the url 参数。
另请参阅 urlsSelected ().
返回目前显示在对话框中的目录。
另请参阅 setDirectory ().
[signal]
void
QFileDialog::
directoryEntered
(const
QString
&
directory
)
This signal is emitted for local operations when the user enters a directory .
Returns the url of the directory currently being displayed in the dialog.
另请参阅 setDirectoryUrl ().
[signal]
void
QFileDialog::
directoryUrlEntered
(const
QUrl
&
directory
)
此信号被发射当用户键入 directory .
[override virtual protected]
void
QFileDialog::
done
(
int
result
)
重实现: QDialog::done (int r).
[signal]
void
QFileDialog::
fileSelected
(const
QString
&
file
)
When the selection changes for local operations and the dialog is accepted, this signal is emitted with the (possibly empty) selected file .
另请参阅 currentChanged () 和 QDialog::Accepted .
[signal]
void
QFileDialog::
filesSelected
(const
QStringList
&
selected
)
When the selection changes for local operations and the dialog is accepted, this signal is emitted with the (possibly empty) list of selected 文件。
另请参阅 currentChanged () 和 QDialog::Accepted .
返回所用过滤器,当显示文件时。
另请参阅 setFilter ().
[signal]
void
QFileDialog::
filterSelected
(const
QString
&
filter
)
此信号被发射,当用户选择 filter .
[static]
QString
QFileDialog::
getExistingDirectory
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
dir
= QString(),
QFileDialog::Options
options
= ShowDirsOnly)
This is a convenience static function that returns an existing directory selected by the user.
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
此函数创建模态文件对话框采用给定
parent
小部件。若
parent
不是
nullptr
, the dialog is shown centered over the parent widget.
The dialog's working directory is set to dir , and the caption is set to caption . Either of these can be an empty string in which case the current directory and a default caption are used respectively.
The options argument holds various options about how to run the dialog. See the QFileDialog::Option enum for more information on the flags you can pass. To ensure a native file dialog, ShowDirsOnly must be set.
On Windows and macOS, this static function uses the native file dialog and not a QFileDialog . However, the native Windows file dialog does not support displaying files in the directory chooser. You need to pass the DontUseNativeDialog option, or set the global \l{Qt::}{AA_DontUseNativeDialogs} application attribute to display files using a QFileDialog .
Note that the macOS native file dialog does not show a title bar.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if
/usr/tmp
is a symlink to
/var/tmp
, the file dialog changes to
/var/tmp
after entering
/usr/tmp
。若
options
包括
DontResolveSymlinks
, the file dialog treats symlinks as regular directories.
On Windows, the dialog spins a blocking modal event loop that does not dispatch any QTimers, and if
parent
不是
nullptr
then it positions the dialog just below the parent's title bar.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you must create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getSaveFileName ().
[static]
QUrl
QFileDialog::
getExistingDirectoryUrl
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QUrl
&
dir
= QUrl(),
QFileDialog::Options
options
= ShowDirsOnly, const
QStringList
&
supportedSchemes
= QStringList())
This is a convenience static function that returns an existing directory selected by the user. If the user presses Cancel, it returns an empty url.
函数的用法类似于 QFileDialog::getExistingDirectory ()。尤其 parent , caption , dir and options are used in exactly the same way.
The main difference with QFileDialog::getExistingDirectory () comes from the ability offered to the user to select a remote directory. That's why the return type and the type of dir is QUrl .
The supportedSchemes argument allows to restrict the type of URLs the user is able to select. It is a way for the application to declare the protocols it supports to fetch the file content. An empty list means that no restriction is applied (the default). Support for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
When possible, this static function uses the native file dialog and not a QFileDialog . On platforms that don't support selecting remote files, Qt allows to select only local files.
另请参阅 getExistingDirectory (), getOpenFileUrl (), getOpenFileUrls (),和 getSaveFileUrl ().
[static]
void
QFileDialog::
getOpenFileContent
(const
QString
&
nameFilter
, const
std::function
<
void
(const
QString
&, const
QByteArray
&)> &
fileOpenCompleted
,
QWidget
*
parent
= nullptr)
This is a convenience static function that returns the content of a file selected by the user.
Use this function to access local files on Qt for WebAssembly, if the web sandbox restricts file access. Its implementation enables displaying a native file dialog in the browser, where the user selects a file based on the nameFilter 参数。
parent is ignored on Qt for WebAssembly. Pass parent on other platforms, to make the popup a child of another widget. If the platform doesn't support native file dialogs, the function falls back to QFileDialog .
The function is asynchronous and returns immediately. The fileOpenCompleted callback will be called when a file has been selected and its contents have been read into memory.
auto fileContentReady = [](const QString &fileName, const QByteArray &fileContent) { if (fileName.isEmpty()) { // No file was selected } else { // Use fileName and fileContent } }; QFileDialog::getOpenFileContent("Images (*.png *.xpm *.jpg)", fileContentReady);
[static]
QString
QFileDialog::
getOpenFileName
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
dir
= QString(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options())
This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string.
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Images (*.png *.xpm *.jpg)"));
The function creates a modal file dialog with the given
parent
小部件。若
parent
不是
nullptr
, the dialog is shown centered over the parent widget.
The file dialog's working directory is set to dir 。若 dir includes a file name, the file is selected. Only files that match the given filter are shown. The selected filter is set to selectedFilter 。参数 dir , selectedFilter ,和 filter may be empty strings. If you want multiple filters, separate them with ';;', for example:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
The options argument holds various options about how to run the dialog. See the QFileDialog::Option 枚举了解可以传递标志的更多有关信息。
把对话框标题设为 caption 。若 caption is not specified, then a default caption will be used.
On Windows, and macOS, this static function uses the native file dialog and not a QFileDialog . Note that the macOS native file dialog does not show a title bar.
On Windows the dialog spins a blocking modal event loop that does not dispatch any QTimers, and if
parent
不是
nullptr
then it positions the dialog just below the parent's title bar.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if
/usr/tmp
is a symlink to
/var/tmp
, the file dialog changes to
/var/tmp
after entering
/usr/tmp
。若
options
包括
DontResolveSymlinks
, the file dialog treats symlinks as regular directories.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you must create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileNames (), getSaveFileName (),和 getExistingDirectory ().
[static]
QStringList
QFileDialog::
getOpenFileNames
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
dir
= QString(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options())
This is a convenience static function that returns one or more existing files selected by the user.
QStringList files = QFileDialog::getOpenFileNames( this, "Select one or more files to open", "/home", "Images (*.png *.xpm *.jpg)");
此函数创建模态文件对话框采用给定
parent
小部件。若
parent
不是
nullptr
, the dialog is shown centered over the parent widget.
The file dialog's working directory is set to dir 。若 dir includes a file name, the file is selected. The filter is set to filter so that only those files which match the filter are shown. The filter selected is set to selectedFilter 。参数 dir , selectedFilter and filter can be empty strings. If you need multiple filters, separate them with ';;', for instance:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
把对话框标题设为 caption 。若 caption 未指定,则使用默认标题。
On Windows and macOS, this static function uses the native file dialog and not a QFileDialog . Note that the macOS native file dialog does not show a title bar.
On Windows the dialog spins a blocking modal event loop that does not dispatch any QTimers, and if
parent
不是
nullptr
then it positions the dialog just below the parent's title bar.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if
/usr/tmp
is a symlink to
/var/tmp
, the file dialog will change to
/var/tmp
after entering
/usr/tmp
。
options
自变量保持有关如何运行对话框的各种选项,见
QFileDialog::Option
枚举了解可以传递标志的更多有关信息。
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you must create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getSaveFileName (),和 getExistingDirectory ().
[static]
QUrl
QFileDialog::
getOpenFileUrl
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QUrl
&
dir
= QUrl(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options(), const
QStringList
&
supportedSchemes
= QStringList())
This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns an empty url.
函数的用法类似于 QFileDialog::getOpenFileName ()。尤其 parent , caption , dir , filter , selectedFilter and options are used in exactly the same way.
The main difference with QFileDialog::getOpenFileName () comes from the ability offered to the user to select a remote file. That's why the return type and the type of dir is QUrl .
The supportedSchemes argument allows to restrict the type of URLs the user is able to select. It is a way for the application to declare the protocols it will support to fetch the file content. An empty list means that no restriction is applied (the default). Support for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
When possible, this static function uses the native file dialog and not a QFileDialog . On platforms that don't support selecting remote files, Qt will allow to select only local files.
另请参阅 getOpenFileName (), getOpenFileUrls (), getSaveFileUrl (),和 getExistingDirectoryUrl ().
[static]
QList
<
QUrl
> QFileDialog::
getOpenFileUrls
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QUrl
&
dir
= QUrl(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options(), const
QStringList
&
supportedSchemes
= QStringList())
This is a convenience static function that returns one or more existing files selected by the user. If the user presses Cancel, it returns an empty list.
函数的用法类似于 QFileDialog::getOpenFileNames ()。尤其 parent , caption , dir , filter , selectedFilter and options are used in exactly the same way.
The main difference with QFileDialog::getOpenFileNames () comes from the ability offered to the user to select remote files. That's why the return type and the type of dir are respectively QList < QUrl > 和 QUrl .
The supportedSchemes argument allows to restrict the type of URLs the user can select. It is a way for the application to declare the protocols it supports to fetch the file content. An empty list means that no restriction is applied (the default). Support for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
When possible, this static function uses the native file dialog and not a QFileDialog . On platforms that don't support selecting remote files, Qt will allow to select only local files.
另请参阅 getOpenFileNames (), getOpenFileUrl (), getSaveFileUrl (),和 getExistingDirectoryUrl ().
[static]
QString
QFileDialog::
getSaveFileName
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
dir
= QString(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options())
This is a convenience static function that returns a file name selected by the user. The file does not have to exist.
它创建模态文件对话框采用给定
parent
小部件。若
parent
不是
nullptr
,对话框将展示在父级 Widget 中心。
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "/home/jana/untitled.png", tr("Images (*.png *.xpm *.jpg)"));
The file dialog's working directory is set to dir 。若 dir includes a file name, the file is selected. Only files that match the filter 才被展示。选中过滤器被设为 selectedFilter 。参数 dir , selectedFilter ,和 filter 可以是空字符串。多个过滤器采用 ;; 分隔。例如:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
The options 自变量保持有关如何运行对话框的各种选项,见 QFileDialog::Option 枚举了解可以传递标志的更多有关信息。
可以选取默认过滤器通过设置 selectedFilter 到期望值。
把对话框标题设为 caption 。若 caption 未指定,使用默认标题。
On Windows, and macOS, this static function uses the native file dialog and not a QFileDialog .
On Windows the dialog spins a blocking modal event loop that does not dispatch any QTimers, and if
parent
不是
nullptr
then it positions the dialog just below the parent's title bar. On macOS, with its native file dialog, the filter argument is ignored.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if
/usr/tmp
is a symlink to
/var/tmp
, the file dialog changes to
/var/tmp
after entering
/usr/tmp
。若
options
包括
DontResolveSymlinks
, the file dialog treats symlinks as regular directories.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you must create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getExistingDirectory ().
[static]
QUrl
QFileDialog::
getSaveFileUrl
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QUrl
&
dir
= QUrl(), const
QString
&
filter
= QString(),
QString
*
selectedFilter
= nullptr,
QFileDialog::Options
options
= Options(), const
QStringList
&
supportedSchemes
= QStringList())
这是返回用户选择文件的方便静态函数。文件不必存在。若用户按下 Cancel,返回空 URL。
函数的用法类似于 QFileDialog::getSaveFileName ()。尤其 parent , caption , dir , filter , selectedFilter and options are used in exactly the same way.
The main difference with QFileDialog::getSaveFileName () comes from the ability offered to the user to select a remote file. That's why the return type and the type of dir is QUrl .
The supportedSchemes argument allows to restrict the type of URLs the user can select. It is a way for the application to declare the protocols it supports to save the file content. An empty list means that no restriction is applied (the default). Support for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
When possible, this static function uses the native file dialog and not a QFileDialog . On platforms that don't support selecting remote files, Qt will allow to select only local files.
另请参阅 getSaveFileName (), getOpenFileUrl (), getOpenFileUrls (),和 getExistingDirectoryUrl ().
以路径列表形式返回文件对话框的浏览历史。
另请参阅 setHistory ().
返回文件对话框使用的图标提供程序。
另请参阅 setIconProvider ().
返回用于在文件对话框视图中呈现项的项委托。
另请参阅 setItemDelegate ().
返回文件对话框展示的文本在指定 label .
另请参阅 setLabelText ().
返回在此文件对话框中运转的 MIME 类型过滤器。
另请参阅 setMimeTypeFilters ().
返回在此文件对话框中运转的文件类型过滤器。
另请参阅 setNameFilters ().
This function shows the dialog, and connects the slot specified by receiver and member to the signal that informs about selection changes. If the fileMode is ExistingFiles , this is the filesSelected () signal, otherwise it is the fileSelected () 信号。
从槽断开信号连接,当关闭对话框时。
Returns the proxy model used by the file dialog. By default no proxy is set.
另请参阅 setProxyModel ().
Restores the dialogs's layout, history and current directory to the state 指定。
通常,这用于结合 QSettings to restore the size from a past session.
返回
false
若有错误
[static]
void
QFileDialog::
saveFileContent
(const
QByteArray
&
fileContent
, const
QString
&
fileNameHint
,
QWidget
*
parent
= nullptr)
This is a convenience static function that saves fileContent to a file, using a file name and location chosen by the user. fileNameHint can be provided to suggest a file name to the user.
Use this function to save content to local files on Qt for WebAssembly, if the web sandbox restricts file access. Its implementation enables displaying a native file dialog in the browser, where the user specifies an output file based on the fileNameHint 自变量。
parent is ignored on Qt for WebAssembly. Pass parent on other platforms, to make the popup a child of another widget. If the platform doesn't support native file dialogs, the function falls back to QFileDialog .
The function is asynchronous and returns immediately.
QByteArray imageData; // obtained from e.g. QImage::save() QFileDialog::saveFileContent(imageData, "myimage.png"); // with filename hint // OR QFileDialog::saveFileContent(imageData); // no filename hint
Saves the state of the dialog's layout, history and current directory.
通常,这用于结合 QSettings to remember the size for a future session. A version number is stored as part of the data.
选择给定 filename 在文件对话框。
另请参阅 selectedFiles ().
设置当前 MIME 类型 filter .
设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.
另请参阅 setNameFilter (), setNameFilters (),和 selectedNameFilter ().
选择给定 url 在文件对话框。
注意: 非本机 QFileDialog 仅支持本地文件。
另请参阅 selectedUrls ().
Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles or ExistingFile , selectedFiles() contains the current path in the viewport.
另请参阅 selectedNameFilter () 和 selectFile ().
Returns The mimetype of the file that the user selected in the file dialog.
Returns the filter that the user selected in the file dialog.
另请参阅 selectedFiles ().
Returns a list of urls containing the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles or ExistingFile , selectedUrls() contains the current path in the viewport.
另请参阅 selectedNameFilter () 和 selectUrl ().
设置文件对话框的当前 directory .
注意:
在 iOS,若设置
directory
to
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last
(), a native image picker dialog is used for accessing the user's photo album. The filename returned can be loaded using
QFile
and related APIs. For this to be enabled, the Info.plist assigned to QMAKE_INFO_PLIST in the project file must contain the key
NSPhotoLibraryUsageDescription
. See Info.plist documentation from Apple for more information regarding this key. This feature was added in Qt 5.5.
另请参阅 directory ().
这是重载函数。
设置文件对话框的当前 directory url.
注意: 非本机 QFileDialog 仅支持本地文件。
注意:
On Windows, it is possible to pass URLs representing one of the
virtual folders
, such as "Computer" or "Network". This is done by passing a
QUrl
使用方案
clsid
followed by the CLSID value with the curly braces removed. For example the URL
clsid:374DE290-123F-4565-9164-39C4925E467B
denotes the download location. For a complete list of possible values, see the MSDN documentation on
KNOWNFOLDERID
。该特征在 Qt 5.5 添加。
另请参阅 directoryUrl () 和 QUuid .
将用于模型的过滤器设为 filters 。过滤器用于指定应展示的文件种类。
另请参阅 filter ().
设置文件对话框浏览历史以包含给定 paths .
另请参阅 history ().
将用于文件对话框的图标提供程序设为指定 provider .
另请参阅 iconProvider ().
Sets the item delegate used to render items in the views in the file dialog to the given delegate .
任何现有委托将被移除,但不被删除。 QFileDialog 未拥有所有权对于 delegate .
警告: 不应该在视图之间,共享实例化的相同委托。这样做会导致不正确 (或不直观) 编辑行为,由于连接到给定委托的各视图都可以接收 closeEditor () 信号,且试图访问、修改或关闭已关闭的编辑器。
Note that the model used is QFileSystemModel . It has custom item data roles, which is described by the Roles 枚举。可以使用 QFileIconProvider 若只想要自定义图标。
另请参阅 itemDelegate (), setIconProvider (),和 QFileSystemModel .
设置 text 展示在文件对话框在指定 label .
另请参阅 labelText ().
设置 filters 以用于文件对话框,从 MIME 类型列表。
Convenience method for setNameFilters ()。使用 QMimeType to create a name filter from the glob patterns and description defined in each MIME type.
Use application/octet-stream for the "All files (*)" filter, since that is the base MIME type for all files.
Calling setMimeTypeFilters overrides any previously set name filters, and changes the return value of nameFilters ().
QStringList mimeTypeFilters({"image/jpeg", // will show "JPEG image (*.jpeg *.jpg *.jpe) "image/png", // will show "PNG image (*.png)" "application/octet-stream" // will show "All files (*)" }); QFileDialog dialog(this); dialog.setMimeTypeFilters(mimeTypeFilters); dialog.exec();
另请参阅 mimeTypeFilters ().
将用于文件对话框的过滤器设为给定 filter .
若 filter contains a pair of parentheses containing one or more filename-wildcard patterns, separated by spaces, then only the text contained in the parentheses is used as the filter. This means that these calls are all equivalent:
dialog.setNameFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)"); dialog.setNameFilter("*.cpp *.cc *.C *.cxx *.c++");
注意: With Android's native file dialog, the mime type matching the given name filter is used because only mime types are supported.
另请参阅 setMimeTypeFilters () 和 setNameFilters ().
设置 filters 用于文件对话框。
Note that the filter
*.*
is not portable, because the historical assumption that the file extension determines the file type is not consistent on every operating system. It is possible to have a file with no dot in its name (for example,
Makefile
). In a native Windows file dialog,
*.*
matches such files, while in other types of file dialogs it might not match. So, it's better to use
*
if you mean to select any file.
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" }); QFileDialog dialog(this); dialog.setNameFilters(filters); dialog.exec();
setMimeTypeFilters
() has the advantage of providing all possible name filters for each file type. For example, JPEG images have three possible extensions; if your application can open such files, selecting the
image/jpeg
mime type as a filter allows you to open all of them.
另请参阅 nameFilters ().
设置给定 option 为被启用若 on 为 true;否则,清零给定 option .
选项 (尤其 DontUseNativeDialog 选项) 应该设置,在改变对话框属性 (或展示对话框) 之前。
设置选项当对话框可见时,不保证会立即影响对话框 (从属选项和平台)。
设置选项在改变其它特性后,可能导致这些值不起作用。
另请参阅 options and testOption ().
把视图模型设为给定 proxyModel 。这很有用,若想要修改底层模型;例如:添加列、过滤数据、或添加驱动器。
移除任何现有代理模型,不删除。文件对话框拥有所有权对于 proxyModel .
另请参阅 proxyModel ().
设置 urls 位于侧边栏中。
例如:
QList<QUrl> urls; urls << QUrl::fromLocalFile("/Users/foo/Code/qt5") << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::MusicLocation).first()); QFileDialog dialog; dialog.setSidebarUrls(urls); dialog.setFileMode(QFileDialog::AnyFile); if (dialog.exec()) { // ... }
那么,文件对话框看起来像这样:
另请参阅 sidebarUrls ().
[override virtual]
void
QFileDialog::
setVisible
(
bool
visible
)
重实现: QDialog::setVisible (bool visible).
返回目前在侧边栏中的 URL 列表
另请参阅 setSidebarUrls ().
返回
true
若给定
option
被启用;否则,返回 false。
另请参阅 options and setOption ().
[signal]
void
QFileDialog::
urlSelected
(const
QUrl
&
url
)
当选定改变且对话框接受时,此信号发射带有 (可能空) 选中 url .
另请参阅 currentUrlChanged () 和 QDialog::Accepted .
[signal]
void
QFileDialog::
urlsSelected
(const
QList
<
QUrl
> &
urls
)
当选定改变且对话框接受时,此信号发射带有 (可能空) 列表选中 urls .
另请参阅 currentUrlChanged () 和 QDialog::Accepted .