PySide.QtGui.QFileDialog class provides a dialog that allow users to select files or directories.
PySide.QtGui.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 PySide.QtGui.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(self,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"))
In the above example, a modal PySide.QtGui.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”.
若希望使用多个过滤器,分隔每过滤器采用 two 分号。例如:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
You can create your own PySide.QtGui.QFileDialog without using the static functions. By calling PySide.QtGui.QFileDialog.setFileMode() , you can specify what the user must select in the dialog:
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.AnyFile)
在以上范例中,文件对话框的模式被设为 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 若用户必须选择现有文件,或 Directory 若只有目录可以被选择。见 QFileDialog.FileMode 枚举了解模式的完整列表。
PySide.QtGui.QFileDialog.fileMode() 特性包含对话框的操作模式;这指示用户期望选择什么类型的对象。使用 PySide.QtGui.QFileDialog.setNameFilter() to set the dialog's file filter. For example:
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"))
在以上范例中,过滤器被设为 "Images (*.png *.xpm *.jpg)" ,这意味着文件具有扩展名 png , xpm ,或 jpg will be shown in the PySide.QtGui.QFileDialog . You can apply several filters by using PySide.QtGui.QFileDialog.setNameFilters() 。使用 PySide.QtGui.QFileDialog.selectNameFilter() to select one of the filters you've given as the file dialog's default filter.
文件对话框有 2 种查看模式: List and Detail . List 将当前目录内容呈现为文件和目录名称列表。 Detail 显示文件和目录名称列表,但每个名称旁边还提供额外信息 (譬如:文件大小和修改日期)。设置模式采用 PySide.QtGui.QFileDialog.setViewMode() :
dialog.setViewMode(QFileDialog.Detail)
当创建自己的文件对话框时需要使用的最后一个重要函数是 PySide.QtGui.QFileDialog.selectedFiles() .
if dialog.exec_():
fileNames = dialog.selectedFiles()
在以上范例中,创建并展示模态文件对话框。若用户点击 OK,选中文件被放入 fileName .
可以设置对话框的工作目录采用 PySide.QtGui.QFileDialog.setDirectory() . Each file in the current directory can be selected using the PySide.QtGui.QFileDialog.selectFile() 函数。
标准对话框 example shows how to use PySide.QtGui.QFileDialog as well as other built-in Qt dialogs.
| 参数: |
|
|---|
构造文件对话框采用给定 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 .
此枚举被用于指示用户可以在文件对话框中选择什么。即:对话框将返回什么,若用户点击 OK。
| 常量 | 描述 |
|---|---|
| QFileDialog.AnyFile | 文件的名称,无论它是否存在。 |
| QFileDialog.ExistingFile | 单个现有文件的名称。 |
| QFileDialog.Directory | The name of a directory. Both files and directories are displayed. |
| QFileDialog.ExistingFiles | 零个或多个现有文件的名称。 |
从 Qt 4.5 起此值已过时:
| 常量 | 描述 |
|---|---|
| QFileDialog.DirectoryOnly | 使用 Directory and setOption( ShowDirsOnly , true) 代替。 |
| 常量 | 描述 |
|---|---|
| QFileDialog.ShowDirsOnly | Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.) |
| QFileDialog.DontResolveSymlinks | Don't resolve symlinks in the file dialog. By default symlinks are resolved. |
| QFileDialog.DontConfirmOverwrite | Don't ask for confirmation if an existing file is selected. By default confirmation is requested. |
| QFileDialog.DontUseNativeDialog | Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of PySide.QtGui.QFileDialog that contains the Q_OBJECT() 宏。 |
| QFileDialog.ReadOnly | Indicates that the model is readonly. |
| QFileDialog.HideNameFilterDetails | Indicates if the file name filter details are hidden or not. |
| QFileDialog.DontUseSheet | In 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() 代替。 |
| 常量 | 描述 |
|---|---|
| QFileDialog.LookIn | |
| QFileDialog.FileName | |
| QFileDialog.FileType | |
| QFileDialog.Accept | |
| QFileDialog.Reject |
This enum describes the view mode of the file dialog; i.e. what information about each file will be displayed.
| 常量 | 描述 |
|---|---|
| QFileDialog.Detail | Displays an icon, a name, and details for each item in the directory. |
| QFileDialog.List | Displays only an icon and a name for each item in the directory. |
| 常量 | 描述 |
|---|---|
| QFileDialog.AcceptOpen | |
| QFileDialog.AcceptSave |
| 返回类型: | PySide.QtGui.QFileDialog.AcceptMode |
|---|
This property holds the accept mode of the dialog.
The action mode defines whether the dialog is for opening or saving files.
默认情况下,此特性被设为 AcceptOpen .
另请参阅
QFileDialog.AcceptMode
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds whether the filedialog should ask before accepting a selected file, when the accept mode is AcceptSave .
Use setOption( DontConfirmOverwrite , !*enabled* ) or !testOption( DontConfirmOverwrite ) 取而代之。
| 参数: | path – unicode |
|---|
| 返回类型: | unicode |
|---|
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).
| 返回类型: | PySide.QtCore.QDir |
|---|
Returns the directory currently being displayed in the dialog.
| 参数: | directory – unicode |
|---|
| 返回类型: | PySide.QtGui.QFileDialog.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.
默认情况下,此特性被设为 AnyFile .
This function will set the labels for the FileName and Accept QFileDialog.DialogLabel s. It is possible to set custom text after the call to PySide.QtGui.QFileDialog.setFileMode() .
另请参阅
QFileDialog.FileMode
| 参数: | file – unicode |
|---|
| 参数: | 文件 – list of strings |
|---|
| 返回类型: | PySide.QtCore.QDir.Filters |
|---|
Returns the filter that is used when displaying files.
| 参数: | filter – unicode |
|---|
| 返回类型: | 字符串列表 |
|---|
| 参数: |
|
|---|---|
| 返回类型: |
unicode |
| 参数: |
|
|---|---|
| 返回类型: |
(fileName, selectedFilter) |
| 参数: |
|
|---|---|
| 返回类型: |
(fileNames, selectedFilter) |
| 参数: |
|
|---|---|
| 返回类型: |
(fileName, selectedFilter) |
| 返回类型: | 字符串列表 |
|---|
以路径列表形式返回文件对话框的浏览历史。
| 返回类型: | PySide.QtGui.QFileIconProvider |
|---|
返回文件对话框使用的图标提供程序。
| 返回类型: | PySide.QtCore.bool |
|---|
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds Whether the filedialog is read-only.
If this property is set to false, the file dialog will allow renaming, and deleting of files and directories and creating directories.
Use setOption( ReadOnly , enabled ) or testOption( ReadOnly ) 取而代之。
| 返回类型: | PySide.QtGui.QAbstractItemDelegate |
|---|
返回用于在文件对话框视图中呈现项的项委托。
| 参数: | label – PySide.QtGui.QFileDialog.DialogLabel |
|---|---|
| 返回类型: | unicode |
返回文件对话框展示的文本在指定 label .
| 返回类型: | 字符串列表 |
|---|
返回在此文件对话框中运转的文件类型过滤器。
| 参数: |
|
|---|
这是重载函数。
This function connects one of its signals to the slot specified by receiver and member . The specific signal depends is PySide.QtGui.QFileDialog.filesSelected() if PySide.QtGui.QFileDialog.fileMode() is ExistingFiles and PySide.QtGui.QFileDialog.fileSelected() if PySide.QtGui.QFileDialog.fileMode() is anything else.
信号将断开槽连接,当对话框被关闭时。
| 返回类型: | PySide.QtGui.QFileDialog.Options |
|---|
| 返回类型: | PySide.QtGui.QAbstractProxyModel |
|---|
Returns the proxy model used by the file dialog. By default no proxy is set.
| 返回类型: | PySide.QtCore.bool |
|---|
This property holds whether the filedialog should resolve shortcuts.
If this property is set to true, the file dialog will resolve shortcuts or symbolic links.
Use setOption( DontResolveSymlinks , !``enabled`` ) or !testOption( DontResolveSymlinks ).
| 参数: | state – PySide.QtCore.QByteArray |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Restores the dialogs's layout, history and current directory to the state 指定。
通常,这用于结合 PySide.QtCore.QSettings to restore the size from a past session.
Returns false if there are errors
| 返回类型: | PySide.QtCore.QByteArray |
|---|
Saves the state of the dialog's layout, history and current directory.
通常,这用于结合 PySide.QtCore.QSettings to remember the size for a future session. A version number is stored as part of the data.
| 参数: | filename – unicode |
|---|
选择给定 filename 在文件对话框。
| 参数: | filter – unicode |
|---|
| 参数: | filter – unicode |
|---|
设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.
| 返回类型: | 字符串列表 |
|---|
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 , PySide.QtGui.QFileDialog.selectedFiles() contains the current path in the viewport.
| 返回类型: | unicode |
|---|
| 返回类型: | unicode |
|---|
Returns the filter that the user selected in the file dialog.
| 参数: | mode – PySide.QtGui.QFileDialog.AcceptMode |
|---|
This property holds the accept mode of the dialog.
The action mode defines whether the dialog is for opening or saving files.
默认情况下,此特性被设为 AcceptOpen .
另请参阅
QFileDialog.AcceptMode
| 参数: | enabled – PySide.QtCore.bool |
|---|
This property holds whether the filedialog should ask before accepting a selected file, when the accept mode is AcceptSave .
Use setOption( DontConfirmOverwrite , !*enabled* ) or !testOption( DontConfirmOverwrite ) 取而代之。
| 参数: | suffix – unicode |
|---|
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).
| 参数: | directory – unicode |
|---|
设置文件对话框的当前 directory .
| 参数: | directory – PySide.QtCore.QDir |
|---|
这是重载函数。
| 参数: | mode – PySide.QtGui.QFileDialog.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.
默认情况下,此特性被设为 AnyFile .
This function will set the labels for the FileName and Accept QFileDialog.DialogLabel s. It is possible to set custom text after the call to PySide.QtGui.QFileDialog.setFileMode() .
另请参阅
QFileDialog.FileMode
| 参数: | filters – PySide.QtCore.QDir.Filters |
|---|
| 参数: | filter – unicode |
|---|
| 参数: | filters – list of strings |
|---|
| 参数: | paths – list of strings |
|---|
Sets the browsing history of the filedialog to contain the given paths .
| 参数: | provider – PySide.QtGui.QFileIconProvider |
|---|
Sets the icon provider used by the filedialog to the specified provider .
| 参数: | delegate – PySide.QtGui.QAbstractItemDelegate |
|---|
Sets the item delegate used to render items in the views in the file dialog to the given delegate .
警告
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 PySide.QtGui.QAbstractItemDelegate.closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.
Note that the model used is PySide.QtGui.QFileSystemModel . It has custom item data roles, which is described by the QFileSystemModel.Roles enum. You can use a PySide.QtGui.QFileIconProvider if you only want custom icons.
| 参数: |
|
|---|
设置 text shown in the filedialog in the specified label .
| 参数: | filter – unicode |
|---|
Sets the filter used in the file dialog to the given filter .
若 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++")
| 参数: | enabled – PySide.QtCore.bool |
|---|
| 参数: | filters – list of strings |
|---|
设置 filters used in the file dialog.
filters = QStringList()
filters << "Image files (*.png *.xpm *.jpg)"
<< "Text files (*.txt)"
<< "Any files (*)"
dialog = QFileDialog(this)
dialog.setNameFilters(filters)
dialog.exec_()
| 参数: |
|
|---|
设置给定 option to be enabled if on is true; otherwise, clears the given option .
| 参数: | options – PySide.QtGui.QFileDialog.Options |
|---|
| 参数: | model – PySide.QtGui.QAbstractProxyModel |
|---|
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 .
| 参数: | enabled – PySide.QtCore.bool |
|---|
This property holds Whether the filedialog is read-only.
If this property is set to false, the file dialog will allow renaming, and deleting of files and directories and creating directories.
Use setOption( ReadOnly , enabled ) or testOption( ReadOnly ) 取而代之。
| 参数: | enabled – PySide.QtCore.bool |
|---|
This property holds whether the filedialog should resolve shortcuts.
If this property is set to true, the file dialog will resolve shortcuts or symbolic links.
Use setOption( DontResolveSymlinks , !``enabled`` ) or !testOption( DontResolveSymlinks ).
| 参数: | urls – |
|---|
| 参数: | mode – PySide.QtGui.QFileDialog.ViewMode |
|---|
This property holds the way files and directories are displayed in the dialog.
默认情况下, Detail mode is used to display information about files and directories.
另请参阅
QFileDialog.ViewMode
| 返回类型: |
|---|
返回目前在侧边栏中的 URL 列表
| 参数: | option – PySide.QtGui.QFileDialog.Option |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if the given option 被启用;否则,返回 false。
| 返回类型: | PySide.QtGui.QFileDialog.ViewMode |
|---|
This property holds the way files and directories are displayed in the dialog.
默认情况下, Detail mode is used to display information about files and directories.
另请参阅
QFileDialog.ViewMode