QFileDialog Class Reference

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

 #include <QFileDialog>

Inherits: QDialog.

Public Types

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

Properties

  • 2 properties inherited from QDialog
  • 58 properties inherited from QWidget
  • 1 property inherited from QObject

Public Functions

QFileDialog ( QWidget * parent, Qt::WindowFlags flags )
QFileDialog ( QWidget * parent = 0, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString() )
~QFileDialog ()
AcceptMode acceptMode () const
bool confirmOverwrite () const
QString defaultSuffix () const
QDir directory () const
FileMode fileMode () const
QDir::Filters filter () const
QStringList history () const
QFileIconProvider * iconProvider () const
bool isNameFilterDetailsVisible () const
bool isReadOnly () const
QAbstractItemDelegate * itemDelegate () const
QString labelText ( DialogLabel label ) const
QStringList nameFilters () const
void open ( QObject * receiver, const char * member )
Options options () const
QAbstractProxyModel * proxyModel () const
bool resolveSymlinks () const
bool restoreState ( const QByteArray & state )
QByteArray saveState () const
void selectFile ( const QString & filename )
void selectNameFilter ( const QString & filter )
QStringList selectedFiles () const
QString selectedNameFilter () const
void setAcceptMode ( AcceptMode mode )
void setConfirmOverwrite ( bool enabled )
void setDefaultSuffix ( const QString & suffix )
void setDirectory ( const QString & directory )
void setDirectory ( const QDir & directory )
void setFileMode ( FileMode mode )
void setFilter ( QDir::Filters filters )
void setHistory ( const QStringList & paths )
void setIconProvider ( QFileIconProvider * provider )
void setItemDelegate ( QAbstractItemDelegate * delegate )
void setLabelText ( DialogLabel label, const QString & text )
void setNameFilter ( const QString & filter )
void setNameFilterDetailsVisible ( bool enabled )
void setNameFilters ( const QStringList & filters )
void setOption ( Option option, bool on = true )
void setOptions ( Options options )
void setProxyModel ( QAbstractProxyModel * proxyModel )
void setReadOnly ( bool enabled )
void setResolveSymlinks ( bool enabled )
void setSidebarUrls ( const QList<QUrl> & urls )
void setViewMode ( ViewMode mode )
QList<QUrl> sidebarUrls () const
bool testOption ( Option option ) const
ViewMode viewMode () const

Reimplemented Public Functions

virtual void setVisible ( bool visible )
  • 8 public functions inherited from QDialog
  • 221 public functions inherited from QWidget
  • 29 public functions inherited from QObject
  • 13 public functions inherited from QPaintDevice

Signals

void currentChanged ( const QString & path )
void directoryEntered ( const QString & directory )
void fileSelected ( const QString & file )
void filesSelected ( const QStringList & selected )
void filterSelected ( const QString & filter )

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
  • 7 static public members inherited from QObject

Reimplemented Protected Functions

virtual void accept ()
virtual void changeEvent ( QEvent * e )
virtual void done ( int result )
  • 7 protected functions inherited from QDialog
  • 37 protected functions inherited from QWidget
  • 8 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice

Additional Inherited Members

  • 5 public slots inherited from QDialog
  • 19 public slots inherited from QWidget
  • 1 public slot inherited from QObject
  • 7 protected functions inherited from QDialog
  • 37 protected functions inherited from QWidget
  • 8 protected functions inherited from QObject
  • 1 protected function inherited from QPaintDevice
  • 1 protected slot inherited from QWidget

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, Mac OS X, KDE and GNOME, these static functions will call the native file dialog when possible.

 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.

Member Type Documentation

enum QFileDialog::AcceptMode

ConstantValue
QFileDialog::AcceptOpen0
QFileDialog::AcceptSave1

enum QFileDialog::DialogLabel

ConstantValue
QFileDialog::LookIn0
QFileDialog::FileName1
QFileDialog::FileType2
QFileDialog::Accept3
QFileDialog::Reject4

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.

