KFileWidget Class

File selector widget. More...

Header: #include <KFileWidget>
CMake: find_package(KF6 REQUIRED COMPONENTS KIO)
target_link_libraries(mytarget PRIVATE KF6::KIOFileWidgets)
Inherits: QWidget

Public Types

enum OperationMode { Other, Opening, Saving }

Public Functions

KFileWidget(const QUrl &startDir, QWidget *parent = nullptr)
QUrl baseUrl() const
QPushButton *cancelButton() const
void clearFilter()
(since 6.0) KFileFilter currentFilter() const
(since 5.0) QSize dialogSizeHint() const
KDirOperator *dirOperator()
KFileFilterCombo *filterWidget() const
bool keepsLocation() const
KUrlComboBox *locationEdit() const
KFile::Modes mode() const
QPushButton *okButton() const
KFileWidget::OperationMode operationMode() const
QString selectedFile() const
QStringList selectedFiles() const
QUrl selectedUrl() const
QList<QUrl> selectedUrls() const
void setConfirmOverwrite(bool enable)
void setCustomWidget(QWidget *widget)
void setCustomWidget(const QString &text, QWidget *widget)
(since 6.0) void setFilters(const QList<KFileFilter> &filters, const KFileFilter &activeFilter = KFileFilter())
void setInlinePreviewShown(bool show)
void setKeepLocation(bool keep)
void setLocationLabel(const QString &text)
void setMode(KFile::Modes m)
void setOperationMode(KFileWidget::OperationMode)
void setPreviewWidget(KPreviewWidgetBase *w)
(since 5.33) void setSelectedUrl(const QUrl &url)
(since 5.75) void setSelectedUrls(const QList<QUrl> &urls)
(since 5.43) void setSupportedSchemes(const QStringList &schemes)
void setUrl(const QUrl &url, bool clearforward = true)
(since 5.0) void setViewMode(KFile::FileView mode)
(since 5.43) QStringList supportedSchemes() const

Public Slots

void accept()
void slotCancel()
void slotOk()

Signals

void accepted()
(since 4.4) void fileHighlighted(const QUrl &)
(since 4.4) void fileSelected(const QUrl &)
(since 6.0) void filterChanged(const KFileFilter &filter)
void selectionChanged()

Static Public Members

QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass)
QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass, QString &fileName)

Detailed Description

This is the contents of the KDE file dialog, without the actual QDialog around it. It can be embedded directly into applications.

Member Type Documentation

enum KFileWidget::OperationMode

Defines some default behavior of the filedialog.

E.g. in mode Opening and Saving, the selected files/urls will be added to the "recent documents" list. The Saving mode also implies setKeepLocation() being set.

Other means that no default actions are performed.

ConstantValue
KFileWidget::Other0
KFileWidget::Opening1
KFileWidget::Saving2

See also setOperationMode and operationMode.

Member Function Documentation

[explicit] KFileWidget::KFileWidget(const QUrl &startDir, QWidget *parent = nullptr)

Constructs a file selector widget.

startDir This can either be:

  • An empty URL (QUrl()) to start in the current working directory, or the last directory where a file has been selected.
  • The path or URL of a starting directory.
  • An initial file name to select, with the starting directory being the current working directory or the last directory where a file has been selected.
  • The path or URL of a file, specifying both the starting directory and an initially selected file name.
  • A URL of the form kfiledialog:///<keyword>; to start in the directory last used by a filedialog in the same application that specified the same keyword.
  • A URL of the form kfiledialog:///<keyword>/<filename>; to start in the directory last used by a filedialog in the same application that specified the same keyword, and to initially select the specified filename.
  • Deprecated: A URL of the form kfiledialog:///<keyword>?global to start in the directory last used by a filedialog in any application that specified the same keyword.
  • Deprecated: A URL of the form kfiledialog:///<keyword>/<filename>?global to start in the directory last used by a filedialog in any application that specified the same keyword, and to initially select the specified filename.

Note: Since 5.96, the "?global" syntax is deprecated, for lack of usage.

parent The parent widget of this widget

[slot] void KFileWidget::accept()

[signal] void KFileWidget::accepted()

Emitted by slotOk() (directly or asynchronously) once everything has been done. Should be used by the caller to call accept().

QUrl KFileWidget::baseUrl() const

Returns the currently shown directory.

QPushButton *KFileWidget::cancelButton() const

Returns a pointer to the Cancel-Button in the filedialog. Note that the button is hidden and unconnected when using KFileWidget alone; KFileDialog shows it and connects to it.

void KFileWidget::clearFilter()

Clears any MIME type or name filter. Does not reload the directory.

[since 6.0] KFileFilter KFileWidget::currentFilter() const

