FileDialog QML Type

Dialog component for choosing files from a local filesystem. 更多...

import 语句: import QtQuick.Dialogs 1.3
Since: Qt 5.1

特性

方法

详细描述

FileDialog provides a basic file chooser: it allows the user to select existing files and/or directories, or create new filenames. The dialog is initially invisible. You need to set the properties as desired first, then set visible to true or call open() .

Here is a minimal example to open a file dialog and exit after the user chooses a file:

import QtQuick 2.2
import QtQuick.Dialogs 1.0
FileDialog {
    id: fileDialog
    title: "Please choose a file"
    folder: shortcuts.home
    onAccepted: {
        console.log("You chose: " + fileDialog.fileUrls)
        Qt.quit()
    }
    onRejected: {
        console.log("Canceled")
        Qt.quit()
    }
    Component.onCompleted: visible = true
}
					

A FileDialog window is automatically transient for its parent window. So whether you declare the dialog inside an Item or inside a Window , the dialog will appear centered over the window containing the item, or over the Window that you declared.

实现为 FileDialog will be a platform file dialog if possible. If that isn't possible, then it will try to instantiate a QFileDialog . If that also isn't possible, then it will fall back to a QML implementation, DefaultFileDialog.qml. In that case you can customize the appearance by editing this file. DefaultFileDialog.qml contains a Rectangle to hold the dialog's contents, because certain embedded systems do not support multiple top-level windows. When the dialog becomes visible, it will automatically be wrapped in a Window if possible, or simply reparented on top of the main window if there can only be one window.

The QML implementation has a sidebar containing shortcuts to common platform-specific locations, and user-modifiable favorites. It uses application-specific settings to store the user's favorites, as well as other user-modifiable state, such as whether or not the sidebar is shown, the positions of the splitters, and the dialog size. The settings are stored in a section called QQControlsFileDialog of the application-specific QSettings . For example when testing an application with the qml tool, the QQControlsFileDialog section will be created in the Qml Runtime settings file (or registry entry). If an application is started via a custom C++ main() function, it is recommended to set the name , organization and domain in order to control the location of the application's settings. If you use 设置 objects in other parts of an application, they will be stored in other sections of the same file.

QFileDialog stores its settings globally instead of per-application. Platform-native file dialogs may or may not store settings in various platform-dependent ways.

特性文档编制

defaultSuffix : string

This property holds the 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).

If the first character is a dot ('.'), it is removed.

该特性在 Qt 5.10 引入。

fileUrl : url

The path of the file which was selected by the user.

注意: This property is set only if exactly one file was selected. In all other cases, it will be empty.

另请参阅 fileUrls .

fileUrls : list < url >

The list of file paths which were selected by the user.

folder : url

The path to the currently selected folder. Setting this property before invoking open() will cause the file browser to be initially positioned on the specified folder.

The value of this property is also updated after the dialog is closed.

By default, the url is empty.

注意: 在 iOS,若设置 folder to shortcuts.pictures , a native image picker dialog will be used for accessing the user's photo album. The URL returned can be set as source for Image . For this to be enabled, the Info.plist assigned to QMAKE_INFO_PLIST in the project file must contain the key, NSPhotoLibraryUsageDescription. See Info.plist documentation from Apple for more information regarding this key. This feature was added in Qt 5.5.

另请参阅 shortcuts .

modality : Qt::WindowModality

Whether the dialog should be shown modal with respect to the window containing the dialog's parent Item, modal with respect to the whole application, or non-modal.

By default it is Qt.WindowModal .

Modality does not mean that there are any blocking calls to wait for the dialog to be accepted or rejected; it's only that the user will be prevented from interacting with the parent window and/or the application windows at the same time. You probably need to write an onAccepted handler to actually load or save the chosen file.

nameFilters : list < string >

A list of strings to be used as file name filters. Each string can be a space-separated list of filters; filters may include the ? and * wildcards. The list of filters can also be enclosed in parentheses and a textual description of the filter can be provided.

例如:

FileDialog {
    nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
}
											

注意: Directories are not excluded by filters.

另请参阅 selectedNameFilter .

selectExisting : bool

Whether only existing files or directories can be selected.

By default, this property is true. This property must be set to the desired value before opening the dialog. Setting this property to false implies that the dialog is for naming a file to which to save something, or naming a folder to be created; therefore selectMultiple must be false.

selectFolder : bool

Whether the selected item should be a folder.

By default, this property is false. This property must be set to the desired value before opening the dialog. Setting this property to true implies that selectMultiple must be false and selectExisting must be true.

selectMultiple : bool

Whether more than one filename can be selected.

By default, this property is false. This property must be set to the desired value before opening the dialog. Setting this property to true implies that selectExisting must be true.

selectedNameFilter : string

Which of the nameFilters is currently selected.

This property can be set before the dialog is visible, to set the default name filter, and can also be set while the dialog is visible to set the current name filter. It is also updated when the user selects a different filter.

shortcuts : 对象

A map of some useful paths from QStandardPaths to their URLs. Each path is verified to exist on the user's computer before being added to this list, at the time when the FileDialog 被创建。

desktop QStandardPaths::DesktopLocation The user's desktop directory.
documents QStandardPaths::DocumentsLocation The directory containing user document files.
home QStandardPaths::HomeLocation The user's home directory.
music QStandardPaths::MusicLocation The directory containing the user's music or other audio files.
movies QStandardPaths::MoviesLocation The directory containing the user's movies and videos.
pictures QStandardPaths::PicturesLocation The directory containing the user's pictures or photos. It is always a kind of file : URL; but on some platforms, it will be specialized, such that the FileDialog will be realized as a gallery browser dialog.

例如, shortcuts.home will provide the URL of the user's home directory.

该特性在 Qt 5.5 引入。

sidebarVisible : bool

This property holds whether the sidebar in the dialog containing shortcuts and bookmarks is visible. By default it depends on the setting stored in the QQControlsFileDialog section of the application's 设置 .

该特性在 Qt 5.4 引入。

title : string

The title of the dialog window.

visible : bool

This property holds whether the dialog is visible. By default this is false.

另请参阅 modality .


方法文档编制

void close ()

Closes the dialog.

void open ()

Shows the dialog to the user. It is equivalent to setting visible to true.