ConstantValueDescription
QFileDialog::AnyFile0The name of a file, whether it exists or not.
QFileDialog::ExistingFile1The name of a single existing file.
QFileDialog::Directory2The name of a directory. Both files and directories are displayed.
QFileDialog::ExistingFiles3The names of zero or more existing files.

This value is obsolete since Qt 4.5:

ConstantValueDescription
QFileDialog::DirectoryOnly4Use Directory and setOption(ShowDirsOnly, true) instead.

See also setFileMode().

typedef QFileDialog::Mode

Use QFileDialog::FileMode instead.

enum QFileDialog::Option
flags QFileDialog::Options

ConstantValueDescription
QFileDialog::ShowDirsOnly0x00000001Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.)
QFileDialog::DontResolveSymlinks0x00000002Don't resolve symlinks in the file dialog. By default symlinks are resolved.
QFileDialog::DontConfirmOverwrite0x00000004Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
QFileDialog::DontUseNativeDialog0x00000010Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT macro.
QFileDialog::ReadOnly0x00000020Indicates that the model is readonly.
QFileDialog::HideNameFilterDetails0x00000040Indicates if the file name filter details are hidden or not.
QFileDialog::DontUseSheet0x00000008In previous versions of Qt, the static functions would create a sheet by default if the static function was given a parent. This is no longer supported and does nothing in Qt 4.5, The static functions will always be an application modal dialog. If you want to use sheets, use QFileDialog::open() instead.

The Options type is a typedef for QFlags<Option>. It stores an OR 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.

ConstantValueDescription
QFileDialog::Detail0Displays an icon, a name, and details for each item in the directory.
QFileDialog::List1Displays 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.

By default, this property is set to AcceptOpen.

Access functions:

AcceptMode acceptMode () const
void setAcceptMode ( AcceptMode mode )

See also AcceptMode.

defaultSuffix : QString

This property holds suffix added to the filename if no other suffix was specified.

This property specifies a string that will be added to the filename if it has no suffix already. The suffix is typically used to indicate the file type (e.g. "txt" indicates a text file).

Access functions:

QString defaultSuffix () const
void setDefaultSuffix ( const QString & suffix )

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, this property is set to AnyFile.

This function will set the labels for the FileName and Accept DialogLabels. It is possible to set custom text after the call to setFileMode().

Access functions:

FileMode fileMode () const
void setFileMode ( FileMode mode )

See also FileMode.

options : Options

This property holds the various options that affect the look and feel of the dialog.

By default, all options are disabled.

Options should be set before showing the dialog. Setting them while the dialog is visible is not guaranteed to have an immediate effect on the dialog (depending on the option and on the platform).

This property was introduced in Qt 4.5.

Access functions:

Options options () const
void setOptions ( Options options )

See also setOption() and testOption().

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.

Member Function Documentation

QFileDialog::QFileDialog ( QWidget * parent, Qt::WindowFlags flags )

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

QFileDialog::QFileDialog ( QWidget * parent = 0, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString() )

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::~QFileDialog ()

Destroys the file dialog.

void QFileDialog::accept () [virtual protected]

Reimplemented from QDialog::accept().

void QFileDialog::changeEvent ( QEvent * e ) [virtual protected]

Reimplemented from QWidget::changeEvent().

void QFileDialog::currentChanged ( const QString & path ) [signal]

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

See also filesSelected().

QDir QFileDialog::directory () const

Returns the directory currently being displayed in the dialog.

See also setDirectory().

void QFileDialog::directoryEntered ( const QString & directory ) [signal]

This signal is emitted when the user enters a directory.

This function was introduced in Qt 4.3.

void QFileDialog::done ( int result ) [virtual protected]

Reimplemented from QDialog::done().

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

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

See also currentChanged() and QDialog::Accepted.

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

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.

QDir::Filters QFileDialog::filter () const

Returns the filter that is used when displaying files.

This function was introduced in Qt 4.4.

See also setFilter().

void QFileDialog::filterSelected ( const QString & filter ) [signal]

This signal is emitted when the user selects a filter.

This function was introduced in Qt 4.3.

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 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 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. To ensure a native file dialog, ShowDirsOnly must be set.

