QFileDialog 类提供允许用户选择文件 (或目录) 的对话框。 更多...
头: | #include <QFileDialog> |
CMake: |
find_package(Qt6 COMPONENTS Widgets REQUIRED)
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 = nullptr, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString()) | |
QFileDialog (QWidget * parent , Qt::WindowFlags flags ) | |
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 ) |
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 = QString()) |
virtual void | accept () override |
virtual void | changeEvent (QEvent * e ) override |
virtual void | done (int result ) override |
QFileDialog 类使用户能够遍历文件系统,以选择一个或多个文件或目录。
创建 QFileDialog 的最轻松方式是使用静态函数。
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 枚举了解模式的完整列表。
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 () 函数。
标准对话框 范例展示如何使用 QFileDialog 及其它内置 Qt 对话框。
默认情况下,若平台有一个本机平台文件对话框,将使用它。在此情况下,否则就不会实例化用于构造对话框的 Widget,所以相关访问程序,譬如 layout () 和 itemDelegate () 将返回 null。可以设置 DontUseNativeDialog 选项以确保将使用基于 Widget 的实现而不是本机对话框。
另请参阅 QDir , QFileInfo , QFile , QColorDialog , QFontDialog , 标准对话框范例 ,和 Qt Widgets - 应用程序范例 .
常量 | 值 |
---|---|
QFileDialog::AcceptOpen
|
0
|
QFileDialog::AcceptSave
|
1
|
常量 | 值 |
---|---|
QFileDialog::LookIn
|
0
|
QFileDialog::FileName
|
1
|
QFileDialog::FileType
|
2
|
QFileDialog::Accept
|
3
|
QFileDialog::Reject
|
4
|
此枚举被用于指示用户可以在文件对话框中选择什么。即:对话框将返回什么,若用户点击 OK。
常量 | 值 | 描述 |
---|---|---|
QFileDialog::AnyFile
|
0
|
文件的名称,无论它是否存在。 |
QFileDialog::ExistingFile
|
1
|
单个现有文件的名称。 |
QFileDialog::Directory
|
2
|
目录的名称。显示文件和目录两者。然而,本机 Windows 文件对话框不支持在目录选取器中显示文件。 |
QFileDialog::ExistingFiles
|
3
|
零个或多个现有文件的名称。 |
另请参阅 setFileMode ().
常量 | 值 | 描述 |
---|---|---|
QFileDialog::ShowDirsOnly
|
0x00000001
|
Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.) |
QFileDialog::DontResolveSymlinks
|
0x00000002
|
不解析文件对话框中的符号链接。默认情况下,解析符号链接。 |
QFileDialog::DontConfirmOverwrite
|
0x00000004
|
不要求确认,若选中现有文件。默认情况下,请求确认。 |
Note: This opption is not supported on macOS when using the native file dialog.
常量 | 值 | 描述 |
---|---|---|
QFileDialog::DontUseNativeDialog
|
0x00000008
|
Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog 包含 Q_OBJECT macro, or the platform does not have a native dialog of the type that you require. |
注意: This option must be set before changing dialog properties or showing the dialog.
常量 | 值 | 描述 |
---|---|---|
QFileDialog::ReadOnly
|
0x00000010
|
指示模型为只读。 |
QFileDialog::HideNameFilterDetails
|
0x00000020
|
指示是否隐藏文件名过滤器细节。 |
QFileDialog::DontUseCustomDirectoryIcons
|
0x00000040
|
Always use the default directory icon. Some platforms allow the user to set a different icon. Custom icon lookup cause a big performance impact over network or removable drives. Setting this will enable the QFileIconProvider::DontUseCustomDirectoryIcons option in the icon provider. This enum value was added in Qt 5.2. |
Options 类型是 typedef 对于 QFlags <Option>。它存储 Option 值的 OR 组合。
This enum describes the view mode of the file dialog; i.e. what information about each file will be displayed.
常量 | 值 | 描述 |
---|---|---|
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 will be added to the filename if it has no suffix already. 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 ) |
This property holds the file mode of the dialog
The file mode defines the number and type of items that the user is expected to select in the dialog.
默认情况下,此特性被设为 AnyFile .
This function will set 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
默认情况下,所有选项是被禁用的。
Options (particularly the DontUseNativeDialogs option) should be set before changing dialog properties or showing the dialog.
设置选项当对话框可见时,不保证会立即影响对话框 (从属选项和平台)。
设置选项在改变其它特性后,可能导致这些值不起作用。
访问函数:
QFileDialog::Options | options () const |
void | setOptions (QFileDialog::Options options ) |
另请参阅 setOption () 和 testOption ().
[since 5.6]
supportedSchemes
:
QStringList
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 will be 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). Supported for local files ("file" scheme) is implicit and always enabled; it is not necessary to include it in the restriction.
该特性在 Qt 5.6 引入。
访问函数:
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 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 .
构造文件对话框采用给定 parent 和小部件 flags .
[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, since 5.2]
void
QFileDialog::
currentUrlChanged
(const
QUrl
&
url
)
When the current file changes, this signal is emitted with the new file URL as the url 参数。
该函数在 Qt 5.2 引入。
另请参阅 urlsSelected ().
[signal]
void
QFileDialog::
directoryEntered
(const
QString
&
directory
)
This signal is emitted for local operations when the user enters a directory .
[signal, since 5.2]
void
QFileDialog::
directoryUrlEntered
(const
QUrl
&
directory
)
此信号被发射当用户键入 directory .
该函数在 Qt 5.2 引入。
[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 .
[signal]
void
QFileDialog::
filterSelected
(const
QString
&
filter
)
此信号被发射,当用户选择 filter .
[signal, since 5.2]
void
QFileDialog::
urlSelected
(const
QUrl
&
url
)
当选定改变且对话框接受时,此信号发射带有 (可能空) 选中 url .
该函数在 Qt 5.2 引入。
另请参阅 currentUrlChanged () 和 QDialog::Accepted .
[signal, since 5.2]
void
QFileDialog::
urlsSelected
(const
QList
<
QUrl
> &
urls
)
当选定改变且对话框接受时,此信号发射带有 (可能空) 列表选中 urls .
该函数在 Qt 5.2 引入。
另请参阅 currentUrlChanged () 和 QDialog::Accepted .
[虚拟]
QFileDialog::
~QFileDialog
()
销毁文件对话框。
[override virtual protected]
void
QFileDialog::
accept
()
重实现: QDialog::accept ().
[override virtual protected]
void
QFileDialog::
changeEvent
(
QEvent
*
e
)
重实现: QWidget::changeEvent (QEvent *event).
返回目前显示在对话框中的目录。
另请参阅 setDirectory ().
[since 5.2]
QUrl
QFileDialog::
directoryUrl
() const
Returns the url of the directory currently being displayed in the dialog.
该函数在 Qt 5.2 引入。
另请参阅 setDirectoryUrl ().
[override virtual protected]
void
QFileDialog::
done
(
int
result
)
重实现: QDialog::done (int r).
返回所用过滤器,当显示文件时。
另请参阅 setFilter ().
[static]
QString
QFileDialog::
getExistingDirectory
(
QWidget
*
parent
= nullptr, const
QString
&
caption
= QString(), const
QString
&
dir
= QString(),
QFileDialog::Options
options
= ShowDirsOnly)
这是将返回由用户选择的现有目录的方便静态函数。
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
此函数创建模态文件对话框采用给定
parent
小部件。若
parent
不是
nullptr
,对话框将展示在父级 Widget 中心。
The dialog's working directory is set to dir , and the caption is set to caption . Either of these may be an empty string in which case the current directory and a default caption will be used respectively.
options 自变量保持有关如何运行对话框的各种选项,见 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 will use 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 DontUseNativeDialog to display files using a QFileDialog .
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
包括
DontResolveSymlinks
, the file dialog will treat symlinks as regular directories.
On Windows, the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if
parent
不是
nullptr
then it will position the dialog just below the parent's title bar.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getSaveFileName ().
[static, since 5.2]
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 will return an existing directory selected by the user. If the user presses Cancel, it returns an empty url.
函数的用法类似于 QFileDialog::getExistingDirectory ()。尤其 parent , caption , dir and options 以准确相同方式被使用。
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 .
supportedSchemes argument allows to restrict the type of URLs the user will be 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). Supported 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 will use the native file dialog and not a QFileDialog . On platforms which don't support selecting remote files, Qt will allow to select only local files.
该函数在 Qt 5.2 引入。
另请参阅 getExistingDirectory (), getOpenFileUrl (), getOpenFileUrls (),和 getSaveFileUrl ().
[static, since 5.13]
void
QFileDialog::
getOpenFileContent
(const
QString
&
nameFilter
, const
std::function
<
void
(const
QString
&, const
QByteArray
&)> &
fileOpenCompleted
)
This is a convenience static function that will return the content of a file selected by the user.
This function is used to access local files on Qt for WebAssembly, where the web sandbox places restrictions on how such access may happen. Its implementation will make the browser display a native file dialog, where the user makes the file selection based on the parameter nameFilter .
It can also be used on other platforms, where it will fall back to using 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);
该函数在 Qt 5.13 引入。
[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
,对话框将展示在父级 Widget 中心。
文件对话框的工作目录将被设为 dir 。若 dir includes a file name, the file will be selected. Only files that match the given filter 才被展示。选中过滤器被设为 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)"
options 自变量保持有关如何运行对话框的各种选项,见 QFileDialog::Option 枚举了解可以传递标志的更多有关信息。
把对话框标题设为 caption 。若 caption is not specified then a default caption will be used.
在 Windows 和 macOS,此静态函数将使用本机文件对话框而不是 QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if
parent
不是
nullptr
then it will position 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
包括
DontResolveSymlinks
, the file dialog will treat symlinks as regular directories.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should 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 will return 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
,对话框将展示在父级 Widget 中心。
文件对话框的工作目录将被设为 dir 。若 dir includes a file name, the file will be 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 may 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 is not specified then a default caption will be used.
在 Windows 和 macOS,此静态函数将使用本机文件对话框而不是 QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if
parent
不是
nullptr
then it will position 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 should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getSaveFileName (),和 getExistingDirectory ().
[static, since 5.2]
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 以准确相同方式被使用。
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 .
supportedSchemes argument allows to restrict the type of URLs the user will be 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). Supported 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 will use the native file dialog and not a QFileDialog . On platforms which don't support selecting remote files, Qt will allow to select only local files.
该函数在 Qt 5.2 引入。
另请参阅 getOpenFileName (), getOpenFileUrls (), getSaveFileUrl (),和 getExistingDirectoryUrl ().
[static, since 5.2]
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 will return 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 以准确相同方式被使用。
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 .
supportedSchemes argument allows to restrict the type of URLs the user will be 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). Supported 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 will use the native file dialog and not a QFileDialog . On platforms which don't support selecting remote files, Qt will allow to select only local files.
该函数在 Qt 5.2 引入。
另请参阅 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())
这是将返回用户选择文件名的方便静态函数。文件不必存在。
它创建模态文件对话框采用给定
parent
小部件。若
parent
不是
nullptr
,对话框将展示在父级 Widget 中心。
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "/home/jana/untitled.png", tr("Images (*.png *.xpm *.jpg)"));
文件对话框的工作目录将被设为 dir 。若 dir 包括文件名,文件将被选中。文件匹配 filter 才被展示。选中过滤器被设为 selectedFilter 。参数 dir , selectedFilter ,和 filter 可以是空字符串。多个过滤器采用 ;; 分隔。例如:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
options 自变量保持有关如何运行对话框的各种选项,见 QFileDialog::Option 枚举了解可以传递标志的更多有关信息。
可以选取默认过滤器通过设置 selectedFilter 到期望值。
把对话框标题设为 caption 。若 caption 未指定,将使用默认标题。
在 Windows 和 macOS,此静态函数将使用本机文件对话框而不是 QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if
parent
不是
nullptr
then it will position 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 will change to
/var/tmp
after entering
/usr/tmp
。若
options
包括
DontResolveSymlinks
the file dialog will treat symlinks as regular directories.
警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getExistingDirectory ().
[static, since 5.2]
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 以准确相同方式被使用。
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 .
supportedSchemes argument allows to restrict the type of URLs the user will be able to select. It is a way for the application to declare the protocols it will support to save the file content. An empty list means that no restriction is applied (the default). Supported 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 will use the native file dialog and not a QFileDialog . On platforms which don't support selecting remote files, Qt will allow to select only local files.
该函数在 Qt 5.2 引入。
另请参阅 getSaveFileName (), getOpenFileUrl (), getOpenFileUrls (),和 getExistingDirectoryUrl ().
以路径列表形式返回文件对话框的浏览历史。
另请参阅 setHistory ().
返回文件对话框使用的图标提供程序。
另请参阅 setIconProvider ().
返回用于在文件对话框视图中呈现项的项委托。
另请参阅 setItemDelegate ().
返回文件对话框展示的文本在指定 label .
另请参阅 setLabelText ().
[since 5.2]
QStringList
QFileDialog::
mimeTypeFilters
() const
返回在此文件对话框中运转的 MIME 类型过滤器。
该函数在 Qt 5.2 引入。
另请参阅 setMimeTypeFilters ().
返回在此文件对话框中运转的文件类型过滤器。
另请参阅 setNameFilters ().
This function connects one of its signals to the slot specified by receiver and member . The specific signal depends is filesSelected () 若 fileMode is ExistingFiles and fileSelected () 若 fileMode is anything else.
将从槽断开信号连接,当关闭对话框时。
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, since 5.14]
void
QFileDialog::
saveFileContent
(const
QByteArray
&
fileContent
, const
QString
&
fileNameHint
= QString())
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.
This function is used to save files to the local file system on Qt for WebAssembly, where the web sandbox places restrictions on how such access may happen. Its implementation will make the browser display a native file dialog, where the user makes the file selection.
It can also be used on other platforms, where it will fall back to using QFileDialog .
The function is asynchronous and returns immediately.
QByteArray imageData; // obtained from e.g. QImage::save() QFileDialog::saveFile("myimage.png", imageData);
该函数在 Qt 5.14 引入。
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 ().
[since 5.2]
void
QFileDialog::
selectMimeTypeFilter
(const
QString
&
filter
)
设置当前 MIME 类型 filter .
该函数在 Qt 5.2 引入。
设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.
另请参阅 setNameFilter (), setNameFilters (),和 selectedNameFilter ().
[since 5.2]
void
QFileDialog::
selectUrl
(const
QUrl
&
url
)
选择给定 url 在文件对话框。
注意: 非本机 QFileDialog 仅支持本地文件。
该函数在 Qt 5.2 引入。
另请参阅 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 ().
[since 5.9]
QString
QFileDialog::
selectedMimeTypeFilter
() const
Returns The mimetype of the file that the user selected in the file dialog.
该函数在 Qt 5.9 引入。
Returns the filter that the user selected in the file dialog.
另请参阅 selectedFiles ().
[since 5.2]
QList
<
QUrl
> QFileDialog::
selectedUrls
() const
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.
该函数在 Qt 5.2 引入。
另请参阅 selectedNameFilter () 和 selectUrl ().
设置文件对话框的当前 directory .
注意:
在 iOS,若设置
directory
to
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last()
, a native image picker dialog will be 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 ().
这是重载函数。
[since 5.2]
void
QFileDialog::
setDirectoryUrl
(const
QUrl
&
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 添加。
该函数在 Qt 5.2 引入。
另请参阅 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 ().
[since 5.2]
void
QFileDialog::
setMimeTypeFilters
(const
QStringList
&
filters
)
设置 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();
该函数在 Qt 5.2 引入。
另请参阅 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,
*.*
will match such files, while in other types of file dialogs it may not. So it is 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 will allow you to open all of them.
另请参阅 nameFilters ().
设置给定 option 为被启用若 on 为 true;否则,清零给定 option .
Options (particularly the DontUseNativeDialogs option) should be set before changing dialog properties or showing the dialog.
设置选项当对话框可见时,不保证会立即影响对话框 (从属选项和平台)。
设置选项在改变其它特性后,可能导致这些值不起作用。
另请参阅 options and testOption ().
把视图模型设为给定 proxyModel 。这很有用,若想要修改底层模型;例如:添加列、过滤数据、或添加驱动器。
Any existing proxy model will be removed, but not deleted. The file dialog will take ownership of the 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。