Qt logo

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

Public Slots

Signals

Static Public Members

Protected Members

Protected Slots


Detailed Description

The QFileDialog provides a dialog widget for inputting file names.

This class implements a dialog which can be used if the user should select a file or a directory.

Example (e.g. to get a filename for saving a file):

    QString fileName = QFileDialog::getSaveFileName( "newfile.txt", "Textfiles (*.txt)", this );
    if ( !fileName.isNull() ) {                 // got a file name
        ...
    }

To let the user specify a filename for e.g. opening a file, you could use following code:

    QString s( QFileDialog::getOpenFileName( QString::null, "Images (*.png *.xpm *.jpg)", this ) );
    if ( s.isEmpty() )
        return;

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

Other convenient static methods are QFileDialog::getExistingDirectory() to let the user choode a directory or QFileDialog::getOpenFileNames() to let the user select multiple files.

Additionally to these convenient static methods you can use one of QFileDialog's constructors, set a mode (see setMode()) and do more things, like adding a preview widget which will preview the current file or information of the current file while the user does the selection (see setPreviewMode(), setInfoPreviewWidget and setContentsPreviewWidget()) or add additional widgets to the filedialog then (see addWidgets(), addToolButton(), addLeftWidget() and addRightWidget()).

To get the selection the user did then, see selectedFile(), selectedFiles(), selectedFilter() and url(). To set these things see setUrl() and setSelection().

For an example about how to use this customization of the QFileDialog, take a look at the qdir example (qt/examples/qdir/qdir.cpp)

See also: QPrintDialog.

Examples: qdir/qdir.cpp movies/main.cpp


Member Type Documentation

QFileDialog::Mode

This enum type is used to set and read QFileDialog's operating mode. The defined values are:

Examples: qdir/qdir.cpp

QFileDialog::ViewMode

This enum type describes the view mode of the filedialog.

Using setViewMode() you can set the view mode of the filedialog, and with viewMode() you can get the current view mode. The value which is set or returned here can also be or'd together combination of these values.


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 QString & dirName, const QString & filter = QString::null, 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 ()

Destructs the file dialog.

void QFileDialog::addFilter ( const QString & filter ) [protected]

Adds filter to the filter list and makes it the current one.

void QFileDialog::addLeftWidget ( QWidget * w ) [protected]

Adds the widget w to the left of the filedialog.

See also: addRightWidget(), addWidgets() and addToolButton().

void QFileDialog::addRightWidget ( QWidget * w ) [protected]

Adds the widget w to the right of the filedialog.

See also: addLeftWidget(), addWidgets() and addToolButton().

void QFileDialog::addToolButton ( QButton * b, bool separator = FALSE ) [protected]

Adds a the button b to the row of tool buttons on the top of the filedialog. The button is appended at the end (right) of this row. If separator is TRUE, a small space is inserted between the last button of the row and the new button b.

See also: addWidgets(), addLeftWidget() and addRightWidget().

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.

Each time calling this method adds a new row of widgets to the bottom of the filedialog.

See also: addToolButton(), addLeftWidget() and addRightWidget().

void QFileDialog::changeMode ( int id ) [protected slot]

For internal use only.

void QFileDialog::detailViewSelectionChanged () [protected slot]

For internal use only.

const QDir * QFileDialog::dir () const

Returns the active directory in the file dialog.

See also: setDir().

void QFileDialog::dirEntered ( const QString & ) [signal]

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

QString 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 QString & ) [signal]

This signal is emitted when the user highlights a file.

void QFileDialog::fileNameEditReturnPressed () [protected slot]

For internal use only.

void QFileDialog::fileSelected ( const QString & ) [signal]

This signal is emitted when the user selects a file.