On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog. On Windows CE, if the device has no native file dialog, a QFileDialog will be used.

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

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 below the parent's title bar.

On Symbian^3 the options parameter is only used to define if the native file dialog is used.

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog constructors.

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]

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 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. If you want multiple filters, separate them with ';;', for example:

 "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"

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.

On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog.

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 below the parent's title bar.

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

On Symbian^3 the parameter selectedFilter has no meaning and the options parameter is only used to define if the native file dialog is used.

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog constructors.

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

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]

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 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. If you need multiple filters, separate them with ';;', for instance:

 "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"

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

On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog.

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 below the parent's title bar.

On 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: 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;
 }

On Symbian^3 the parameter selectedFilter has no meaning and the options parameter is only used to define if the native file dialog is used. On Symbian^3, this function can only return a single filename.

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog constructors.

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

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 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. Multiple filters are separated with ';;'. For instance:

 "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"

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, a default caption will be used.

On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog.

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 below the parent's title bar. On Mac OS X, with its native file dialog, the filter argument is ignored.

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

On Symbian^3 the parameters filter and selectedFilter have no meaning. The options parameter is only used to define if the native file dialog is used.

Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog constructors.

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

QStringList QFileDialog::history () const

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

See also setHistory().

QFileIconProvider * QFileDialog::iconProvider () const

Returns the icon provider used by the filedialog.

See also setIconProvider().

QAbstractItemDelegate * QFileDialog::itemDelegate () const

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

See also setItemDelegate().

QString QFileDialog::labelText ( DialogLabel label ) const

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

See also setLabelText().

QStringList QFileDialog::nameFilters () const

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

This function was introduced in Qt 4.4.

See also setNameFilters().

void QFileDialog::open ( QObject * receiver, const char * member )

This is an overloaded function.

This function connects one of its signals to the slot specified by receiver and member. The specific signal depends is filesSelected() if fileMode is ExistingFiles and fileSelected() if fileMode is anything else.

The signal will be disconnected from the slot when the dialog is closed.

This function was introduced in Qt 4.5.

QAbstractProxyModel * QFileDialog::proxyModel () const

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

See also setProxyModel().

