6.0.0-rc4Source

QFileDialog Class

The QFileDialog class lets the user choose files or a directory. More…

Header#include <QFileDialog>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQDialog

List of all members, including inherited members

Public Types

enumViewMode
enumFileMode
enumAcceptMode
enumDialogLabel
enumOption
typedefOptions

Public Functions

QFileDialog(QWidget *parent, Qt::WindowFlags flags)
QFileDialog(QWidget *parent = nullptr, const QString &caption = QString(), const QString &directory = QString(), const QString &filter = QString())
~QFileDialog()
voidsetDirectory(const QString &directory)
voidsetDirectory(const QDir &directory)
QDirdirectory() const
voidselectFile(const QString &filename)
QStringListselectedFiles() const
voidsetNameFilter(const QString &filter)
voidsetNameFilters(const QStringList &filters)
QStringListnameFilters() const
voidselectNameFilter(const QString &filter)
QStringselectedNameFilter() const
voidsetViewMode(ViewMode mode)
ViewModeviewMode() const
voidsetFileMode(FileMode mode)
FileModefileMode() const
voidsetAcceptMode(AcceptMode mode)
AcceptModeacceptMode() const
voidsetDefaultSuffix(const QString &suffix)
QStringdefaultSuffix() const
voidsetLabelText(DialogLabel label, const QString &text)
QStringlabelText(DialogLabel label) const
voidsetOption(Option option, bool on = true)
booltestOption(Option option) const
voidsetOptions(Options options)
Optionsoptions() const

Public Slots

voidaccept()

Signals

voidcurrentChanged(const QString &path)
voiddirectoryEntered(const QString &directory)
voidfileSelected(const QString &file)
voidfilesSelected(const QStringList &selected)
voidfilterSelected(const QString &filter)

Static Public Members

QStringgetOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options())
QStringListgetOpenFileNames(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options())
QStringgetSaveFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options())
QStringgetExistingDirectory(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), Options options = ShowDirsOnly)

Detailed Description

The static functions show a file dialog and wait for the user:

const QString name = QFileDialog::getOpenFileName(window, "Open Image", QDir::homePath(),
                                                  "Images (*.png *.jpg);;All files (*)");
if (!name.isEmpty())
    open(name);

The dialog lists the directories and then the files of a directory that match the chosen name filter. The user enters a directory by double-clicking it, goes up with the Up button, or types a path. The file mode says what may be chosen: any file name (for saving), an existing file, several existing files, or a directory. In AcceptSave mode the dialog asks before it replaces an existing file, and adds defaultSuffix() to a name without one.

See also QDir and QFileInfo.

Differences from Qt

The dialog is always drawn by Nt, as a plain list of names: there is no detail view, no icons, no sidebar of places, no history and no file system watching (the list is read when a directory is entered). There are no URL variants (getOpenFileUrl() and the others), no setFilter(), setProxyModel(), setIconProvider() or saveState(), and no open() with a receiver. The texts are not translated.

Member Type Documentation

enum QFileDialog::AcceptMode

Whether the dialog opens or saves.

ConstantDescription
QFileDialog::AcceptOpenThe accept button is Open.
QFileDialog::AcceptSaveThe accept button is Save, and replacing a file is confirmed.

enum QFileDialog::DialogLabel

The labels that setLabelText() changes.

ConstantDescription
QFileDialog::LookInThe label of the directory.
QFileDialog::FileNameThe label of the file name.
QFileDialog::FileTypeThe label of the name filters.
QFileDialog::AcceptThe accept button.
QFileDialog::RejectThe reject button.

enum QFileDialog::FileMode

What the user may choose.

ConstantDescription
QFileDialog::AnyFileAny file name, whether the file exists or not, as for saving.
QFileDialog::ExistingFileOne existing file.
QFileDialog::DirectoryA directory.
QFileDialog::ExistingFilesOne or more existing files.

enum QFileDialog::Option

Options of the dialog. Options is a typedef for QFlags<Option>.

ConstantDescription
QFileDialog::ShowDirsOnlyShow only directories, in Directory mode.
QFileDialog::DontResolveSymlinksAccepted for compatibility; links are shown as what they point to.
QFileDialog::DontConfirmOverwriteDo not ask before replacing a file in AcceptSave mode.
QFileDialog::DontUseNativeDialogAccepted for compatibility; the dialog is never native.
QFileDialog::ReadOnlyAccepted for compatibility; the dialog never changes the file system.
QFileDialog::HideNameFilterDetailsShow the names of the filters without their patterns.
QFileDialog::DontUseCustomDirectoryIconsAccepted for compatibility; there are no icons.

typedef QFileDialog::Options

The Options type is a typedef for QFlags<Option>. It stores an OR combination of Option values.

enum QFileDialog::ViewMode

How the files are shown; recorded only, the dialog shows a list.

ConstantDescription
QFileDialog::DetailNames with their size, type and date.
QFileDialog::ListNames only.

