QFileDialog Class
The QFileDialog class lets the user choose files or a directory. More…
| Header | #include <QFileDialog> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QDialog |
List of all members, including inherited members
Public Types
| enum | ViewMode |
| enum | FileMode |
| enum | AcceptMode |
| enum | DialogLabel |
| enum | Option |
| typedef | Options |
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() | |
| void | setDirectory(const QString &directory) |
| void | setDirectory(const QDir &directory) |
| QDir | directory() const |
| void | selectFile(const QString &filename) |
| QStringList | selectedFiles() const |
| void | setNameFilter(const QString &filter) |
| void | setNameFilters(const QStringList &filters) |
| QStringList | nameFilters() const |
| void | selectNameFilter(const QString &filter) |
| QString | selectedNameFilter() const |
| void | setViewMode(ViewMode mode) |
| ViewMode | viewMode() const |
| void | setFileMode(FileMode mode) |
| FileMode | fileMode() const |
| void | setAcceptMode(AcceptMode mode) |
| AcceptMode | acceptMode() const |
| void | setDefaultSuffix(const QString &suffix) |
| QString | defaultSuffix() const |
| void | setLabelText(DialogLabel label, const QString &text) |
| QString | labelText(DialogLabel label) const |
| void | setOption(Option option, bool on = true) |
| bool | testOption(Option option) const |
| void | setOptions(Options options) |
| Options | options() const |
Public Slots
| void | accept() |
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 | getOpenFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options()) |
| QStringList | getOpenFileNames(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options()) |
| QString | getSaveFileName(QWidget *parent = nullptr, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = nullptr, Options options = Options()) |
| QString | getExistingDirectory(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.
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.
| Constant | Description |
|---|---|
QFileDialog::AcceptOpen | The accept button is Open. |
QFileDialog::AcceptSave | The accept button is Save, and replacing a file is confirmed. |
enum QFileDialog::DialogLabel
The labels that setLabelText() changes.
| Constant | Description |
|---|---|
QFileDialog::LookIn | The label of the directory. |
QFileDialog::FileName | The label of the file name. |
QFileDialog::FileType | The label of the name filters. |
QFileDialog::Accept | The accept button. |
QFileDialog::Reject | The reject button. |
enum QFileDialog::FileMode
What the user may choose.
| Constant | Description |
|---|---|
QFileDialog::AnyFile | Any file name, whether the file exists or not, as for saving. |
QFileDialog::ExistingFile | One existing file. |
QFileDialog::Directory | A directory. |
QFileDialog::ExistingFiles | One or more existing files. |
enum QFileDialog::Option
Options of the dialog. Options is a typedef for QFlags<Option>.
| Constant | Description |
|---|---|
QFileDialog::ShowDirsOnly | Show only directories, in Directory mode. |
QFileDialog::DontResolveSymlinks | Accepted for compatibility; links are shown as what they point to. |
QFileDialog::DontConfirmOverwrite | Do not ask before replacing a file in AcceptSave mode. |
QFileDialog::DontUseNativeDialog | Accepted for compatibility; the dialog is never native. |
QFileDialog::ReadOnly | Accepted for compatibility; the dialog never changes the file system. |
QFileDialog::HideNameFilterDetails | Show the names of the filters without their patterns. |
QFileDialog::DontUseCustomDirectoryIcons | Accepted 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.
| Constant | Description |
|---|---|
QFileDialog::Detail | Names with their size, type and date. |
QFileDialog::List | Names 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.
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 ";;".
QFileDialog::~QFileDialog()
Destroys the file dialog.
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().
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().
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().
void QFileDialog::fileSelected(const QString &file)
This signal is emitted when the dialog is accepted with one file, in all the modes but ExistingFiles.
void QFileDialog::filesSelected(const QStringList &selected)
This signal is emitted when the dialog is accepted, with the selected files.
void QFileDialog::filterSelected(const QString &filter)
This signal is emitted when the user chooses the name filter.
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.
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.
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.
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.