Qt Toolkit - QFileDialog Class

# QFileDialog Class Reference

The QFileDialog provides a dialog widget for inputting file names. More...

`#include <qfiledialog.h>`

Inherits QDialog.

List of all member functions.

## Public Members

- QFileDialog ( const char * dirName, const char * filter = 0, QWidget * parent=0, const char * name=0, bool modal=FALSE )
- QFileDialog ( QWidget * parent=0, const char * name=0, bool modal=FALSE )
- ~QFileDialog ()
- QString selectedFile () const
- void setSelection ( const char * )
- const char* dirPath () const
- void setDir ( const QDir & )
- const QDir* dir () const
- void rereadDir ()
- void setMode ( Mode )
- Mode mode () const
- virtual bool eventFilter ( QObject *, QEvent * )

## Public Slots

- void setDir ( const char * )
- void setFilter ( const char * )
- void setFilters ( const char ** )
- void setFilters ( const QStrList & )

## Signals

- void fileHighlighted ( const char * )
- void fileSelected ( const char * )
- void dirEntered ( const char * )

## Static Public Members

- QString getOpenFileName ( const char * initially = 0, const char * filter= 0, QWidget * parent = 0, const char * name = 0 )
- QString getSaveFileName ( const char * initially = 0, const char * filter= 0, QWidget * parent = 0, const char * name = 0 )
- QString getExistingDirectory ( const char * dir = 0, QWidget * parent = 0, const char * name = 0 )
- QStrList getOpenFileNames ( const char * filter= 0, const char * dir = 0, QWidget * parent = 0, const char * name = 0 )
- void setIconProvider ( QFileIconProvider * )
- QFileIconProvider* iconProvider ()

## Protected Members

- virtual void resizeEvent ( QResizeEvent * )
- virtual void keyPressEvent ( QKeyEvent * )
- void addWidgets ( QLabel *, QWidget *, QPushButton * )

## Detailed Description

The QFileDialog provides a dialog widget for inputting file names.

Example:

        QString fileName = QFileDialog::getOpenFileName();
        if ( !fileName.isNull() ) {                 // got a file name
            ...
        }

There are two ready-made convenience functions, getOpenFileName() and getSaveFileName(), which may be used like this:

        QString s( QFileDialog::getOpenFileName() );
        if ( s.isNull() )
            return;

        open( s ); // open() being your function to read the file

See also: QPrintDialog.

Examples: movies/main.cpp

## Member Function Documentation

### QFileDialog::QFileDialog ( QWidget * parent=0, const char * name=0, bool modal=FALSE )

Constructs a file dialog with a parent, name and modal flag.

The dialog becomes modal if modal is TRUE, otherwise modeless.

### QFileDialog::QFileDialog ( const char * dirName, const char * filter = 0, QWidget * parent=0, const char * name=0, bool modal=FALSE )

Constructs a file dialog with a parent, name and modal flag.

The dialog becomes modal if modal is TRUE, otherwise modeless.

### QFileDialog::~QFileDialog ()

Destroys the file dialog.

### void QFileDialog::addWidgets ( QLabel * l, QWidget * w, QPushButton * b ) `[protected]`

Adds 1-3 widgets to the bottom of the file dialog. l is the (optional) label, which is put beneath the "file name" and "file type" labels, w is a (optional) widget, which is put beneath the file type combo box, and b is the (you guessed it - optional) button, which is put beneath the cancel button.

If you don't want to add something in one of the columns, pass 0.

It is not currently possible to add more than one row.

### const QDir * QFileDialog::dir () const

Returns the active directory in the file dialog.

See also: setDir().

### void QFileDialog::dirEntered ( const char * ) `[signal]`

This signal is emitted when the user has selected a new directory.

### const char * QFileDialog::dirPath () const

Returns the active directory path string in the file dialog.

See also: dir() and setDir().

### bool QFileDialog::eventFilter ( QObject * o, QEvent * e ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

### void QFileDialog::fileHighlighted ( const char * ) `[signal]`

This signal is emitted when the user highlights a file.

### void QFileDialog::fileSelected ( const char * ) `[signal]`

This signal is emitted when the user selects a file.

### QString QFileDialog::getExistingDirectory ( const char * dir = 0, QWidget * parent = 0, const char * name = 0 ) `[static]`

Ask the user for the name of an existing directory, starting at dir. Returns the name of the directory the user selected.

If dir is null, getExistingDirectory() starts wherever the previous file dialog left off.

### QString QFileDialog::getOpenFileName ( const char * startWith = 0, const char * filter= 0, QWidget * parent = 0, const char * name = 0 ) `[static]`

Opens a modal file dialog and returns the name of the file to be opened.

If startWith is the name of a directory, the dialog starts off in that directory. If startWith is the name of an existing file, the dialogs starts in that directory, and with startWith selected.

Only files matching filter are selectable. If filter is 0, all files are selectable.

If widget and/or name is provided, the dialog will be centered over widget and named name.

getOpenFileName() returns a null string if the user cancelled the dialog.

This static function is less capable than the full QFileDialog object, but is convenient and easy to use.

