Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QFileDialog Class Reference

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

    #include <QFileDialog>

Inherits QDialog.

- List of all members, including inherited members
- Compatibility members

### Public Types

- enum AcceptMode { Open, Save }
- enum FileMode { AnyFile, ExistingFile, Directory, DirectoryOnly, ExistingFiles }
- typedef Mode
- enum Option { DontResolveSymlinks, ShowDirsOnly }
- typedef Options
- enum ViewMode { Detail, List }

### Writable Properties

- acceptMode : AcceptMode
- fileMode : FileMode
- viewMode : ViewMode

- 2 writable properties inherited from QDialog
- 33 writable properties inherited from QWidget
- 1 writable property inherited from QObject

### Read-Only Properties

- 24 read-only properties inherited from QWidget

### Public Functions

- QFileDialog ( QWidget * parent, Qt::WFlags flags )
- QFileDialog ( QWidget * parent = 0, const QString & caption = QString::null, const QString & dir = QString::null, const QString & filter = QString::null, const QString & selectedFilter = QString::null, const QString & selectedFile = QString::null, FileMode fileMode = AnyFile )
- ~QFileDialog ()
- QDir directory () const
- QStringList filters () const
- void selectFile ( const QString & filename )
- void selectFilter ( const QString & filter )
- QStringList selectedFiles () const
- QString selectedFilter () const
- void setDirectory ( const QString & directory )
- void setDirectory ( const QDir & directory )
- void setFilter ( const QString & filter )
- void setFilters ( const QStringList & filters )

- 6 public functions inherited from QDialog
- 83 public functions inherited from QWidget
- 24 public functions inherited from QObject
- 2 public functions inherited from QPaintDevice

### Public Slots

- 1 public slot inherited from QDialog
- 16 public slots inherited from QWidget
- 1 public slot inherited from QObject

### Signals

- void filesSelected ( const QStringList & selected )

- 1 signal inherited from QObject

### Static Public Members

- QString getExistingDirectory ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly )
- QString getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
- QStringList getOpenFileNames ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )
- QString getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 )

- 4 static public members inherited from QWidget
- 4 static public members inherited from QObject

### Protected Functions

- 1 protected function inherited from QDialog
- 44 protected functions inherited from QWidget
- 9 protected functions inherited from QObject

### Protected Slots

- void renameCurrent ()

- 4 protected slots inherited from QDialog

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

        QString s = QFileDialog::getOpenFileName(
                        this,
                        "Choose a file",
                        "/home",
                        "Images (*.png *.xpm *.jpg)");

