Qt 5.9 | Qt Quick Dialogs | QML Types | FileDialog QML Type

Qt 5.9.2 Reference Documentation

# FileDialog QML Type

Dialog component for choosing files from a local filesystem. More...

Import Statement: | import QtQuick.Dialogs 1.2
Since: | Qt 5.1

- List of all members, including inherited members

## Properties

- fileUrl : url
- fileUrls : list<url>
- folder : url
- modality : Qt::WindowModality
- nameFilters : list<string>
- selectExisting : bool
- selectFolder : bool
- selectMultiple : bool
- selectedNameFilter : string
- shortcuts : Object
- sidebarVisible : bool
- title : string
- visible : bool

## Methods

- void close()
- void open()

## Detailed Description

FileDialog provides a basic file chooser: it allows the user to select existing files and/or directories, or create new filenames. The dialog is initially invisible. You need to set the properties as desired first, then set visible to true or call open().

Here is a minimal example to open a file dialog and exit after the user chooses a file:

      import QtQuick 2.2
      import QtQuick.Dialogs 1.0

      FileDialog {
          id: fileDialog
          title: "Please choose a file"
          folder: shortcuts.home
          onAccepted: {
              console.log("You chose: " + fileDialog.fileUrls)
              Qt.quit()
          }
          onRejected: {
              console.log("Canceled")
              Qt.quit()
          }
          Component.onCompleted: visible = true
      }

A FileDialog window is automatically transient for its parent window. So whether you declare the dialog inside an Item or inside a Window, the dialog will appear centered over the window containing the item, or over the Window that you declared.

The implementation of FileDialog will be a platform file dialog if possible. If that isn't possible, then it will try to instantiate a QFileDialog. If that also isn't possible, then it will fall back to a QML implementation, DefaultFileDialog.qml. In that case you can customize the appearance by editing this file. DefaultFileDialog.qml contains a Rectangle to hold the dialog's contents, because certain embedded systems do not support multiple top-level windows. When the dialog becomes visible, it will automatically be wrapped in a Window if possible, or simply reparented on top of the main window if there can only be one window.

The QML implementation has a sidebar containing shortcuts to common platform-specific locations, and user-modifiable favorites. It uses application-specific settings to store the user's favorites, as well as other user-modifiable state, such as whether or not the sidebar is shown, the positions of the splitters, and the dialog size. The settings are stored in a section called `QQControlsFileDialog` of the application-specific QSettings. For example when testing an application with the qml tool, the `QQControlsFileDialog` section will be created in the `Qml Runtime` settings file (or registry entry). If an application is started via a custom C++ main() function, it is recommended to set the name, organization and domain in order to control the location of the application's settings. If you use Settings objects in other parts of an application, they will be stored in other sections of the same file.

QFileDialog stores its settings globally instead of per-application. Platform-native file dialogs may or may not store settings in various platform-dependent ways.

## Property Documentation

fileUrl : url

The path of the file which was selected by the user.

Note: This property is set only if exactly one file was selected. In all other cases, it will be empty.

See also fileUrls.

fileUrls : list<url>

The list of file paths which were selected by the user.

folder : url

The path to the currently selected folder. Setting this property before invoking open() will cause the file browser to be initially positioned on the specified folder.

The value of this property is also updated after the dialog is closed.

By default, the url is empty.

Note: On iOS, if you set folder to shortcuts.pictures, a native image picker dialog will be used for accessing the user's photo album. The URL returned can be set as source for Image. This feature was added in Qt 5.5.

See also shortcuts.

modality : Qt::WindowModality

Whether the dialog should be shown modal with respect to the window containing the dialog's parent Item, modal with respect to the whole application, or non-modal.

By default it is `Qt.WindowModal`.

Modality does not mean that there are any blocking calls to wait for the dialog to be accepted or rejected; it's only that the user will be prevented from interacting with the parent window and/or the application windows at the same time. You probably need to write an onAccepted handler to actually load or save the chosen file.

nameFilters : list<string>

A list of strings to be used as file name filters. Each string can be a space-separated list of filters; filters may include the ? and * wildcards. The list of filters can also be enclosed in parentheses and a textual description of the filter can be provided.

For example:

      FileDialog {
          nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
      }

Note: Directories are not excluded by filters.

See also selectedNameFilter.

selectExisting : bool

Whether only existing files or directories can be selected.

By default, this property is true. This property must be set to the desired value before opening the dialog. Setting this property to false implies that the dialog is for naming a file to which to save something, or naming a folder to be created; therefore selectMultiple must be false.

selectFolder : bool

Whether the selected item should be a folder.

By default, this property is false. This property must be set to the desired value before opening the dialog. Setting this property to true implies that selectMultiple must be false and selectExisting must be true.

selectMultiple : bool

Whether more than one filename can be selected.

By default, this property is false. This property must be set to the desired value before opening the dialog. Setting this property to true implies that selectExisting must be true.

selectedNameFilter : string

Which of the nameFilters is currently selected.

This property can be set before the dialog is visible, to set the default name filter, and can also be set while the dialog is visible to set the current name filter. It is also updated when the user selects a different filter.

shortcuts : Object

A map of some useful paths from QStandardPaths to their URLs. Each path is verified to exist on the user's computer before being added to this list, at the time when the FileDialog is created.

`desktop` | QStandardPaths::DesktopLocation | The user's desktop directory.
`documents` | QStandardPaths::DocumentsLocation | The directory containing user document files.
`home` | QStandardPaths::HomeLocation | The user's home directory.
`music` | QStandardPaths::MusicLocation | The directory containing the user's music or other audio files.
`movies` | QStandardPaths::MoviesLocation | The directory containing the user's movies and videos.
`pictures` | QStandardPaths::PicturesLocation | The directory containing the user's pictures or photos. It is always a kind of `file`: URL; but on some platforms, it will be specialized, such that the FileDialog will be realized as a gallery browser dialog.

For example, `shortcuts.home` will provide the URL of the user's home directory.

This QML property was introduced in Qt 5.5.

sidebarVisible : bool

This property holds whether the sidebar in the dialog containing shortcuts and bookmarks is visible. By default it depends on the setting stored in the `QQControlsFileDialog` section of the application's Settings.

This QML property was introduced in Qt 5.4.

title : string

The title of the dialog window.

visible : bool

This property holds whether the dialog is visible. By default this is false.

See also modality.

## Method Documentation

void close()

Closes the dialog.

void open()

Shows the dialog to the user. It is equivalent to setting visible to true.