The QFileDialog class provides dialogs that allow users to select files or directories.
.PP
The QFileDialog class enables a user to traverse their file system in order to select one or many files or a directory.
.PP
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.
In the above example, a modal QFileDialog is created using a static function. The startup directory is set to "/home". The file filter is set to "Images (*.png *.xpm *.jpg)". The parent of the file dialog is set to \fIthis\fR and it is given the identification name - "open file dialog". The caption at the top of file dialog is set to "Choose a file". If you want to use multiple filters, separate each one with \fItwo\fR semi-colons, e.g.
You can create your own QFileDialog without using the static functions. By calling setMode(), you can set what can be returned by the QFileDialog.
.PP
.nf
.br
QFileDialog* fd = new QFileDialog( this, "file dialog", TRUE );
.br
fd->setMode( QFileDialog::AnyFile );
.br
.fi
.PP
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 "File 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::Mode enum for the complete list of modes.)
.PP
You can retrieve the dialog's mode with mode(). Use setFilter() to set the dialog's file filter, e.g.
.PP
.nf
.br
fd->setFilter( "Images (*.png *.xpm *.jpg)" );
.br
.fi
.PP
In the above example, the filter is set to "Images (*.png *.xpm *.jpg)", this means that only files with the extension \fCpng\fR, \fCxpm\fR or \fCjpg\fR 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.
.PP
The file dialog has two view modes, QFileDialog::List which simply lists file and directory names and QFileDialog::Detail which displays additional information alongside each name, e.g. file size, modification date, etc. Set the mode with setViewMode().
.PP
.nf
.br
fd->setViewMode( QFileDialog::Detail );
.br
.fi
.PP
The last important function you will need to use when creating your own file dialog is selectedFile().
In the above example, a modal file dialog is created and shown. If the user clicked OK, then the file they selected is put in \fCfileName\fR.
.PP
If you are using the ExistingFiles mode then you will need to use selectedFiles() which will return the selected files in a QStringList.
.PP
The dialog's working directory can be set with setDir(). The display of hidden files is controlled with setShowHiddenFiles(). The dialog can be forced to re-read the directory with rereadDir() and re-sort the directory with resortDir(). All the files in the current directory can be selected with selectAll().
.SH "Creating and using preview widgets"
There are two kinds of preview widgets that can be used with QFileDialogs: \fIcontent\fR preview widgets and \fIinformation\fR preview widgets. They are created and used in the same way except that the function names differ, e.g. setContentsPreview() and setInfoPreview().
.PP
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.
.PP
.nf
.br
class Preview : public QLabel, public QFilePreview
In the above snippet, we create a preview widget which inherits from QLabel and QFilePreview. File preview widgets \fImust\fR inherit from QFilePreview.
.PP
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:
.PP
.nf
.br
Preview* p = new Preview;
.br
.br
QFileDialog* fd = new QFileDialog( this );
.br
fd->setContentsPreviewEnabled( TRUE );
.br
fd->setContentsPreview( p, p );
.br
fd->setPreviewMode( QFileDialog::Contents );
.br
fd->show();
.br
.fi
.PP
The first line creates an instance of our preview widget. We then create our file dialog and call setContentsPreviewEnabled( TRUE ), this tell the file dialog 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 \fIContents\fR which will show the contents preview of the file that the user has selected.
.PP
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 and call setInfoPreviewEnabled(), and setInfoPreview(). Then the user will be able to switch between the two preview modes.
.PP
For more information about creating a QFilePreview widget see QFilePreview.
.PP
.ce 1
.B "[Image Omitted]"
.PP
.ce 1
.B "[Image Omitted]"
.PP
See also Dialog Classes.
.SS "Member Type Documentation"
.SH "QFileDialog::Mode"
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.
.TP
\fCQFileDialog::AnyFile\fR - The name of a file, whether it exists or not.
.TP
\fCQFileDialog::ExistingFile\fR - The name of a single existing file.
.TP
\fCQFileDialog::Directory\fR - The name of a directory. Both files and directories are displayed.
.TP
\fCQFileDialog::DirectoryOnly\fR - The name of a directory. The file dialog will only display directories.
.TP
\fCQFileDialog::ExistingFiles\fR - The names of zero or more existing files.
.PP
See setMode().
.SH "QFileDialog::PreviewMode"
This enum describes the preview mode of the file dialog.
.TP
\fCQFileDialog::NoPreview\fR - No preview is shown at all.
.TP
\fCQFileDialog::Contents\fR - Show a preview of the contents of the current file using the contents preview widget.
.TP
\fCQFileDialog::Info\fR - Show information about the current file using the info preview widget.
.PP
See setPreviewMode(), setContentsPreview() and setInfoPreview().
.SH "QFileDialog::ViewMode"
This enum describes the view mode of the file dialog, i.e. what information about each file will be displayed.
.TP
\fCQFileDialog::List\fR - Display file and directory names with icons.
.TP
\fCQFileDialog::Detail\fR - Display file and directory names with icons plus additional information, such as file size and modification date.
Constructs a file dialog called \fIname\fR with the parent, \fIparent\fR. If \fImodal\fR is TRUE then the file dialog is modal; otherwise it is modeless.
.PP
If \fIdirName\fR is specified then it will be used as the dialog's working directory, i.e. it will be the directory that is shown when the dialog appears. If \fIfilter\fR is specified it will be used as the dialog's file filter.
Constructs a file dialog called \fIname\fR, with the parent, \fIparent\fR. If \fImodal\fR is TRUE then the file dialog is modal; otherwise it is modeless.
Adds the filter \fIfilter\fR to the list of filters and makes it the current filter.
.PP
.nf
.br
QFileDialog* fd = new QFileDialog( this );
.br
fd->addFilter( "Images (*.png *.jpg *.xpm)" );
.br
fd->show();
.br
.fi
.PP
In the above example, a file dialog is created, and the file filter "Images (*.png *.jpg *.xpm)" is added and is set as the current filter. The original filter, "All Files (*)", is still available.
Adds the tool button \fIb\fR to the row of tool buttons at the top of the file dialog. The button is appended to the right of this row. If \fIseparator\fR is TRUE, a small space is inserted between the last button of the row and the new button \fIb\fR.
.PP
See also addWidgets(), addLeftWidget(), and addRightWidget().
Adds the specified widgets to the bottom of the file dialog. The label \fIl\fR is placed underneath the "file name" and the "file types" labels. The widget \fIw\fR is placed underneath the file types combobox. The button \fIb\fR is placed underneath the Cancel pushbutton.
This function creates a modal file dialog called \fIname\fR, with parent, \fIparent\fR. If parent is not 0, the dialog will be shown centered over the parent.
The dialog's working directory is set to \fIdir\fR, and the caption is set to \fIcaption\fR. Either of these may be TQString::null in which case the current directory and a default caption will be used respectively.
If \fIdirOnly\fR is TRUE, then only directories will be shown in the file dialog; otherwise both directories and files will be shown.
.PP
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 \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories.
.PP
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 titlebar).
.PP
See also getOpenFileName(), getOpenFileNames(), and getSaveFileName().
The function creates a modal file dialog called \fIname\fR, with parent, \fIparent\fR. If a parent is not 0, the dialog will be shown centered over the parent.
The file dialog's working directory will be set to \fIstartWith\fR. If \fIstartWith\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIstartWith\fR, \fIselectedFilter\fR and \fIfilter\fR may be TQString::null.
The dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used.
.PP
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 titlebar).
.PP
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 \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories.
.PP
See also getOpenFileNames(), getSaveFileName(), and getExistingDirectory().
.PP
Examples:
.)l action/application.cpp, addressbook/mainwindow.cpp, application/application.cpp, distributor/distributor.ui.h, network/ftpclient/ftpmainwindow.ui.h, qwerty/qwerty.cpp, and showimg/showimg.cpp.
This function creates a modal file dialog called \fIname\fR, with parent \fIparent\fR. If \fIparent\fR is not 0, the dialog will be shown centered over the parent.
The file dialog's working directory will be set to \fIdir\fR. If \fIdir\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIdir\fR, \fIselectedFilter\fR and \fIfilter\fR may be TQString::null.
The dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used.
.PP
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 titlebar).
.PP
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 \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories.
.PP
Note that if you want to iterate over the list of files, you should iterate over a copy, e.g.
.PP
.nf
.br
QStringList list = files;
.br
QStringList::Iterator it = list.begin();
.br
while( it != list.end() ) {
.br
myProcessing( *it );
.br
++it;
.br
}
.br
.fi
.PP
See also getOpenFileName(), getSaveFileName(), and getExistingDirectory().
The file dialog's working directory will be set to \fIstartWith\fR. If \fIstartWith\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIstartWith\fR, \fIselectedFilter\fR and \fIfilter\fR may be TQString::null.
The dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used.
.PP
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 titlebar.
.PP
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 \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories.
.PP
See also getOpenFileName(), getOpenFileNames(), and getExistingDirectory().
.PP
Examples:
.)l action/application.cpp, addressbook/mainwindow.cpp, application/application.cpp, network/ftpclient/ftpmainwindow.ui.h, qmag/qmag.cpp, qwerty/qwerty.cpp, and showimg/showimg.cpp.
Returns TRUE if the file dialog can provide a contents preview of the currently selected file; otherwise returns FALSE. See the "contentsPreview" property for details.
Returns TRUE if the file dialog can provide preview information about the currently selected file; otherwise returns FALSE. See the "infoPreview" property for details.
.SH "Mode QFileDialog::mode () const"
Returns the file dialog's mode. See the "mode" property for details.
Returns the preview mode for the file dialog. See the "previewMode" property for details.
.SH "void QFileDialog::rereadDir ()"
Rereads the current directory shown in the file dialog.
.PP
The only time you will need to call this function is if the contents of the directory change and you wish to refresh the file dialog to reflect the change.
.PP
See also resortDir().
.SH "void QFileDialog::resortDir ()"
Re-sorts the displayed directory.
.PP
See also rereadDir().
.SH "void QFileDialog::selectAll ( bool b )"
If \fIb\fR is TRUE then all the files in the current directory are selected; otherwise, they are deselected.
Sets the widget to be used for displaying the contents of the file to the widget \fIw\fR and a preview of those contents to the QFilePreview \fIpreview\fR.
Normally you would create a preview widget that derives from both TQWidget and QFilePreview, so you should pass the same widget twice. If you don't, you must remember to delete the preview object in order to avoid memory leaks.
Sets the filter used in the file dialog to \fInewFilter\fR.
.PP
If \fInewFilter\fR contains a pair of parentheses containing one or more of \fI\fBanything*something\fR\fR separated by spaces or by semi-colons then only the text contained in the parentheses is used as the filter. This means that these calls are all equivalent:
.PP
.nf
.br
fd->setFilter( "All C++ files (*.cpp *.cc *.C *.cxx *.c++)" );
.br
fd->setFilter( "*.cpp *.cc *.C *.cxx *.c++" );
.br
fd->setFilter( "All C++ files (*.cpp;*.cc;*.C;*.cxx;*.c++)" );
Sets the widget to be used for displaying information about the file to the widget \fIw\fR and a preview of that information to the QFilePreview \fIpreview\fR.
Normally you would create a preview widget that derives from both TQWidget and QFilePreview, so you should pass the same widget twice. If you don't, you must remember to delete the preview object in order to avoid memory leaks.
Sets the default selection to \fIfilename\fR. If \fIfilename\fR is absolute, setDir() is also called to set the file dialog's working directory to the filename's directory.
.PP
Example: qdir/qdir.cpp.
.SH "void QFileDialog::setShowHiddenFiles ( bool s )"
Sets whether hidden files are shown in the file dialog to \fIs\fR. See the "showHiddenFiles" property for details.
This property holds the file dialog's working directory.
.PP
Get this property's value with dirPath().
.PP
See also dir() and setDir().
.SH "bool infoPreview"
This property holds whether the file dialog can provide preview information about the currently selected file.
.PP
The default is FALSE.
.PP
Set this property's value with setInfoPreviewEnabled() and get this property's value with isInfoPreviewEnabled().
.SH "Mode mode"
This property holds the file dialog's mode.
.PP
The default mode is ExistingFile.
.PP
Set this property's value with setMode() and get this property's value with mode().
.SH "PreviewMode previewMode"
This property holds the preview mode for the file dialog.
.PP
If you set the mode to be a mode other than \fINoPreview\fR, you must use setInfoPreview() or setContentsPreview() to set the dialog's preview widget to your preview widget and enable the preview widget(s) with setInfoPreviewEnabled() or setContentsPreviewEnabled().
.PP
See also infoPreview, contentsPreview, and viewMode.
.PP
Set this property's value with setPreviewMode() and get this property's value with previewMode().
If one or more files are selected, selectedFiles contains their names including their absolute paths. If no files are selected or the mode isn't ExistingFiles selectedFiles is an empty list.
.PP
It is more convenient to use selectedFile() if the mode is ExistingFile, Directory or DirectoryOnly.
.PP
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
.PP
.nf
.br
QStringList list = myFileDialog.selectedFiles();
.br
QStringList::Iterator it = list.begin();
.br
while( it != list.end() ) {
.br
myProcessing( *it );
.br
++it;
.br
}
.br
.fi
.PP
See also selectedFile, selectedFilter, and QValueList::empty().
This property holds the filter which the user has selected in the file dialog.
.PP
Get this property's value with selectedFilter().
.PP
See also filterSelected(), selectedFiles, and selectedFile.
.SH "bool showHiddenFiles"
This property holds whether hidden files are shown in the file dialog.
.PP
The default is FALSE, i.e. don't show hidden files.
.PP
Set this property's value with setShowHiddenFiles() and get this property's value with showHiddenFiles().
.SH "ViewMode viewMode"
This property holds the file dialog's view mode.
.PP
If you set the view mode to be \fIDetail\fR (the default), then you will see the file's details, such as the size of the file and the date the file was last modified in addition to the file's name.
.PP
If you set the view mode to be \fIList\fR, then you will just see a list of the files and folders.
.PP
See QFileDialog::ViewMode
.PP
Set this property's value with setViewMode() and get this property's value with viewMode().