bool QFileDialog::restoreState ( const 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 () const

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.

void QFileDialog::selectFile ( const QString & filename )

Selects the given filename in the file dialog.

See also selectedFiles().

void QFileDialog::selectNameFilter ( const 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().

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

See also selectedNameFilter() and selectFile().

QString QFileDialog::selectedNameFilter () const

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

This function was introduced in Qt 4.4.

See also selectedFiles().

void QFileDialog::setDirectory ( const QString & directory )

Sets the file dialog's current directory.

See also directory().

void QFileDialog::setDirectory ( const QDir & directory )

This is an overloaded function.

void QFileDialog::setFilter ( QDir::Filters filters )

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

void QFileDialog::setHistory ( const QStringList & paths )

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

See also history().

void QFileDialog::setIconProvider ( QFileIconProvider * provider )

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

See also iconProvider().

void QFileDialog::setItemDelegate ( 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.

Note that the model used is QFileSystemModel. It has custom item data roles, which is described by the Roles enum. You can use a QFileIconProvider if you only want custom icons.

See also itemDelegate(), setIconProvider(), and QFileSystemModel.

void QFileDialog::setLabelText ( DialogLabel label, const QString & text )

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

See also labelText().

void QFileDialog::setNameFilter ( 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, 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().

void QFileDialog::setNameFilters ( const 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().

void QFileDialog::setOption ( Option option, bool on = true )

Sets the given option to be enabled if on is true; otherwise, clears the given option.

This function was introduced in Qt 4.5.

See also options and testOption().

void QFileDialog::setProxyModel ( QAbstractProxyModel * proxyModel )

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

void QFileDialog::setSidebarUrls ( const QList<QUrl> & urls )

Sets the urls that are located in the sidebar.

For instance:

     QList<QUrl> urls;
     urls << QUrl::fromLocalFile("/home/gvatteka/dev/qt-45")
          << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation));

     QFileDialog dialog;
     dialog.setSidebarUrls(urls);
     dialog.setFileMode(QFileDialog::AnyFile);
     if(dialog.exec()) {
         // ...
     }

The file dialog will then look like this:

This function was introduced in Qt 4.3.

See also sidebarUrls().

void QFileDialog::setVisible ( bool visible ) [virtual]

Reimplemented from QWidget::setVisible().

QList<QUrl> QFileDialog::sidebarUrls () const

Returns a list of urls that are currently in the sidebar

This function was introduced in Qt 4.3.

See also setSidebarUrls().

bool QFileDialog::testOption ( Option option ) const

Returns true if the given option is enabled; otherwise, returns false.

This function was introduced in Qt 4.5.

See also options and setOption().

Notes provided by the Qt Community
Cool Hack
  • 5

Votes: 2

Coverage: Qt library 4.7, 4.8, 5.0

Picture of peppe peppe

Ant Farmer
8 notes

How to use the Qt file dialog instead of the native one

When using QFileDialog static methods, the native file selection dialog is used, unless QFileDialog::DontUseNativeDialog is passed between the options. Of course that’s always a viable way, but what if you have a huge codebase and don’t want to patch each and every single use of QFileDialog (f.i. for debugging purposes)?

The trick is resetting the native file dialog hooks used by Qt. You just need to declare them (they’re pointers to functions) and set them to 0. Qt will then use its non-native file dialog. See the attached snippet (tested under Linux and Win7) for a demo: setting the env variable QT_NO_NATIVE_FILEDIALOGS to a non-empty value will disable native dialogs.

  1. #include <QtGui>
  2.  
  3. // see src/gui/dialogs/qfiledialog.cpp
  4.  
  5. typedef QStringList (*_qt_filedialog_open_filenames_hook)(QWidget * parent, const QString &caption, const QString &dir,
  6.                                                           const QString &filter, QString *selectedFilter, QFileDialog::Options options);
  7. typedef QString (*_qt_filedialog_open_filename_hook)     (QWidget * parent, const QString &caption, const QString &dir,
  8.                                                           const QString &filter, QString *selectedFilter, QFileDialog::Options options);
  9. typedef QString (*_qt_filedialog_save_filename_hook)     (QWidget * parent, const QString &caption, const QString &dir,
  10.                                                           const QString &filter, QString *selectedFilter, QFileDialog::Options options);
  11. typedef QString (*_qt_filedialog_existing_directory_hook)(QWidget *parent, const QString &caption, const QString &dir,
  12.                                                           QFileDialog::Options options);
  13.  
  14. extern Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook;
  15. extern Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook;
  16. extern Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook;
  17. extern Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook;
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.     QApplication app(argc, argv);
  22.    
  23.     if (!qgetenv("QT_NO_NATIVE_FILEDIALOGS").isEmpty()) {
  24.         qt_filedialog_open_filename_hook = 0;
  25.         qt_filedialog_open_filenames_hook = 0;
  26.         qt_filedialog_save_filename_hook = 0;
  27.         qt_filedialog_existing_directory_hook = 0;
  28.     }
  29.    
  30.     QString filename = QFileDialog::getOpenFileName();
  31.     qDebug() << filename;
  32.    
  33.     return 0;
  34. }

[Revisions]

Informative
  • 4

Votes: 3

Coverage: Qt library 4.7, 4.8, 5.0

Picture of loladiro loladiro

Lab Rat
2 notes

Native file dialogs with hidden parent.

If a native file dialog is executed and the parent parameter is an existing, but no visible widget, strange behaviors can occur during the rest of the execution. The most prominent behavior is that every static model dialog other than QFileDialog will return immediately, while some will continue to be drawn on the screen without reacting to user input. This does only happen once the event loop has started.

[Revisions]

Informative
  • 4

Votes: 1

Coverage: Qt library 4.7, Qt 4.8

Picture of Andre Andre

Robot Herder
30 notes

Nokia Certified Qt Developer

On windows, only static functions result in native dialog

On windows, only using the static functions will result in a native dialog being used. Using the non-static API will result in a non-native dialog provided by Qt.

[Revisions]