Example:

        // start at the current working directory and with *.cpp as filter
        QString f = QFileDialog::getOpenFileName( 0, "*.cpp", this );
        if ( !f.isEmpty() ) {
            // the user selected a valid existing file
        } else {
            // the user cancelled the dialog
        }

getSaveFileName() is another convenience function, equal to this one except that it allows the user to specify the name of a nonexistent file name.

See also: getSaveFileName().

### QStrList QFileDialog::getOpenFileNames ( const char * filter= 0, const char * dir = 0, QWidget * parent = 0, const char * name = 0 ) `[static]`

Lets the user select N files from a single directory, and returns a list of the selected files. The list may be empty, and the file names are fully qualified (i.e. "/usr/games/quake" or "c:\\quake\\quake").

filter is the default glob pattern (which the user can change). The default is all files. dir is the starting directory. If dir is not supplied, QFileDialog picks something presumably useful (such as the directory where the user selected something last, or the current working directory).

parent is a widget over which the dialog should be positioned and name is the object name of the temporary QFileDialog object.

Note that the returned list has auto-delete turned off. It is the application developer's responsibility to delete the strings in the list, for example using code such as:

        QStrList s( QFileDialog::getOpenFileNames() );
        // do something with the files in s.
        s.setAutoDelete();
        s.clear(); // or just go out of scope.

### QString QFileDialog::getSaveFileName ( const char * startWith = 0, const char * filter= 0, QWidget * parent = 0, const char * name = 0 ) `[static]`

Opens a modal file dialog and returns the name of the file to be saved.

If startWith is the name of a directory, the dialog starts off in that directory. If startWith is the name of an existing file, the dialogs starts in that directory, and with startWith selected.

Only files matching filter are selectable. If filter is 0, all files are selectable.

If widget and/or name is provided, the dialog will be centered over widget and named name.

Returns a null string if the user cancelled the dialog.

This static function is less capable than the full QFileDialog object, but is convenient and easy to use.

Example:

        // start at the current working directory and with *.cpp as filter
        QString f = QFileDialog::getSaveFileName( 0, "*.cpp", this );
        if ( !f.isEmpty() ) {
            // the user gave a file name
        } else {
            // the user cancelled the dialog
        }

getOpenFileName() is another convenience function, equal to this one except that it does not allow the user to specify the name of a nonexistent file name.

See also: getOpenFileName().

### QFileIconProvider * QFileDialog::iconProvider () `[static]`

Returns the icon provider currently in use. By default there is no icon provider and this function returns 0.

See also: setIconProvider() and QFileIconProvider.

### void QFileDialog::keyPressEvent ( QKeyEvent * ke ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### QFileDialog::Mode QFileDialog::mode() const

Returns the file mode of this dialog.

See also: setMode().

### void QFileDialog::rereadDir ()

Re-reads the active directory in the file dialog.

It is seldom necessary to call this function. It is provided in case the directory contents change and you want to refresh the directory list box.

### void QFileDialog::resizeEvent ( QResizeEvent * ) `[virtual protected]`

Handles resize events for the file dialog.

Reimplemented from QWidget.

### QString QFileDialog::selectedFile () const

Returns the selected file name.

If a file name was selected, the returned string contains the absolute path name. The returned string is a null string if no file name was selected.

See also: QString::isNull().

### void QFileDialog::setDir ( const QDir & dir )

Sets a directory path for the file dialog.

See also: dir().

### void QFileDialog::setDir ( const char * pathstr ) `[slot]`

Sets a directory path string for the file dialog.

See also: dir().

### void QFileDialog::setFilter ( const char * newFilter ) `[slot]`

Sets the filter spec in use to newFilter.

If newFilter matches the regular expression `([a-zA-Z0-9\.\*\?]*)$` (ie. it ends with a normal wildcard expression enclosed in parentheses), only the parenthesized is used. This means that these two calls are equivalent:

         fd->setFilter( "All perl files (*.pl)" );
         fd->setFilter( "*.pl" )

### void QFileDialog::setFilters ( const char ** types ) `[slot]`

Sets this file dialog to offer types in the File Type combo box. types must be a null-terminated list of strings; each string must be in the format described in the documentation for setFilter().

See also: setFilter().

### void QFileDialog::setFilters ( const QStrList & ) `[slot]`

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

### void QFileDialog::setIconProvider ( QFileIconProvider * provider ) `[static]`

Sets all file dialogs to use provider to select icons to draw for each file. By default there is no icon provider, and QFileDialog simply draws a "folder" icon next to each directory and nothing next to the files.

See also: QFileIconProvider and iconProvider().

### void QFileDialog::setMode ( Mode newMode )

Sets this file dialog to newMode, which can be one of `Directory` (directories are accepted), `ExistingFile` (existing files are accepted) or `AnyFile` (any valid file name is accepted).

See also: mode().

### void QFileDialog::setSelection ( const char * filename )

Sets the default selection to filename. If filename is absolute, setDir() is also called.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qfiledialog.h: 1998/08/21
- qfiledialog.cpp: 1999/02/10