Returns the current filter as entered by the user or one of the predefined set via setFilters().

This function was introduced in 6.0.

See also setFilters() and filterChanged().

[since 5.0] QSize KFileWidget::dialogSizeHint() const

Provides a size hint, useful for dialogs that embed the widget.

Returns a QSize, calculated to be optimal for a dialog.

This function was introduced in 5.0.

KDirOperator *KFileWidget::dirOperator()

Returns the KDirOperator used to navigate the filesystem

[signal, since 4.4] void KFileWidget::fileHighlighted(const QUrl &)

Emitted when the user highlights a file.

This function was introduced in 4.4.

[signal, since 4.4] void KFileWidget::fileSelected(const QUrl &)

Emitted when the user selects a file. It is only emitted in single- selection mode. The best way to get notified about selected file(s) is to connect to the okClicked() signal inherited from KDialog and call selectedFile(), selectedFiles(), selectedUrl() or selectedUrls().

This function was introduced in 4.4.

[signal, since 6.0] void KFileWidget::filterChanged(const KFileFilter &filter)

Emitted when the filter changed, i.e. the user entered an own filter or chose one of the predefined set via setFilters().

filter contains the new filter (only the extension part, not the explanation), i.e. "*.cpp" or "*.cpp *.cc".

This function was introduced in 6.0.

See also setFilters() and currentFilter().

KFileFilterCombo *KFileWidget::filterWidget() const

Returns the combobox that contains the filters

[static] QUrl KFileWidget::getStartUrl(const QUrl &startDir, QString &recentDirClass)

This method implements the logic to determine the user's default directory to be listed. E.g. the documents directory, home directory or a recently used directory.

startDir A URL specifying the initial directory, or using the kfiledialog:/// syntax to specify a last used directory. If this URL specifies a file name, it is ignored. Refer to the KFileWidget::KFileWidget() documentation for the kfiledialog:/// URL syntax.

recentDirClass If the kfiledialog:/// syntax is used, this will return the string to be passed to KRecentDirs::dir() and KRecentDirs::add().

Returns The URL that should be listed by default (e.g. by KFileDialog or KDirSelectDialog).

See also KFileWidget::KFileWidget().

[static] QUrl KFileWidget::getStartUrl(const QUrl &startDir, QString &recentDirClass, QString &fileName)

Similar to getStartUrl(const QUrl& startDir,QString& recentDirClass), but allows both the recent start directory keyword and a suggested file name to be returned.

startDir A URL specifying the initial directory and/or filename, or using the kfiledialog:/// syntax to specify a last used location. Refer to the KFileWidget::KFileWidget() documentation for the kfiledialog:/// URL syntax.

recentDirClass If the kfiledialog:/// syntax is used, this will return the string to be passed to KRecentDirs::dir() and KRecentDirs::add().

fileName The suggested file name, if specified as part of the StartDir URL.

Returns The URL that should be listed by default (e.g. by KFileDialog or KDirSelectDialog).

See also KFileWidget::KFileWidget().

bool KFileWidget::keepsLocation() const

Returns whether the contents of the location edit are kept when changing directories.

KUrlComboBox *KFileWidget::locationEdit() const

Returns the combobox used to type the filename or full location of the file.

KFile::Modes KFileWidget::mode() const

Returns the mode of the filedialog.

See also setMode().

QPushButton *KFileWidget::okButton() const

Returns a pointer to the OK-Button in the filedialog. Note that the button is hidden and unconnected when using KFileWidget alone; KFileDialog shows it and connects to it.

KFileWidget::OperationMode KFileWidget::operationMode() const

Returns the current operation mode, Opening, Saving or Other. Default is Other.

See also setOperationMode(), operationMode, and KFileWidget::OperationMode.

QString KFileWidget::selectedFile() const

Returns the full path of the selected file in the local filesystem. (Local files only)

QStringList KFileWidget::selectedFiles() const

Returns a list of all selected local files.

QUrl KFileWidget::selectedUrl() const

Returns The selected fully qualified filename.

See also setSelectedUrl().

QList<QUrl> KFileWidget::selectedUrls() const

Returns The list of selected URLs.

See also setSelectedUrls().

[signal] void KFileWidget::selectionChanged()

Emitted when the user highlights one or more files in multiselection mode.

Note: fileHighlighted() or fileSelected() are @em not emitted in multiselection mode. You may use selectedItems() to ask for the current highlighted items.

See also fileSelected.

void KFileWidget::setConfirmOverwrite(bool enable)

Sets whether the user should be asked for confirmation when an overwrite might occur.

enable Set this to true to enable checking.

void KFileWidget::setCustomWidget(QWidget *widget)

Set a custom widget that should be added to the file dialog.

