|  | Home · All Classes · Modules

# QFileDialog Class Reference
[QtGui module]

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

Inherits QDialog.

### Types

- enum AcceptMode { AcceptOpen, AcceptSave }
- enum DialogLabel { LookIn, FileName, FileType, Accept, Reject }
- enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles, DirectoryOnly }
- enum Option { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseSheet, DontUseNativeDialog }
- class Options
- enum ViewMode { Detail, List }

### Methods

- __init__ (self, QWidget parent, Qt.WindowFlags f)
- __init__ (self, QWidget parent = None, QString caption = QString(), QString directory = QString(), QString filter = QString())
- accept (self)
- AcceptMode acceptMode (self)
- changeEvent (self, QEvent e)
- bool confirmOverwrite (self)
- QString defaultSuffix (self)
- QDir directory (self)
- done (self, int result)
- FileMode fileMode (self)
- QDir.Filters filter (self)
- QStringList filters (self)
- QStringList history (self)
- QFileIconProvider iconProvider (self)
- bool isNameFilterDetailsVisible (self)
- bool isReadOnly (self)
- QAbstractItemDelegate itemDelegate (self)
- QString labelText (self, DialogLabel label)
- QStringList nameFilters (self)
- QAbstractProxyModel proxyModel (self)
- bool resolveSymlinks (self)
- bool restoreState (self, QByteArray state)
- QByteArray saveState (self)
- QStringList selectedFiles (self)
- QString selectedFilter (self)
- QString selectedNameFilter (self)
- selectFile (self, QString filename)
- selectFilter (self, QString filter)
- selectNameFilter (self, QString filter)
- setAcceptMode (self, AcceptMode mode)
- setConfirmOverwrite (self, bool enabled)
- setDefaultSuffix (self, QString suffix)
- setDirectory (self, QString directory)
- setDirectory (self, QDir adirectory)
- setFileMode (self, FileMode mode)
- setFilter (self, QString filter)
- setFilter (self, QDir.Filters filters)
- setFilters (self, QStringList filters)
- setHistory (self, QStringList paths)
- setIconProvider (self, QFileIconProvider provider)
- setItemDelegate (self, QAbstractItemDelegate delegate)
- setLabelText (self, DialogLabel label, QString text)
- setNameFilter (self, QString filter)
- setNameFilterDetailsVisible (self, bool enabled)
- setNameFilters (self, QStringList filters)
- setProxyModel (self, QAbstractProxyModel model)
- setReadOnly (self, bool enabled)
- setResolveSymlinks (self, bool enabled)
- setSidebarUrls (self, QUrl-list urls)
- setViewMode (self, ViewMode mode)
- QUrl-list sidebarUrls (self)
- ViewMode viewMode (self)

### Static Methods

- QString getExistingDirectory (QWidget parent = None, QString caption = QString(), QString dir = QString(), Options options = QFileDialog.ShowDirsOnly)
- QString getOpenFileName (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)
- QStringList getOpenFileNames (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)
- QString getSaveFileName (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)

### Qt Signals

- void currentChanged (const QString&)
- void directoryEntered (const QString&)
- void filesSelected (const QStringList&)
- void filterSelected (const QString&)

## Detailed Description

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

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. On Windows, these static functions will call the native Windows file dialog, and on Mac OS X these static function will call the native Mac OS X file dialog.

     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.

See also QDir, QFileInfo, QFile, QPrintDialog, QColorDialog, QFontDialog, Standard Dialogs Example, and Application Example.

## Type Documentation

### QFileDialog.AcceptMode

Constant | Value
--- | ---
`QFileDialog.AcceptOpen` | `0`
`QFileDialog.AcceptSave` | `1`

### QFileDialog.DialogLabel

Constant | Value
--- | ---
`QFileDialog.LookIn` | `0`
`QFileDialog.FileName` | `1`
`QFileDialog.FileType` | `2`
`QFileDialog.Accept` | `3`
`QFileDialog.Reject` | `4`

### 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.
`QFileDialog.DirectoryOnly` | `4` | The name of a directory. The file dialog will only display directories.
`QFileDialog.ExistingFiles` | `3` | The names of zero or more existing files.

See also setFileMode().

### QFileDialog.Option

Constant | Value | Description
--- | --- | ---
`QFileDialog.ShowDirsOnly` | `0x01` | Only show directories in the file dialog. By default both files and directories are shown.
`QFileDialog.DontResolveSymlinks` | `0x02` | Don't resolve symlinks in the file dialog. By default symlinks are resolved.
`QFileDialog.DontConfirmOverwrite` | `0x04` | Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
`QFileDialog.DontUseSheet` | `0x08` | Don't make the native file dialog a sheet. By default on Mac OS X, the native file dialog is made a sheet if it has a parent that can take a sheet.
`QFileDialog.DontUseNativeDialog` | `0x10` | Don't use the native file dialog. By default on Mac OS X and Windows, the native file dialog is used.

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

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