Member Function Documentation

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

Constructs a file dialog with the given parent and window flags, showing the current directory.

[explicit] QFileDialog::QFileDialog(QWidget *parent = nullptr, const QString &caption = QString(), const QString &directory = QString(), const QString &filter = QString())

Constructs a file dialog with the given parent and window caption, showing directory, with the name filters of filter separated by ";;".

[override] QFileDialog::~QFileDialog()

Destroys the file dialog.

[slot, override] void QFileDialog::accept()

Reimplements QDialog::accept().

Checks the file name field for the file mode: a directory that the user typed is entered instead, missing files are refused, and replacing a file is confirmed in AcceptSave mode. When the names are good the dialog emits filesSelected() and fileSelected() and closes.

AcceptMode QFileDialog::acceptMode() const

Returns whether the dialog opens or saves, AcceptOpen by default.

See also setAcceptMode().

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

This signal is emitted when the user moves to the file or directory at path.

QString QFileDialog::defaultSuffix() const

Returns the suffix that is added to a file name without one.

See also setDefaultSuffix().

QDir QFileDialog::directory() const

Returns the directory that the dialog shows.

See also setDirectory().

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

This signal is emitted when the dialog shows the directory.

FileMode QFileDialog::fileMode() const

Returns what the user may choose, AnyFile by default.

See also setFileMode().

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

This signal is emitted when the dialog is accepted with one file, in all the modes but ExistingFiles.

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

This signal is emitted when the dialog is accepted, with the selected files.

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

This signal is emitted when the user chooses the name filter.

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

Shows a file dialog for choosing a directory, with the given parent, window caption, starting directory dir and options, and waits. Returns the chosen directory, or an empty string if the user canceled.

[static] QString QFileDialog::getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options())

Shows a file dialog for opening one existing file, with the given parent, window caption, starting directory or file dir, name filters filter and options, and waits. Returns the chosen file, or an empty string if the user canceled. selectedFilter, if given, chooses the starting filter and is set to the chosen one.

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

Shows a file dialog for opening one or more existing files, with the given parent, window caption, starting directory dir, name filters filter and options, and waits. Returns the chosen files, or an empty list if the user canceled. selectedFilter, if given, chooses the starting filter and is set to the chosen one.

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

Shows a file dialog for saving a file, with the given parent, window caption, starting directory or file dir, name filters filter and options, and waits. Returns the chosen file name, or an empty string if the user canceled. selectedFilter, if given, chooses the starting filter and is set to the chosen one.

QString QFileDialog::labelText(DialogLabel label) const

Returns the text of label.

See also setLabelText().

QStringList QFileDialog::nameFilters() const

Returns the name filters.

See also setNameFilters().

Options QFileDialog::options() const

Returns the options of the dialog.

QStringList QFileDialog::selectedFiles() const

Returns the files that were chosen when the dialog was accepted, as absolute paths. Before that, returns the files that the file name field names.

QString QFileDialog::selectedNameFilter() const

Returns the name filter that is chosen.

See also selectNameFilter().

void QFileDialog::selectFile(const QString &filename)

Puts filename in the file name field and selects it in the list. A path with a directory shows that directory.

See also selectedFiles().

void QFileDialog::selectNameFilter(const QString &filter)

Chooses the name filter filter, if it is one of nameFilters().

See also selectedNameFilter().

void QFileDialog::setAcceptMode(AcceptMode mode)

Sets whether the dialog opens or saves to mode.

See also acceptMode().

void QFileDialog::setDefaultSuffix(const QString &suffix)

Sets the suffix that is added to a file name without one in AcceptSave mode to suffix, without its dot.

See also defaultSuffix().

void QFileDialog::setDirectory(const QString &directory)

Shows directory, which becomes the directory of relative file names. Emits directoryEntered().

See also directory().

void QFileDialog::setDirectory(const QDir &directory)

This is an overloaded function.

Shows directory.

void QFileDialog::setFileMode(FileMode mode)

Sets what the user may choose to mode.

See also fileMode().

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

Sets the text of label to text.

See also labelText().

void QFileDialog::setNameFilter(const QString &filter)

Sets the name filters to the ones in filter, separated by ";;", such as "Images (*.png *.jpg);;Text (*.txt)". An empty filter shows all files.

See also setNameFilters().

void QFileDialog::setNameFilters(const QStringList &filters)

Sets the name filters to filters. The first one is chosen.

See also nameFilters() and selectNameFilter().

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

Sets option if on is true, and clears it otherwise.

See also testOption().

void QFileDialog::setOptions(Options options)

Sets the options of the dialog to options.

See also options().

void QFileDialog::setViewMode(ViewMode mode)

Sets how files are shown to mode; recorded only.

See also viewMode().

bool QFileDialog::testOption(Option option) const

Returns whether option is set.

ViewMode QFileDialog::viewMode() const

Returns how files are shown, Detail by default.