widget A widget, or a widget of widgets, for displaying custom data in the file widget. This can be used, for example, to display a check box with the title "Open as read-only". When creating this widget, you don't need to specify a parent, since the widget's parent will be set automatically by KFileWidget.

void KFileWidget::setCustomWidget(const QString &text, QWidget *widget)

Sets a custom widget that should be added below the location and the filter editors.

text Label of the custom widget, which is displayed below the labels "Location:" and "Filter:".

widget Any kind of widget, but preferable a combo box or a line editor to be compliant with the location and filter layout. When creating this widget, you don't need to specify a parent, since the widget's parent will be set automatically by KFileWidget.

[since 6.0] void KFileWidget::setFilters(const QList<KFileFilter> &filters, const KFileFilter &activeFilter = KFileFilter())

Set the filters to be used.

Each item of the list corresponds to a selectable filter.

Only one filter is active at a time.

activeFilter the initially active filter

This function was introduced in 6.0.

void KFileWidget::setInlinePreviewShown(bool show)

Forces the inline previews to be shown or hidden, depending on show.

show Whether to show inline previews or not.

void KFileWidget::setKeepLocation(bool keep)

Sets whether the filename/url should be kept when changing directories. This is for example useful when having a predefined filename where the full path for that file is searched.

This is implicitly set when operationMode() is KFileWidget::Saving

getSaveFileName() and getSaveUrl() set this to true by default, so that you can type in the filename and change the directory without having to type the name again.

void KFileWidget::setLocationLabel(const QString &text)

Sets the text to be displayed in front of the selection.

The default is "Location". Most useful if you want to make clear what the location is used for.

void KFileWidget::setMode(KFile::Modes m)

Sets the mode of the dialog.

The mode is defined as (in kfile.h):

enum Mode {
     File         = 1,
     Directory    = 2,
     Files        = 4,
     ExistingOnly = 8,
     LocalOnly    = 16,
};

You can OR the values, e.g.

KFile::Modes mode = KFile::Files |
                    KFile::ExistingOnly |
                    KFile::LocalOnly );
setMode( mode );

See also mode().

void KFileWidget::setOperationMode(KFileWidget::OperationMode)

Sets the operational mode of the filedialog to Saving, Opening or Other. This will set some flags that are specific to loading or saving files. E.g. setKeepLocation() makes mostly sense for a save-as dialog. So setOperationMode( KFileWidget::Saving ); sets setKeepLocation for example.

The mode Saving, together with a default filter set via setMimeFilter() will make the filter combobox read-only.

The default mode is Opening.

Call this method right after instantiating KFileWidget.

See also operationMode and KFileWidget::OperationMode.

void KFileWidget::setPreviewWidget(KPreviewWidgetBase *w)

Adds a preview widget and enters the preview mode.

In this mode the dialog is split and the right part contains your preview widget.

Ownership is transferred to KFileWidget. You need to create the preview-widget with "new", i.e. on the heap.

w The widget to be used for the preview.

[since 5.33] void KFileWidget::setSelectedUrl(const QUrl &url)

Sets the URL to preselect to url

This method handles absolute URLs (remember to use fromLocalFile for local paths). It also handles relative URLs, which you should construct like this: QUrl relativeUrl; relativeUrl.setPath(fileName);

This function was introduced in 5.33.

See also selectedUrl().

[since 5.75] void KFileWidget::setSelectedUrls(const QList<QUrl> &urls)

Sets a list of URLs as preselected

This function was introduced in 5.75.

See also selectedUrls() and setSelectedUrl.

[since 5.43] void KFileWidget::setSupportedSchemes(const QStringList &schemes)

Set the URL schemes that the file widget should allow navigating to.

If the returned list is empty, all schemes are supported.

This function was introduced in 5.43.

See also supportedSchemes() and QFileDialog::setSupportedSchemes.

void KFileWidget::setUrl(const QUrl &url, bool clearforward = true)

Sets the directory to view.

url URL to show.

clearforward Indicates whether the forward queue should be cleared.

[since 5.0] void KFileWidget::setViewMode(KFile::FileView mode)

Sets how the view should be displayed.

This function was introduced in 5.0.

See also KFile::FileView.

[slot] void KFileWidget::slotCancel()

[slot] void KFileWidget::slotOk()

Called when clicking ok (when this widget is used in KFileDialog) Might or might not call accept().

[since 5.43] QStringList KFileWidget::supportedSchemes() const

Returns the URL schemes that the file widget should allow navigating to.

If the returned list is empty, all schemes are supported. Examples for schemes are "file" or "ftp".

This function was introduced in 5.43.

See also setSupportedSchemes() and QFileDialog::supportedSchemes.