QFileDialog 类

QFileDialog class provides a dialog that allow users to select files or directories. 更多...

头: #include <QFileDialog>
qmake: QT += widgets
继承: QDialog

公共类型

enum AcceptMode { AcceptOpen, AcceptSave }
enum DialogLabel { LookIn, FileName, FileType, Accept, Reject }
enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles, DirectoryOnly }
enum 选项 { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseNativeDialog, ..., DontUseCustomDirectoryIcons }
flags 选项
enum ViewMode { Detail, List }

特性

公共函数

QFileDialog (QWidget * parent , Qt::WindowFlags flags )
QFileDialog (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString())
~QFileDialog ()
AcceptMode acceptMode () const
QString defaultSuffix () const
QDir directory () const
QUrl directoryUrl () const
FileMode fileMode () const
QDir::Filters filter () const
QStringList history () const
QFileIconProvider * iconProvider () const
QAbstractItemDelegate * itemDelegate () const
QString labelText (DialogLabel label ) const
QStringList mimeTypeFilters () const
QStringList nameFilters () const
void open (QObject * receiver , const char * member )
选项 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 (AcceptMode mode )
void setDefaultSuffix (const QString & suffix )
void setDirectory (const QString & directory )
void setDirectory (const QDir & directory )
void setDirectoryUrl (const QUrl & directory )
void setFileMode (FileMode mode )
void setFilter (QDir::Filters 过滤 )
void setHistory (const QStringList & paths )
void setIconProvider (QFileIconProvider * provider )
void setItemDelegate (QAbstractItemDelegate * delegate )
void setLabelText (DialogLabel label , const QString & text )
void setMimeTypeFilters (const QStringList & 过滤 )
void setNameFilter (const QString & filter )
void setNameFilters (const QStringList & 过滤 )
void setOption (Option option , bool on = true)
void setOptions (Options options )
void setProxyModel (QAbstractProxyModel * proxyModel )
void setSidebarUrls (const QList<QUrl> & urls )
void setSupportedSchemes (const QStringList & schemes )
void setViewMode (ViewMode mode )
QList<QUrl> sidebarUrls () const
QStringList supportedSchemes () const
bool testOption (Option option ) const
ViewMode viewMode () const

重实现公共函数

virtual void setVisible (bool visible )

信号

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 = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly)
QUrl getExistingDirectoryUrl (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), Options options = ShowDirsOnly, const QStringList & supportedSchemes = QStringList())
QString getOpenFileName (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options())
QStringList getOpenFileNames (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options())
QUrl getOpenFileUrl (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options(), const QStringList & supportedSchemes = QStringList())
QList<QUrl> getOpenFileUrls (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options(), const QStringList & supportedSchemes = QStringList())
QString getSaveFileName (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options())
QUrl getSaveFileUrl (QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, Options options = Options(), const QStringList & supportedSchemes = QStringList())

重实现保护函数

virtual void accept ()
virtual void changeEvent (QEvent * e )
virtual void done (int result )

额外继承成员

详细描述

QFileDialog class provides a dialog that allow users to select files or directories.

QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.

The easiest way to create a QFileDialog is to use the static functions.