## Method Documentation

### QFileDialog.__init__ (self, QWidget parent, Qt.WindowFlags f)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a file dialog with the given parent and widget flags.

### QFileDialog.__init__ (self, QWidget parent = None, QString caption = QString(), QString directory = QString(), QString filter = QString())

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a file dialog with the given 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.accept (self)

### AcceptMode QFileDialog.acceptMode (self)

### QFileDialog.changeEvent (self, QEvent e)

### bool QFileDialog.confirmOverwrite (self)

### QString QFileDialog.defaultSuffix (self)

### QDir QFileDialog.directory (self)

Returns the directory currently being displayed in the dialog.

See also setDirectory().

### QFileDialog.done (self, int result)

### FileMode QFileDialog.fileMode (self)

### QDir.Filters QFileDialog.filter (self)

Returns the filter that is used when displaying files.

This function was introduced in Qt 4.4.

See also setFilter().

### QStringList QFileDialog.filters (self)

### QString QFileDialog.getExistingDirectory (QWidget parent = None, QString caption = QString(), QString dir = QString(), Options options = QFileDialog.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 widget. If the 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. The options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.

Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. On Mac OS X, the dir argument is ignored, the native dialog always displays the last visited directory. On Windows CE, if the device has no native file dialog, a QFileDialog will be used.

Under 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`. If options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories.

Note that 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 under the parent's title bar.

See also getOpenFileName(), getOpenFileNames(), and getSaveFileName().

### QString QFileDialog.getOpenFileName (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)

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 widget. If the parent is not 0, the dialog will be shown centered over the parent widget.

The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected. Only files that match the given filter are shown. The filter selected is set to selectedFilter. The parameters dir, selectedFilter, and filter may be empty strings. The options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.

The dialog's caption is set to caption. If caption is not specified then a default caption will be used.

Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog.

Note that 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 under the parent's title bar.

Under 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`. If options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories.

See also getOpenFileNames(), getSaveFileName(), and getExistingDirectory().

### QStringList QFileDialog.getOpenFileNames (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)

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 widget. If the parent is not 0, the dialog will be shown centered over the parent widget.

The file dialog's working directory will be set to dir. If 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. The parameters dir, selectedFilter and filter may be empty strings.

The dialog's caption is set to caption. If caption is not specified then a default caption will be used.

Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. On Mac OS X, the dir argument is ignored, the native dialog always displays the last visited directory.

Note that 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 under the parent's title bar.

Under 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`. The options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.

Note that if you want to iterate over the list of files, you should iterate over a copy. For example:

     QStringList list = files;
     QStringList.Iterator it = list.begin();
     while(it != list.end()) {
         myProcessing(*it);
         ++it;
     }

See also getOpenFileName(), getSaveFileName(), and getExistingDirectory().

### QString QFileDialog.getSaveFileName (QWidget parent = None, QString caption = QString(), QString dir = QString(), QString filter = QString(), QString selectedFilter = None, Options options = 0)

This is a convenience static function that will return a file name selected by the user. The file does not have to exist.

It creates a modal file dialog with the given parent widget. If the 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)"));

The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected. Only files that match the filter are shown. The filter selected is set to selectedFilter. The parameters dir, selectedFilter, and filter may be empty strings. The options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.

The default filter can be chosen by setting selectedFilter to the desired value.

The dialog's caption is set to caption. If caption is not specified then a default caption will be used.

Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog.

Note that 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 under the parent's title bar. On Mac OS X, the filter argument is ignored.

Under 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`. If options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories.

See also getOpenFileName(), getOpenFileNames(), and getExistingDirectory().

### QStringList QFileDialog.history (self)

Returns the browsing history of the filedialog as a list of paths.

See also setHistory().

### QFileIconProvider QFileDialog.iconProvider (self)

Returns the icon provider used by the filedialog.

See also setIconProvider().

### bool QFileDialog.isNameFilterDetailsVisible (self)

### bool QFileDialog.isReadOnly (self)

### QAbstractItemDelegate QFileDialog.itemDelegate (self)

Returns the item delegate used to render the items in the views in the filedialog.

See also setItemDelegate().

### QString QFileDialog.labelText (self, DialogLabel label)

Returns the text shown in the filedialog in the specified label.

See also setLabelText().

### QStringList QFileDialog.nameFilters (self)

Returns the file type filters that are in operation on this file dialog.

This function was introduced in Qt 4.4.