QString QFileDialog::getExistingDirectory ( const QString & dir = QString::null, 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.

NOTE: In the windows version of Qt this static method uses the native windows file dialog, and not the QFileDialog.

QString QFileDialog::getOpenFileName ( const QString & startWith = QString::null, const QString & filter = QString::null, 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 QString::null, all files are selectable. In the filter string multiple filters can be specified seperated by either two semicolons next to each other or seperated by newlines. To add two filters, one to show all C++ files and one to show all header files, the filter string could look like "C++ Files (*.cpp *.cc *.C *.cxx *.c++);;Header Files (*.h *.hxx *.h++)"

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( QString::null, "*.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.

NOTE: In the windows version of Qt this static method uses the native windows file dialog, and not the QFileDialog.

See also: getSaveFileName().

QStringList QFileDialog::getOpenFileNames ( const QString & filter= QString::null, const QString & dir = QString::null, 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. In the filter string multiple filters can be specified seperated by either two semicolons next to each other or seperated by newlines. To add two filters, one to show all C++ files and one to show all header files, the filter string could look like "C++ Files (*.cpp *.cc *.C *.cxx *.c++);;Header Files (*.h *.hxx *.h++)"

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.

Example:

    QStringList s( QFileDialog::getOpenFileNames() );
    // do something with the files in s.

NOTE: In the windows version of Qt this static method uses the native windows file dialog, and not the QFileDialog.

QString QFileDialog::getSaveFileName ( const QString & startWith = QString::null, const QString & filter = QString::null, 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 QString::null, all files are selectable. In the filter string multiple filters can be specified seperated by either two semicolons next to each other or seperated by newlines. To add two filters, one to show all C++ files and one to show all header files, the filter string could look like "C++ Files (*.cpp *.cc *.C *.cxx *.c++);;Header Files (*.h *.hxx *.h++)"

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( QString::null, "*.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.

NOTE: In the windows version of Qt this static method uses the native windows file dialog, and not the QFileDialog.

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.

void QFileDialog::listBoxSelectionChanged () [protected slot]

For internal use only.

QFileDialog::Mode QFileDialog::mode() const

Returns the file mode of this dialog.

See also: setMode().

void QFileDialog::removeProgressDia () [protected slot]

For internal use only.

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.

void QFileDialog::resortDir ()

Resorts the displayed directory.

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(), QFileDialog::selectedFiles() and QFileDialog::selectedFilter().

Examples: qdir/qdir.cpp

QStringList QFileDialog::selectedFiles () const

Returns a list of selected files. This is only useful, if the mode of the filedialog is ExistingFiles. Else the list will only contain one entry, which is the the selecedFile. If no files were selected, this list is empty.

See also: QFileDialog::selecedFile() and QStringList::isEmpty().

QString QFileDialog::selectedFilter () const

Returns the filter which the user has chosen in the file dialog.

See also: QString::isNull() and QFileDialog::selectedFiles().

void QFileDialog::setContentsPreviewWidget ( QWidget * w )

Sets the widget which should be used for displaying the preview of a file.

This widget should implement a public slot

void showPreview( const QUrl & );

A signal of the filedialog will then be automatically connected to this slot. If the user selects a file then, this signal is emitted, so that the preview widget can show a preview of this file (url).

Examples: qdir/qdir.cpp

void QFileDialog::setDir ( const QDir & dir )

Sets a directory path for the file dialog.

See also: dir().

void QFileDialog::setDir ( const QString & pathstr ) [slot]

Sets a directory path string for the file dialog.

See also: dir().

void QFileDialog::setFilter ( const QString & 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 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++" )

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 QString & ) [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::setFilters ( const QStringList & ) [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().

Examples: showimg/main.cpp

void QFileDialog::setInfoPreviewWidget ( QWidget * w )

Sets the widget which should be used for displaying information of a file.

This widget should implement a public slot

void showPreview( const QUrl & );

A signal of the filedialog will then be automatically connected to this slot. If the user selects a file then, this signal is emitted, so that the preview widget can show information of this file (url).

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), AnyFile (any valid file name is accepted) or ExistingFiles (like ExistingFile, but multiple files may be selected)

See also: mode().

Examples: qdir/qdir.cpp

void QFileDialog::setPreviewMode ( bool info, bool contents )

Sets the file preview modes. If info is TRUE, a widget for showing file information can be shown, else not. If contents is TRUE, a widget for showing a file preview can be shown, else not.

Examples: qdir/qdir.cpp

void QFileDialog::setSelection ( const QString & filename )

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

Examples: qdir/qdir.cpp

void QFileDialog::setShowHiddenFiles ( bool s )

If s is TRUE, hidden files are shown in the filedialog, else no hidden files are shown.

void QFileDialog::setUrl ( const QUrlOperator & url ) [slot]

Sets the url which should be used as working directory.

void QFileDialog::setViewMode ( int m )

Set the viewmode of the filedialog. You can choose between DetailView, ListView, PreviewContents and PreviewInfo. One of the View-Flags and one of the Preview-Flags can be or'd together, e.g. to set the filedialog to show a detail view and the show contents preview widget, use setViewMode( QFileDialog::DetailView | QFileDialog::PreviewContents );.

Examples: qdir/qdir.cpp

bool QFileDialog::showHiddenFiles () const

Returns TRUE if hidden files are shown in the filedialog, else FALSE.

void QFileDialog::showPreview ( const QUrl & u ) [signal]

This signal is emitted when a preview of the URL u should be shown in the preview widget. Normally you don't need to connect to this signal, as this is done automatically.

void QFileDialog::stopCopy () [protected slot]

Stops the current copy operation.

QUrlOperator QFileDialog::url () const

Returns the URL of the current working directory.

int QFileDialog::viewMode () const

Returns the viewmode of the filedialog. This is a value of either DetailView or ListView maybe or'd together with either PreviewContents or PreviewInfo.


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


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


Copyright İ 2000 Troll TechTrademarks
Qt version 2.1.0 (pre-release)