• PySide 模块
  • PySide.QtGui
  • 内容表

    上一话题

    QPageSetupDialog

    下一话题

    QWizard

    QFileDialog

    概要

    函数

    信号

    静态函数

    • def getExistingDirectory ([parent=None[, caption=”“[, dir=”“[, options=QFileDialog.ShowDirsOnly]]]])
    • def getOpenFileName ([parent=None[, caption=”“[, dir=”“[, filter=”“[, selectedFilter=”“[, options=0]]]]]])
    • def getOpenFileNames ([parent=None[, caption=”“[, dir=”“[, filter=”“[, selectedFilter=”“[, options=0]]]]]])
    • def getSaveFileName ([parent=None[, caption=”“[, dir=”“[, filter=”“[, selectedFilter=”“[, options=0]]]]]])

    详细描述

    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.

    class PySide.QtGui. QFileDialog ( parent , f )
    class PySide.QtGui. QFileDialog ( [ parent=None [ , caption="" [ , directory="" [ , filter="" ] ] ] ] )
    参数:
    • f PySide.QtCore.Qt.WindowFlags
    • directory – unicode
    • caption – unicode
    • filter – unicode
    • parent PySide.QtGui.QWidget

    构造文件对话框采用给定 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 .

    PySide.QtGui.QFileDialog. FileMode

    此枚举被用于指示用户可以在文件对话框中选择什么。即:对话框将返回什么,若用户点击 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) 代替。
    PySide.QtGui.QFileDialog. Option
    常量 描述
    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() 代替。
    PySide.QtGui.QFileDialog. DialogLabel
    常量 描述
    QFileDialog.LookIn  
    QFileDialog.FileName  
    QFileDialog.FileType  
    QFileDialog.Accept  
    QFileDialog.Reject  
    PySide.QtGui.QFileDialog. ViewMode

    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.
    PySide.QtGui.QFileDialog. AcceptMode
    常量 描述
    QFileDialog.AcceptOpen  
    QFileDialog.AcceptSave  
    PySide.QtGui.QFileDialog. acceptMode ( )
    返回类型: 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.QtGui.QFileDialog. confirmOverwrite ( )
    返回类型: 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 ) 取而代之。

    PySide.QtGui.QFileDialog. currentChanged ( path )
    参数: path – unicode
    PySide.QtGui.QFileDialog. defaultSuffix ( )
    返回类型: 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.QtGui.QFileDialog. directory ( )
    返回类型: PySide.QtCore.QDir

    Returns the directory currently being displayed in the dialog.

    PySide.QtGui.QFileDialog. directoryEntered ( directory )
    参数: directory – unicode
    PySide.QtGui.QFileDialog. fileMode ( )
    返回类型: 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

    PySide.QtGui.QFileDialog. fileSelected ( file )
    参数: file – unicode
    PySide.QtGui.QFileDialog. filesSelected ( 文件 )
    参数: 文件 – list of strings
    PySide.QtGui.QFileDialog. filter ( )
    返回类型: PySide.QtCore.QDir.Filters

    Returns the filter that is used when displaying files.

    PySide.QtGui.QFileDialog. filterSelected ( filter )
    参数: filter – unicode
    PySide.QtGui.QFileDialog. filters ( )
    返回类型: 字符串列表

    使用 PySide.QtGui.QFileDialog.nameFilters() 代替。

    static PySide.QtGui.QFileDialog. getExistingDirectory ( [ parent=None [ , caption="" [ , dir="" [ , options=QFileDialog.ShowDirsOnly ] ] ] ] )
    参数:
    • parent PySide.QtGui.QWidget
    • caption – unicode
    • dir – unicode
    • options PySide.QtGui.QFileDialog.Options
    返回类型:

    unicode

    static PySide.QtGui.QFileDialog. getOpenFileName ( [ parent=None [ , caption="" [ , dir="" [ , filter="" [ , selectedFilter="" [ , options=0 ] ] ] ] ] ] )
    参数:
    • parent PySide.QtGui.QWidget
    • caption – unicode
    • dir – unicode
    • filter – unicode
    • selectedFilter – unicode
    • options PySide.QtGui.QFileDialog.Options
    返回类型:

    (fileName, selectedFilter)

    static PySide.QtGui.QFileDialog. getOpenFileNames ( [ parent=None [ , caption="" [ , dir="" [ , filter="" [ , selectedFilter="" [ , options=0 ] ] ] ] ] ] )
    参数:
    • parent PySide.QtGui.QWidget
    • caption – unicode
    • dir – unicode
    • filter – unicode
    • selectedFilter – unicode
    • options PySide.QtGui.QFileDialog.Options
    返回类型:

    (fileNames, selectedFilter)

    static PySide.QtGui.QFileDialog. getSaveFileName ( [ parent=None [ , caption="" [ , dir="" [ , filter="" [ , selectedFilter="" [ , options=0 ] ] ] ] ] ] )
    参数:
    • parent PySide.QtGui.QWidget
    • caption – unicode
    • dir – unicode
    • filter – unicode
    • selectedFilter – unicode
    • options PySide.QtGui.QFileDialog.Options
    返回类型:

    (fileName, selectedFilter)

    PySide.QtGui.QFileDialog. history ( )
    返回类型: 字符串列表

    以路径列表形式返回文件对话框的浏览历史。

    PySide.QtGui.QFileDialog. iconProvider ( )
    返回类型: PySide.QtGui.QFileIconProvider

    返回文件对话框使用的图标提供程序。

    PySide.QtGui.QFileDialog. isNameFilterDetailsVisible ( )
    返回类型: PySide.QtCore.bool
    PySide.QtGui.QFileDialog. isReadOnly ( )
    返回类型: 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.QFileDialog. itemDelegate ( )
    返回类型: PySide.QtGui.QAbstractItemDelegate

    返回用于在文件对话框视图中呈现项的项委托。

    PySide.QtGui.QFileDialog. labelText ( label )
    参数: label PySide.QtGui.QFileDialog.DialogLabel
    返回类型: unicode

    返回文件对话框展示的文本在指定 label .

    PySide.QtGui.QFileDialog. nameFilters ( )
    返回类型: 字符串列表

    返回在此文件对话框中运转的文件类型过滤器。

    PySide.QtGui.QFileDialog. open ( receiver , member )
    参数:

    这是重载函数。

    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.QFileDialog.Options
    PySide.QtGui.QFileDialog. proxyModel ( )
    返回类型: 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 ).

    PySide.QtGui.QFileDialog. restoreState ( state )
    参数: 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.QtGui.QFileDialog. saveState ( )
    返回类型: 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.

    PySide.QtGui.QFileDialog. selectFile ( filename )
    参数: filename – unicode

    选择给定 filename 在文件对话框。

    PySide.QtGui.QFileDialog. selectFilter ( filter )
    参数: filter – unicode

    使用 PySide.QtGui.QFileDialog.selectNameFilter() 代替。

    PySide.QtGui.QFileDialog. selectNameFilter ( filter )
    参数: filter – unicode

    设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.

    PySide.QtGui.QFileDialog. selectedFiles ( )
    返回类型: 字符串列表

    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.

    PySide.QtGui.QFileDialog. selectedFilter ( )
    返回类型: unicode

    使用 PySide.QtGui.QFileDialog.selectedNameFilter() 代替。

    PySide.QtGui.QFileDialog. selectedNameFilter ( )
    返回类型: unicode

    Returns the filter that the user selected in the file dialog.

    PySide.QtGui.QFileDialog. setAcceptMode ( mode )
    参数: 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

    PySide.QtGui.QFileDialog. setConfirmOverwrite ( enabled )
    参数: 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 ) 取而代之。

    PySide.QtGui.QFileDialog. setDefaultSuffix ( suffix )
    参数: 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).

    PySide.QtGui.QFileDialog. setDirectory ( directory )
    参数: directory – unicode

    设置文件对话框的当前 directory .

    PySide.QtGui.QFileDialog. setDirectory ( directory )
    参数: directory PySide.QtCore.QDir

    这是重载函数。

    PySide.QtGui.QFileDialog. setFileMode ( mode )
    参数: 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

    PySide.QtGui.QFileDialog. setFilter ( filters )
    参数: filters PySide.QtCore.QDir.Filters
    PySide.QtGui.QFileDialog. setFilter ( filter )
    参数: filter – unicode

    使用 PySide.QtGui.QFileDialog.setNameFilter() 代替。

    PySide.QtGui.QFileDialog. setFilters ( filters )
    参数: filters – list of strings

    使用 PySide.QtGui.QFileDialog.setNameFilters() 代替。

    PySide.QtGui.QFileDialog. setHistory ( paths )
    参数: paths – list of strings

    Sets the browsing history of the filedialog to contain the given paths .

    PySide.QtGui.QFileDialog. setIconProvider ( provider )
    参数: provider PySide.QtGui.QFileIconProvider

    Sets the icon provider used by the filedialog to the specified provider .

    PySide.QtGui.QFileDialog. setItemDelegate ( delegate )
    参数: 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.

    PySide.QtGui.QFileDialog. setLabelText ( label , text )
    参数:

    设置 text shown in the filedialog in the specified label .

    PySide.QtGui.QFileDialog. setNameFilter ( filter )
    参数: 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++")
    											
    PySide.QtGui.QFileDialog. setNameFilterDetailsVisible ( enabled )
    参数: enabled PySide.QtCore.bool
    PySide.QtGui.QFileDialog. setNameFilters ( filters )
    参数: 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_()
    										
    PySide.QtGui.QFileDialog. setOption ( option [ , on=true ] )
    参数:

    设置给定 option to be enabled if on is true; otherwise, clears the given option .

    PySide.QtGui.QFileDialog. setOptions ( options )
    参数: options PySide.QtGui.QFileDialog.Options
    PySide.QtGui.QFileDialog. setProxyModel ( model )
    参数: 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 .

    PySide.QtGui.QFileDialog. setReadOnly ( enabled )
    参数: 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 ).

    PySide.QtGui.QFileDialog. setSidebarUrls ( urls )
    参数: urls
    PySide.QtGui.QFileDialog. setViewMode ( mode )
    参数: 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

    PySide.QtGui.QFileDialog. sidebarUrls ( )
    返回类型:

    返回目前在侧边栏中的 URL 列表

    PySide.QtGui.QFileDialog. testOption ( option )
    参数: option PySide.QtGui.QFileDialog.Option
    返回类型: PySide.QtCore.bool

    Returns true if the given option 被启用;否则,返回 false。

    PySide.QtGui.QFileDialog. viewMode ( )
    返回类型: 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