fileName = QFileDialog::getOpenFileName(this,
    tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
					

In the above example, a modal QFileDialog is created using a static function. The dialog initially displays the contents of the "/home/jana" directory, and displays files matching the patterns given in the string "Image Files (*.png *.jpg *.bmp)". The parent of the file dialog is set to this ,且窗口标题被设为 Open Image。

若想要使用多个过滤器,分隔每过滤器采用 two 分号。例如:

"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
					

You can create your own QFileDialog without using the static functions. By calling 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 will be shown in the QFileDialog . You can apply several filters by using 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 as well as other built-in Qt dialogs.

默认情况下,若平台有一个平台本机文件对话框,将使用它。在这种情况下,原本用于构造对话框的 Widget 将不会被实例化,因此相关访问器,譬如 layout () 和 itemDelegate () 将返回 null。可以设置 DontUseNativeDialog 选项以确保将使用基于 Widget 的实现而不是本机对话框。

另请参阅 QDir , QFileInfo , QFile , QColorDialog , QFontDialog , 标准对话框范例 ,和 应用程序范例 .

成员类型文档编制

enum QFileDialog:: AcceptMode

常量
QFileDialog::AcceptOpen 0
QFileDialog::AcceptSave 1

enum QFileDialog:: DialogLabel

常量
QFileDialog::LookIn 0
QFileDialog::FileName 1
QFileDialog::FileType 2
QFileDialog::Accept 3
QFileDialog::Reject 4

enum QFileDialog:: FileMode

此枚举被用于指示用户可以在文件对话框中选择什么。即:对话框将返回什么,若用户点击 OK。

常量 描述
QFileDialog::AnyFile 0 文件的名称,无论它是否存在。
QFileDialog::ExistingFile 1 单个现有文件的名称。
QFileDialog::Directory 2 目录的名称。显示文件和目录两者。然而,本机 Windows 文件对话框不支持在目录选取器中显示文件。
QFileDialog::ExistingFiles 3 零个或多个现有文件的名称。

从 Qt 4.5 起此值已过时:

常量 描述
QFileDialog::DirectoryOnly 4 使用 Directory and setOption ( ShowDirsOnly , true) 代替。

另请参阅 setFileMode ().

enum QFileDialog:: 选项
flags QFileDialog:: 选项

常量 描述
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 Don't resolve symlinks in the file dialog. By default symlinks are resolved.
QFileDialog::DontConfirmOverwrite 0x00000004 Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
QFileDialog::DontUseNativeDialog 0x00000010 Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT macro, or the platform does not have a native dialog of the type that you require.
QFileDialog::ReadOnly 0x00000020 Indicates that the model is readonly.
QFileDialog::HideNameFilterDetails 0x00000040 Indicates if the file name filter details are hidden or not.
QFileDialog::DontUseSheet 0x00000008 In previous versions of Qt, the static functions would create a sheet by default if the static function was given a parent. This is no longer supported and does nothing in Qt 4.5, The static functions will always be an application modal dialog. If you want to use sheets, use QFileDialog::open () 代替。
QFileDialog::DontUseCustomDirectoryIcons 0x00000080 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.

The Options type is a typedef for QFlags <Option>. It stores an OR combination of Option values.

enum QFileDialog:: ViewMode

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 ().

特性文档编制

acceptMode : AcceptMode

This property holds the accept mode of the dialog

The action mode defines whether the dialog is for opening or saving files.

默认情况下,此特性被设为 AcceptOpen .

访问函数:

AcceptMode acceptMode () const
void setAcceptMode (AcceptMode mode )

另请参阅 AcceptMode .

defaultSuffix : QString

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 )

fileMode : FileMode

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().

访问函数:

FileMode fileMode () const
void setFileMode (FileMode mode )

另请参阅 FileMode .

options : 选项

This property holds the various options that affect the look and feel of the dialog

默认情况下,所有选项是被禁用的。

Options should be set before showing the dialog. Setting them while the dialog is visible is not guaranteed to have an immediate effect on the dialog (depending on the option and on the platform).

该特性在 Qt 4.5 引入。

访问函数:

选项 options () const
void setOptions (Options options )

另请参阅 setOption () 和 testOption ().

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 )

viewMode : ViewMode

This property holds the way files and directories are displayed in the dialog

默认情况下, Detail mode is used to display information about files and directories.

访问函数:

ViewMode viewMode () const
void setViewMode (ViewMode mode )

另请参阅 ViewMode .

成员函数文档编制

QFileDialog:: QFileDialog ( QWidget * parent , Qt::WindowFlags flags )

构造文件对话框采用给定 parent 和小部件 flags .

QFileDialog:: QFileDialog ( QWidget * parent = Q_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 .

QFileDialog:: ~QFileDialog ()

销毁文件对话框。

[virtual protected] void QFileDialog:: accept ()

重实现自 QDialog::accept ().

[virtual protected] void QFileDialog:: changeEvent ( QEvent * e )

重实现自 QWidget::changeEvent ().

[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 参数。

该函数在 Qt 5.2 引入。

另请参阅 urlsSelected ().

QDir QFileDialog:: directory () const

Returns the directory currently being displayed in the dialog.

另请参阅 setDirectory ().

[signal] void QFileDialog:: directoryEntered (const QString & directory )

This signal is emitted for local operations when the user enters a directory .

该函数在 Qt 4.3 引入。

QUrl QFileDialog:: directoryUrl () const

Returns the url of the directory currently being displayed in the dialog.

该函数在 Qt 5.2 引入。

另请参阅 setDirectoryUrl ().

[signal] void QFileDialog:: directoryUrlEntered (const QUrl & directory )

此信号被发射当用户键入 directory .

该函数在 Qt 5.2 引入。

[virtual protected] void QFileDialog:: done ( int result )

重实现自 QDialog::done ().

[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 .

QDir::Filters QFileDialog:: filter () const

Returns the filter that is used when displaying files.

该函数在 Qt 4.4 引入。

另请参阅 setFilter ().

[signal] void QFileDialog:: filterSelected (const QString & filter )

This signal is emitted when the user selects a filter .

该函数在 Qt 4.3 引入。

[static] QString QFileDialog:: getExistingDirectory ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), 选项 options = ShowDirsOnly)

This is a convenience static function that will return an existing directory selected by the user.

QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                "/home",
                                                QFileDialog::ShowDirsOnly
                                                | QFileDialog::DontResolveSymlinks);
					