See also setNameFilters().

### QAbstractProxyModel QFileDialog.proxyModel (self)

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

See also setProxyModel().

### bool QFileDialog.resolveSymlinks (self)

### bool QFileDialog.restoreState (self, QByteArray state)

Restores the dialogs's layout, history and current directory to the state specified.

Typically this is used in conjunction with QSettings to restore the size from a past session.

Returns false if there are errors

This function was introduced in Qt 4.3.

### QByteArray QFileDialog.saveState (self)

Saves the state of the dialog's layout, history and current directory.

Typically this is used in conjunction with QSettings to remember the size for a future session. A version number is stored as part of the data.

This function was introduced in Qt 4.3.

### QStringList QFileDialog.selectedFiles (self)

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, selectedFiles() contains the current path in the viewport.

See also selectedNameFilter() and selectFile().

### QString QFileDialog.selectedFilter (self)

### QString QFileDialog.selectedNameFilter (self)

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

This function was introduced in Qt 4.4.

See also selectedFiles().

### QFileDialog.selectFile (self, QString filename)

Selects the given filename in the file dialog.

See also selectedFiles().

### QFileDialog.selectFilter (self, QString filter)

### QFileDialog.selectNameFilter (self, QString filter)

Sets the current file type filter. Multiple filters can be passed in filter by separating them with semicolons or spaces.

This function was introduced in Qt 4.4.

See also setNameFilter(), setNameFilters(), and selectedNameFilter().

### QFileDialog.setAcceptMode (self, AcceptMode mode)

### QFileDialog.setConfirmOverwrite (self, bool enabled)

### QFileDialog.setDefaultSuffix (self, QString suffix)

### QFileDialog.setDirectory (self, QString directory)

Sets the file dialog's current directory.

See also directory().

### QFileDialog.setDirectory (self, QDir adirectory)

This is an overloaded member function, provided for convenience.

### QFileDialog.setFileMode (self, FileMode mode)

### QFileDialog.setFilter (self, QString filter)

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

This function was introduced in Qt 4.4.

See also filter().

### QFileDialog.setFilter (self, QDir.Filters filters)

### QFileDialog.setFilters (self, QStringList filters)

### QFileDialog.setHistory (self, QStringList paths)

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

See also history().

### QFileDialog.setIconProvider (self, QFileIconProvider provider)

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

See also iconProvider().

### QFileDialog.setItemDelegate (self, QAbstractItemDelegate delegate)

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

Warning: 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() signal, and attempt to access, modify or close an editor that has already been closed.

See also itemDelegate().

### QFileDialog.setLabelText (self, DialogLabel label, QString text)

Sets the text shown in the filedialog in the specified label.

See also labelText().

### QFileDialog.setNameFilter (self, QString filter)

Sets the filter used in the file dialog to the given filter.

If filter contains a pair of parentheses containing one or more of anything*something, 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++");

This function was introduced in Qt 4.4.

See also setNameFilters().

### QFileDialog.setNameFilterDetailsVisible (self, bool enabled)

### QFileDialog.setNameFilters (self, QStringList filters)

Sets the filters used in the file dialog.

     QStringList filters;
     filters << "Image files (*.png *.xpm *.jpg)"
             << "Text files (*.txt)"
             << "Any files (*)";

     QFileDialog dialog(this);
     dialog.setNameFilters(filters);
     dialog.exec();

This function was introduced in Qt 4.4.

See also nameFilters().

### QFileDialog.setProxyModel (self, QAbstractProxyModel model)

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.

This function was introduced in Qt 4.3.

See also proxyModel().

### QFileDialog.setReadOnly (self, bool enabled)

### QFileDialog.setResolveSymlinks (self, bool enabled)

### QFileDialog.setSidebarUrls (self, QUrl-list urls)

Sets the urls that are located in the sidebar

This function was introduced in Qt 4.3.

See also sidebarUrls().

### QFileDialog.setViewMode (self, ViewMode mode)

### QUrl-list QFileDialog.sidebarUrls (self)

Returns a list of urls that are currently in the sidebar

This function was introduced in Qt 4.3.

See also setSidebarUrls().

### ViewMode QFileDialog.viewMode (self)

## Qt Signal Documentation

### void currentChanged (const QString&)

When the current file changes, this signal is emitted with the new file name as the path parameter.

See also filesSelected().

### void directoryEntered (const QString&)

This signal is emitted when the user enters a directory.

This function was introduced in Qt 4.3.

### void filesSelected (const QStringList&)

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

See also currentChanged() and QDialog.Accepted.

### void filterSelected (const QString&)

This signal is emitted when the user selects a filter.

This function was introduced in Qt 4.3.