In the above example, a modal QFileDialog is created using a static function. The dialog initially displays the contents of the "/home" directory, and displays files matching the patterns given in the string "Images (*.png *.xpm *.jpg)". The parent of the file dialog is set to this, and the dialog is named "open file dialog". The caption at the top of file dialog is set to "Choose a file" instead of the default.

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 setMode(), you can specify what the user must select in the dialog:

          QFileDialog *fd = new QFileDialog(this);
          fd->setMode(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.

You can retrieve the dialog's mode with mode(). Use setFilter() to set the dialog's file filter. For example:

          fd->setFilter( "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 setFilters(), and add additional filters with addFilter(). Use setSelectedFilter() to select one of the filters you've given as the file dialog's default filter. Whenever the user changes the filter the filterSelected() signal is emitted.

The file dialog has two view modes: QFileDialog::List and QFileDialog::Detail. QFileDialog::List presents the contents of the current directory as a list of file and directory names. QFileDialog::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().

          fd->setViewMode(QFileDialog::Detail);

The last important function you will need to use when creating your own file dialog is selectedFile().

          QString fileName;
          if (fd->exec() == QDialog::Accepted)
              fileName = fd->selectedFile();

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

If you are using the `ExistingFiles` mode then you will need to use selectedFiles() which returns the selected files in a QStringList.

The dialog's working directory can be set with setDirectory(). The display of hidden files is controlled with showHidden(). The dialog can be forced to refresh the directory display with reload() and sort the directory with sortByDate(), sortByName(), and sortBySize(). Each file in the current directory can be selected using the selectFile() function.

### Creating and using preview widgets

A preview widget is a widget that is placed inside a QFileDialog so that the user can see either the contents of the file, or information about the file.

There are two kinds of preview widgets that can be used with QFileDialogs: content preview widgets and information preview widgets. They are created and used with the setContentsPreview() and setInfoPreview() functions.

          class Preview : public QLabel, public QFilePreview
          {
          public:
              Preview(QWidget *parent=0) : QLabel(parent) {}

              void previewUrl(const QUrl &u) {
                  QString path = u.path();
                  QPixmap pix(path);
                  if (pix.isNull())
                      setText("This is not a pixmap");
                  else
                      setPixmap(pix);
              }
          };

In the above snippet, we create a preview widget which inherits from QLabel and QFilePreview. File preview widgets must inherit from QFilePreview.

Inside the class we reimplement QFilePreview::previewUrl(); this is where we determine what happens when a file is selected. In the above example we only show a preview of the file if it is a valid pixmap. Here's how to make a file dialog use a preview widget:

          Preview* preview = new Preview;

          QFileDialog* dialog = new QFileDialog(this);
          dialog->setContentsPreviewEnabled(true);
          dialog->setContentsPreview(preview, preview);
          dialog->setPreviewMode(QFileDialog::Contents);
          dialog->show();

The first line creates an instance of our preview widget. We create our file dialog, and call setContentsPreviewEnabled(true) to preview the contents of the currently selected file. We then call setContentsPreview() -- note that we pass the same preview widget twice. Finally, before showing the file dialog, we call setPreviewMode(), setting the mode to Contents. As a result, the file dialog will show the contents preview of the file that the user has selected.

If you create another preview widget that is used for displaying information about a file, create it in the same way as the contents preview widget then call setInfoPreviewEnabled() and setInfoPreview(). The user will be able to switch between the two preview modes.

For more information about creating a QFilePreview widget see QFilePreview.

<img src=qfiledlg-m.png> <img src=qfiledlg-w.png>

## Member Type Documentation

### enum QFileDialog::AcceptMode

`QFileDialog::Open` |
`QFileDialog::Save` |

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

`QFileDialog::AnyFile` | The name of a file, whether it exists or not.
`QFileDialog::ExistingFile` | The name of a single existing file.
`QFileDialog::Directory` | The name of a directory. Both files and directories are displayed.
`QFileDialog::DirectoryOnly` | The name of a directory. The file dialog will only display directories.
`QFileDialog::ExistingFiles` | The names of zero or more existing files.

See also setFileMode().

### typedef QFileDialog::Mode

### enum QFileDialog::Option
typedef QFileDialog::Options

`QFileDialog::DontResolveSymlinks` |
`QFileDialog::ShowDirsOnly` |

The Options typedef can store a 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` | Displays an icon, a name, and details for each item in the directory.
`QFileDialog::List` | 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.

Access functions:

- AcceptMode acceptMode () const
- void setAcceptMode ( AcceptMode mode )

See also AcceptMode, acceptMode(), and setAcceptMode().

### 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,

Access functions:

- FileMode fileMode () const
- void setFileMode ( FileMode mode )

See also FileMode, fileMode(), and setFileMode().

### 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:

- ViewMode viewMode () const
- void setViewMode ( ViewMode mode )

See also ViewMode, viewMode(), and setViewMode().

## Member Function Documentation

### QFileDialog::QFileDialog ( QWidget * parent, Qt::WFlags flags )

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

### QFileDialog::QFileDialog ( QWidget * parent = 0, const QString & caption = QString::null, const QString & dir = QString::null, const QString & filter = QString::null, const QString & selectedFilter = QString::null, const QString & selectedFile = QString::null, FileMode fileMode = AnyFile )

Constructs a file dialog with the given parent. The dialog has the given caption, starts in directory dir, has the filters specified by filter, with the filter in selectedFilter selected, and with the selectedFile selected. The files that can be chosen are determined by the fileMode.

### QFileDialog::~QFileDialog ()

### QDir QFileDialog::directory () const

Returns the directory currently being displayed in the dialog.

### void QFileDialog::filesSelected ( const QStringList & selected ) `[signal]`

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

### QStringList QFileDialog::filters () const

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

### QString QFileDialog::getExistingDirectory ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly ) `[static]`

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

        QString s = QFileDialog::getExistingDirectory(
                        this,
                        "Choose a directory",
                        "/home",
                        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 can be used to tell the dialog not to resolve symbolic links (`DontResolveSymlinks`), and not to show files (`ShowDirsOnly`).

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 = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 ) `[static]`

### QStringList QFileDialog::getOpenFileNames ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 ) `[static]`

### QString QFileDialog::getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0 ) `[static]`

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 s = QFileDialog::getSaveFileName(
                        this,
                        "Choose a filename to save under",
                        "/home",
                        "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 can be used to tell the dialog not to resolve symbolic links (`DontResolveSymlinks`), and not to show files (`ShowDirsOnly`).

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, unless the style of the application is set to something other than the native style.

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

### void QFileDialog::renameCurrent () `[protected slot]`

Tells the dialog to rename the currently selected item using input from the user.

### void QFileDialog::selectFile ( const QString & filename )

Selects the given filename in both the file dialog.

### void QFileDialog::selectFilter ( const QString & filter )

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

See also setFilter() and setFilters().

### 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, selectedFiles is an empty list.

It is more convenient to use selectedFile() if the mode is `ExistingFile`, `Directory` or `DirectoryOnly`.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

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

See also selectedFile, selectedFilter, and QValueList::empty().

### QString QFileDialog::selectedFilter () const

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

See also filterSelected(), selectedFiles, and selectedFile.

### void QFileDialog::setDirectory ( const QString & directory )

Sets the file dialog's current directory.

### void QFileDialog::setDirectory ( const QDir & directory )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

### void QFileDialog::setFilter ( const 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 or by semicolons then only the text contained in the parentheses is used as the filter. This means that these calls are all equivalent:

        fd->setFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)");
        fd->setFilter("*.cpp *.cc *.C *.cxx *.c++");
        fd->setFilter("All C++ files (*.cpp;*.cc;*.C;*.cxx;*.c++)");
        fd->setFilter("*.cpp;*.cc;*.C;*.cxx;*.c++");

See also setFilters().

### void QFileDialog::setFilters ( const QStringList & filters )

Sets the filters used in the file dialog.

        QStringList types;
        types << "Image files (*.png *.xpm *.jpg)"
                  << "Text files (*.txt)"
                  << "Any files (*)";
        QFileDialog fd = new QFileDialog( this );
        fd->setFilters( types );
        fd->show();