This function creates a modal file dialog with the given parent 小部件。若 parent is not 0, the dialog will be 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 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 includes 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 is not 0 then it will position the dialog just below the parent's title bar.

警告: Do not delete 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] QUrl QFileDialog:: getExistingDirectoryUrl ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), 选项 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] QString QFileDialog:: getOpenFileName ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 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 is not 0, the dialog will be shown centered over the parent 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 is not 0 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 includes DontResolveSymlinks , the file dialog will treat symlinks as regular directories.

警告: Do not delete 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 = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 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)");
					

This function creates a modal file dialog with the given parent 小部件。若 parent is not 0, the dialog will be shown centered over the parent 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 is not 0 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 枚举了解可以传递标志的更多有关信息。

警告: Do not delete 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] QUrl QFileDialog:: getOpenFileUrl ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 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] QList < QUrl > QFileDialog:: getOpenFileUrls ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 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 > and 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 = Q_NULLPTR, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 options = Options())

这是将返回用户选择文件名的方便静态函数。文件不必存在。

它创建模态文件对话框采用给定 parent 小部件。若 parent is not 0, the dialog will be shown centered over the parent 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 is not 0 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 includes DontResolveSymlinks the file dialog will treat symlinks as regular directories.

警告: Do not delete 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] QUrl QFileDialog:: getSaveFileUrl ( QWidget * parent = Q_NULLPTR, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = Q_NULLPTR, 选项 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 ().

QStringList QFileDialog:: history () const

以路径列表形式返回文件对话框的浏览历史。

另请参阅 setHistory ().

QFileIconProvider *QFileDialog:: iconProvider () const

返回文件对话框使用的图标提供程序。

另请参阅 setIconProvider ().

QAbstractItemDelegate *QFileDialog:: itemDelegate () const

返回用于在文件对话框视图中呈现项的项委托。

另请参阅 setItemDelegate ().

QString QFileDialog:: labelText ( DialogLabel label ) const

返回文件对话框展示的文本在指定 label .

另请参阅 setLabelText ().

QStringList QFileDialog:: mimeTypeFilters () const

返回在此文件对话框中运转的 MIME 类型过滤器。

该函数在 Qt 5.2 引入。

另请参阅 setMimeTypeFilters ().

QStringList QFileDialog:: nameFilters () const

返回在此文件对话框中运转的文件类型过滤器。

该函数在 Qt 4.4 引入。

另请参阅 setNameFilters ().

void QFileDialog:: open ( QObject * receiver , const char * member )

这是重载函数。

This function connects one of its signals to the slot specified by receiver and member . The specific signal depends is filesSelected () if fileMode is ExistingFiles and fileSelected () if fileMode is anything else.

信号将断开槽连接,当对话框被关闭时。

该函数在 Qt 4.5 引入。

QAbstractProxyModel *QFileDialog:: proxyModel () const

Returns the proxy model used by the file dialog. By default no proxy is set.

另请参阅 setProxyModel ().

bool QFileDialog:: restoreState (const QByteArray & state )

Restores the dialogs's layout, history and current directory to the state 指定。

通常,这用于结合 QSettings to restore the size from a past session.

返回 false 若有错误

该函数在 Qt 4.3 引入。

QByteArray QFileDialog:: saveState () const

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.

该函数在 Qt 4.3 引入。

void QFileDialog:: selectFile (const QString & filename )

选择给定 filename 在文件对话框。

另请参阅 selectedFiles ().

void QFileDialog:: selectMimeTypeFilter (const QString & filter )

设置当前 MIME 类型 filter .

该函数在 Qt 5.2 引入。

void QFileDialog:: selectNameFilter (const QString & filter )

设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.

该函数在 Qt 4.4 引入。

另请参阅 setNameFilter (), setNameFilters (),和 selectedNameFilter ().

void QFileDialog:: selectUrl (const QUrl & url )

选择给定 url 在文件对话框。

注意: 非本机 QFileDialog 仅支持本地文件。

该函数在 Qt 5.2 引入。

另请参阅 selectedUrls ().

QStringList QFileDialog:: selectedFiles () const

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 ().

QString QFileDialog:: selectedMimeTypeFilter () const

Returns The mimetype of the file that the user selected in the file dialog.

该函数在 Qt 5.9 引入。

QString QFileDialog:: selectedNameFilter () const

Returns the filter that the user selected in the file dialog.

该函数在 Qt 4.4 引入。

另请参阅 selectedFiles ().

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 ().

void QFileDialog:: setDirectory (const QString & directory )

设置文件对话框的当前 directory .

注意: On iOS, if you set 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 ().

void QFileDialog:: setDirectory (const QDir & directory )

这是重载函数。

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 using the scheme 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 . This feature was added in Qt 5.5.

该函数在 Qt 5.2 引入。

另请参阅 directoryUrl () 和 QUuid .

void QFileDialog:: setFilter ( QDir::Filters 过滤 )

Sets the filter used by the model to 过滤 . The filter is used to specify the kind of files that should be shown.

该函数在 Qt 4.4 引入。

另请参阅 filter ().

void QFileDialog:: setHistory (const QStringList & paths )

Sets the browsing history of the filedialog to contain the given paths .

另请参阅 history ().

void QFileDialog:: setIconProvider ( QFileIconProvider * provider )

Sets the icon provider used by the filedialog to the specified provider .

另请参阅 iconProvider ().

void QFileDialog:: setItemDelegate ( QAbstractItemDelegate * delegate )

Sets the item delegate used to render items in the views in the file dialog to the given delegate .

警告: You should not share the same instance of a delegate between views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() 信号,并尝试访问、修改或关闭已关闭的编辑器。

Note that the model used is QFileSystemModel . It has custom item data roles, which is described by the Roles enum. You can use a QFileIconProvider if you only want custom icons.

另请参阅 itemDelegate (), setIconProvider (),和 QFileSystemModel .

void QFileDialog:: setLabelText ( DialogLabel label , const QString & text )

设置 text shown in the filedialog in the specified label .

另请参阅 labelText ().

void QFileDialog:: setMimeTypeFilters (const QStringList & 过滤 )

设置 过滤 以用于文件对话框,从 MIME 类型列表。

Convenience method for setNameFilters (). Uses 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;
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 ().

void QFileDialog:: setNameFilter (const QString & filter )

Sets the filter used in the file dialog to the given 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++");
					

该函数在 Qt 4.4 引入。

另请参阅 setMimeTypeFilters () 和 setNameFilters ().

void QFileDialog:: setNameFilters (const QStringList & 过滤 )

设置 过滤 used in the file dialog.

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.

QStringList filters;
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.

该函数在 Qt 4.4 引入。

另请参阅 nameFilters ().

void QFileDialog:: setOption ( 选项 option , bool on = true)

设置给定 option 为被启用若 on 为 true;否则,清零给定 option .

该函数在 Qt 4.5 引入。

另请参阅 options and testOption ().

void QFileDialog:: setProxyModel ( QAbstractProxyModel * proxyModel )

Sets the model for the views to the given proxyModel . This is useful if you want to modify the underlying model; for example, to add columns, filter data or add drives.

Any existing proxy model will be removed, but not deleted. The file dialog will take ownership of the proxyModel .

该函数在 Qt 4.3 引入。

另请参阅 proxyModel ().

void QFileDialog:: setSidebarUrls (const QList < QUrl > & urls )

设置 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()) {
        // ...
    }
					

文件对话框看起来就像这样:

该函数在 Qt 4.3 引入。

另请参阅 sidebarUrls ().

[virtual] void QFileDialog:: setVisible ( bool visible )

重实现自 QWidget::setVisible ().

QList < QUrl > QFileDialog:: sidebarUrls () const

返回目前在侧边栏中的 URL 列表

该函数在 Qt 4.3 引入。

另请参阅 setSidebarUrls ().

bool QFileDialog:: testOption ( 选项 option ) const

返回 true 若给定 option 被启用;否则,返回 false。

该函数在 Qt 4.5 引入。

另请参阅 options and setOption ().

[signal] void QFileDialog:: urlSelected (const QUrl & url )

When the selection changes and the dialog is accepted, this signal is emitted with the (possibly empty) selected url .

该函数在 Qt 5.2 引入。

另请参阅 currentUrlChanged () 和 QDialog::Accepted .

[signal] void QFileDialog:: urlsSelected (const QList < QUrl > & urls )

When the selection changes and the dialog is accepted, this signal is emitted with the (possibly empty) list of selected urls .

该函数在 Qt 5.2 引入。

另请参阅 currentUrlChanged () 和 QDialog::Accepted .