QFileDialog Class
The QFileDialog class provides a dialog that allow users to select files or directories. More...
| Header: | #include <QFileDialog> |
| qmake: | QT += widgets |
| Inherits: | QDialog |
Public Types
| enum | AcceptMode { AcceptOpen, AcceptSave } |
| enum | DialogLabel { LookIn, FileName, FileType, Accept, Reject } |
| enum | FileMode { AnyFile, ExistingFile, Directory, ExistingFiles, DirectoryOnly } |
| enum | Option { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseNativeDialog, ReadOnly, …, DontUseCustomDirectoryIcons } |
| enum | ViewMode { Detail, List } |
Properties
|
|
Public Functions
| QFileDialog::AcceptMode | acceptMode() const |
| QString | defaultSuffix() const |
| QFileDialog::FileMode | fileMode() const |
| QFileDialog::Options | options() const |
| void | setAcceptMode(QFileDialog::AcceptMode mode) |
| void | setDefaultSuffix(const QString &suffix) |
| void | setFileMode(QFileDialog::FileMode mode) |
| void | setOptions(QFileDialog::Options options) |
| void | setSupportedSchemes(const QStringList &schemes) |
| void | setViewMode(QFileDialog::ViewMode mode) |
| QStringList | supportedSchemes() const |
| QFileDialog::ViewMode | viewMode() const |
Detailed Description
The 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, and the window title is set to "Open Image".
If you want to use multiple filters, separate each one with two semicolons. For example:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
You can create your own QFileDialog without using the static functions. By calling setFileMode(), you can specify what the user must select in the dialog:
QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile);
In the above example, the mode of the file dialog is set to AnyFile, meaning that the user can select any file, or even specify a file that doesn't exist. This mode is useful for creating a "Save As" file dialog. Use ExistingFile if the user must select an existing file, or Directory if only a directory may be selected. See the QFileDialog::FileMode enum for the complete list of modes.
The fileMode property contains the mode of operation for the dialog; this indicates what types of objects the user is expected to select. Use setNameFilter() to set the dialog's file filter. For example:
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
In the above example, the filter is set to "Images (*.png *.xpm *.jpg)", this means that only files with the extension png, xpm, or jpg will be shown in the QFileDialog. You can apply several filters by using setNameFilters(). Use selectNameFilter() to select one of the filters you've given as the file dialog's default filter.
The file dialog has two view modes: List and Detail. List presents the contents of the current directory as a list of file and directory names. Detail also displays a list of file and directory names, but provides additional information alongside each name, such as the file size and modification date. Set the mode with setViewMode():
dialog.setViewMode(QFileDialog::Detail);
The last important function you will need to use when creating your own file dialog is selectedFiles().
QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
In the above example, a modal file dialog is created and shown. If the user clicked OK, the file they selected is put in fileName.
The dialog's working directory can be set with setDirectory(). Each file in the current directory can be selected using the selectFile() function.
The Standard Dialogs example shows how to use QFileDialog as well as other built-in Qt dialogs.
By default, a platform-native file dialog will be used if the platform has one. In that case, the widgets which would otherwise be used to construct the dialog will not be instantiated, so related accessors such as layout() and itemDelegate() will 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 to ensure that the widget-based implementation will be used instead of the native dialog.
See also QDir, QFileInfo, QFile, QColorDialog, QFontDialog, Standard Dialogs Example, and Application Example.
Member Type Documentation
enum QFileDialog::AcceptMode
| Constant | Value |
|---|---|
QFileDialog::AcceptOpen | 0 |
QFileDialog::AcceptSave | 1 |
enum QFileDialog::DialogLabel
| Constant | Value |
|---|---|
QFileDialog::LookIn | 0 |
QFileDialog::FileName | 1 |
QFileDialog::FileType | 2 |
QFileDialog::Accept | 3 |
QFileDialog::Reject | 4 |
enum QFileDialog::FileMode
This enum is used to indicate what the user may select in the file dialog; i.e. what the dialog will return if the user clicks OK.
| Constant | Value | Description |
|---|---|---|
QFileDialog::AnyFile | 0 | The name of a file, whether it exists or not. |
QFileDialog::ExistingFile | 1 | The name of a single existing file. |
QFileDialog::Directory | 2 | The name of a directory. Both files and directories are displayed. However, the native Windows file dialog does not support displaying files in the directory chooser. |
QFileDialog::ExistingFiles | 3 | The names of zero or more existing files. |
This value is obsolete since Qt 4.5:
| Constant | Value | Description |
|---|---|---|
QFileDialog::DirectoryOnly | 4 | Use Directory and setOption(ShowDirsOnly, true) instead. |
See also setFileMode().
enum QFileDialog::Option
| Constant | Value | Description |
|---|---|---|
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. |
Note: This opption is not supported on macOS when using the native file dialog.
| Constant | Value | Description |
|---|---|---|
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. |
Note: This option must be set before changing dialog properties or showing the dialog.
| Constant | Value | Description |
|---|---|---|
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() instead. |
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. |
enum QFileDialog::ViewMode
This enum describes the view mode of the file dialog; i.e. what information about each file will be displayed.
| Constant | Value | Description |
|---|---|---|
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. |
See also setViewMode().
Property Documentation
acceptMode : AcceptMode
This property holds the accept mode of the dialog
The action mode defines whether the dialog is for opening or saving files.
By default, this property is set to AcceptOpen.
Access functions:
| QFileDialog::AcceptMode | acceptMode() const |
| void | setAcceptMode(QFileDialog::AcceptMode mode) |
See also 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.
Access functions:
| 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.
By default, this property is set to AnyFile.
This function will set the labels for the FileName and Accept DialogLabels. It is possible to set custom text after the call to setFileMode().
Access functions:
| QFileDialog::FileMode | fileMode() const |
| void | setFileMode(QFileDialog::FileMode mode) |
See also FileMode.
options : Options
This property holds the various options that affect the look and feel of the dialog
By default, all options are disabled.
Options (particularly the DontUseNativeDialogs option) should be set before changing dialog properties or showing the dialog.
Setting options while the dialog is visible is not guaranteed to have an immediate effect on the dialog (depending on the option and on the platform).
Setting options after changing other properties may cause these values to have no effect.
This property was introduced in Qt 4.5.
Access functions:
| QFileDialog::Options | options() const |
| void | setOptions(QFileDialog::Options options) |
See also setOption() and 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.
This property was introduced in Qt 5.6.
Access functions:
| QStringList | supportedSchemes() const |
| void | setSupportedSchemes(const QStringList &schemes) |
viewMode : ViewMode
This property holds the way files and directories are displayed in the dialog
By default, the Detail mode is used to display information about files and directories.
Access functions:
| QFileDialog::ViewMode | viewMode() const |
| void | setViewMode(QFileDialog::ViewMode mode) |
See also ViewMode.