The QFileDialog class provides a dialog that allow users to select files or directories. 更多...
继承 QDialog .
The QFileDialog class provides a dialog that allow users to select files or directories.
The 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 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(this, tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
In the above example, a modal 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 QFileDialog without using the static functions. By calling setFileMode (), you can specify what the user must select in the dialog:
QFileDialog dialog(this); 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. 使用 ExistingFile if the user must select an existing file, or Directory if only a directory may be selected. See the QFileDialog.FileMode enum for the complete list of modes.
fileMode property contains the mode of operation for the dialog; this indicates what types of objects the user is expected to select. Use setNameFilter () to set the dialog's file filter. For example:
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
In the above example, the filter is set to "Images (*.png *.xpm *.jpg)" , this means that only files with the extension png , xpm ,或 jpg will be shown in the QFileDialog. You can apply several filters by using setNameFilters ()。使用 selectNameFilter () to select one of the filters you've given as the file dialog's default filter.
The file dialog has two view modes: List and Detail . List presents the contents of the current directory as a list of file and directory names. Detail also displays a list of file and directory names, but provides additional information alongside each name, such as the file size and modification date. Set the mode with setViewMode ():
dialog.setViewMode(QFileDialog.Detail);
The last important function you will need to use when creating your own file dialog is selectedFiles ().
QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles();
In the above example, a modal file dialog is created and shown. If the user clicked OK, the file they selected is put in fileName .
The dialog's working directory can be set with setDirectory (). Each file in the current directory can be selected using the selectFile () 函数。
标准对话框 example shows how to use QFileDialog as well as other built-in Qt dialogs.
| 常量 | 值 |
|---|---|
| QFileDialog.AcceptOpen | 0 |
| QFileDialog.AcceptSave | 1 |
| 常量 | 值 |
|---|---|
| QFileDialog.LookIn | 0 |
| QFileDialog.FileName | 1 |
| QFileDialog.FileType | 2 |
| QFileDialog.Accept | 3 |
| QFileDialog.Reject | 4 |
This enum is used to indicate what the user may select in the file dialog; i.e. what the dialog will return if the user clicks OK.
| 常量 | 值 | 描述 |
|---|---|---|
| QFileDialog.AnyFile | 0 | The name of a file, whether it exists or not. |
| QFileDialog.ExistingFile | 1 | 单个现有文件的名称。 |
| QFileDialog.Directory | 2 | The name of a directory. Both files and directories are displayed. |
| QFileDialog.ExistingFiles | 3 | 零个或多个现有文件的名称。 |
从 Qt 4.5 起此值已过时:
| 常量 | 值 | 描述 |
|---|---|---|
| QFileDialog.DirectoryOnly | 4 | 使用 Directory and setOption( ShowDirsOnly , true) 代替。 |
另请参阅 setFileMode ().
| 常量 | 值 | 描述 |
|---|---|---|
| QFileDialog.ShowDirsOnly | 0x00000001 | Only show directories in the file dialog. By default both files and directories are shown. (Valid only in the Directory file mode.) |
| QFileDialog.DontResolveSymlinks | 0x00000002 | Don't resolve symlinks in the file dialog. By default symlinks are resolved. |
| QFileDialog.DontConfirmOverwrite | 0x00000004 | Don't ask for confirmation if an existing file is selected. By default confirmation is requested. |
| QFileDialog.DontUseNativeDialog | 0x00000010 | Don't use the native file dialog. By default, the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT 宏。 |
| QFileDialog.ReadOnly | 0x00000020 | Indicates that the model is readonly. |
| QFileDialog.HideNameFilterDetails | 0x00000040 | Indicates if the file name filter details are hidden or not. |
| QFileDialog.DontUseSheet | 0x00000008 | 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.DontUseCustomDirectoryIcons | 0x00000080 | Always use the default directory icon. Some platforms allow the user to set a different icon. Custom icon lookup cause a big performance impact over network or removable drives. Setting this will affect the behavior of the icon provider. This enum value was added in Qt 4.8.6. |
The Options type is a typedef for QFlags <Option>. It stores an OR combination of Option values.
This enum describes the view mode of the file dialog; i.e. what information about each file will be displayed.
| 常量 | 值 | 描述 |
|---|---|---|
| QFileDialog.Detail | 0 | Displays an icon, a name, and details for each item in the directory. |
| QFileDialog.List | 1 | Displays only an icon and a name for each item in the directory. |
另请参阅 setViewMode ().
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造文件对话框采用给定 parent 和小部件 flags .
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造文件对话框采用给定 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 .
重实现自 QDialog.accept ().
重实现自 QWidget.changeEvent ().
Returns the directory currently being displayed in the 对话框。
另请参阅 setDirectory ().
重实现自 QDialog.done ().
Returns the filter that is used when displaying files.
该函数在 Qt 4.4 引入。
另请参阅 setFilter ().
This is a convenience static function that will return an existing directory selected by the user.
QString dir = QFileDialog.getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks);
This function creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.
The dialog's working directory is set to dir ,和 caption is set to caption . Either of these may be an empty string in which case the current directory and a default caption will be used respectively.
options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass. To ensure a native file dialog, ShowDirsOnly must be set.
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog . On Windows CE, if the device has no native file dialog, a QFileDialog 会被使用。
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp 是 symlink to /var/tmp , the file dialog will change to /var/tmp after entering /usr/tmp 。若 options includes DontResolveSymlinks , the file dialog will treat symlinks as regular directories.
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar.
On Symbian^3 the options parameter is only used to define if the native file dialog is used.
警告: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getSaveFileName ().
This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string.
QString fileName = QFileDialog.getOpenFileName(this, tr("Open File"), "/home", tr("Images (*.png *.xpm *.jpg)"));
The function creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.
The file dialog's working directory will be set to dir . 若 dir includes a file name, the file will be selected. Only files that match the given filter are shown. The filter selected is set to selectedFilter . The parameters dir , selectedFilter ,和 filter may be empty strings. If you want multiple filters, separate them with ';;', for example:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.
The dialog's caption is set to caption 。若 caption is not specified then a default caption will be used.
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp 是 symlink to /var/tmp , the file dialog will change to /var/tmp after entering /usr/tmp 。若 options includes DontResolveSymlinks , the file dialog will treat symlinks as regular directories.
On Symbian^3 the parameter selectedFilter has no meaning 和 options parameter is only used to define if the native file dialog is used.
警告: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileNames (), getSaveFileName (), and getExistingDirectory ().
This is a convenience static function that will return one or more existing files selected by the user.
QStringList files = QFileDialog.getOpenFileNames( this, "Select one or more files to open", "/home", "Images (*.png *.xpm *.jpg)");
This function creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.
The file dialog's working directory will be set to dir . 若 dir includes a file name, the file will be selected. The filter is set to filter so that only those files which match the filter are shown. The filter selected is set to selectedFilter . The parameters dir , selectedFilter and filter may be empty strings. If you need multiple filters, separate them with ';;', for instance:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
The dialog's caption is set to caption 。若 caption is not specified then a default caption will be used.
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp 是 symlink to /var/tmp , the file dialog will change to /var/tmp after entering /usr/tmp 。 options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.
注意: If you want to iterate over the list of files, you should iterate over a copy. For example:
QStringList list = files; QStringList.Iterator it = list.begin(); while(it != list.end()) { myProcessing(*it); ++it; }
On Symbian^3 the parameter selectedFilter has no meaning 和 options parameter is only used to define if the native file dialog is used. On Symbian^3, this function can only return a single filename.
警告: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getSaveFileName (),和 getExistingDirectory ().
This is a convenience static function that will return a file name selected by the user. The file does not have to exist.
It creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.
QString fileName = QFileDialog.getSaveFileName(this, tr("Save File"), "/home/jana/untitled.png", tr("Images (*.png *.xpm *.jpg)"));
The file dialog's working directory will be set to dir . 若 dir includes a file name, the file will be selected. Only files that match the filter are shown. The filter selected is set to selectedFilter . The parameters dir , selectedFilter ,和 filter may be empty strings. Multiple filters are separated with ';;'. For instance:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
options argument holds various options about how to run the dialog, see the QFileDialog.Option enum for more information on the flags you can pass.
The default filter can be chosen by setting selectedFilter to the desired value.
The dialog's caption is set to caption 。若 caption is not specified, a default caption will be used.
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog .
On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar. On Mac OS X, with its native file dialog, the filter argument is ignored.
On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp 是 symlink to /var/tmp , the file dialog will change to /var/tmp after entering /usr/tmp 。若 options includes DontResolveSymlinks the file dialog will treat symlinks as regular directories.
On Symbian^3 the parameters filter and selectedFilter have no meaning. The options 参数 is only used to define if the native file dialog is used.
警告: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog 构造函数。
另请参阅 getOpenFileName (), getOpenFileNames (),和 getExistingDirectory ().
Returns the browsing history of the filedialog as a list of paths.
另请参阅 setHistory ().
Returns the icon provider used by the filedialog.
另请参阅 setIconProvider ().
Returns the item delegate used to render the items in the views in the filedialog.
另请参阅 setItemDelegate ().
Returns the text shown in the filedialog in the specified label .
另请参阅 setLabelText ().
Returns the file type filters that are in operation on this file 对话框。
该函数在 Qt 4.4 引入。
另请参阅 setNameFilters ().
这是重载函数。
This function connects one of its signals to the slot specified by receiver and member . The specific signal depends is filesSelected () if fileMode is ExistingFiles and fileSelected () if fileMode is anything else.
The signal will be disconnected from the slot when the dialog is closed.
该函数在 Qt 4.5 引入。
Returns the proxy model used by the file dialog. By default no proxy is set.
另请参阅 setProxyModel ().
Restores the dialogs's layout, history and current directory to the state 指定。
通常,这用于结合 QSettings to restore the size from a past session.
Returns false if there are errors
该函数在 Qt 4.3 引入。
Saves the state of the dialog's layout, history and current 目录。
通常,这用于结合 QSettings to remember the size for a future session. A version number is stored as part of the data.
该函数在 Qt 4.3 引入。
Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode 不是 ExistingFiles or ExistingFile , selectedFiles() contains the current path in the viewport.
另请参阅 selectedNameFilter () 和 selectFile ().
Returns the filter that the user selected in the file 对话框。
该函数在 Qt 4.4 引入。
另请参阅 selectedFiles ().
选择给定 filename 在文件对话框。
另请参阅 selectedFiles ().
设置当前文件类型 filter . Multiple filters can be passed in filter by separating them with semicolons or spaces.
该函数在 Qt 4.4 引入。
另请参阅 setNameFilter (), setNameFilters (),和 selectedNameFilter ().
设置文件对话框的当前 directory .
另请参阅 directory ().
这是重载函数。
Sets the filter used by the model to filters . The filter is used to specify the kind of files that should be shown.
该函数在 Qt 4.4 引入。
另请参阅 filter ().
Sets the browsing history of the filedialog to contain the given paths .
另请参阅 history ().
Sets the icon provider used by the filedialog to the specified provider .
另请参阅 iconProvider ().
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 closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.
Note that the model used is QFileSystemModel . It has custom item data roles, which is described by the Roles enum. You can use a QFileIconProvider if you only want custom icons.
另请参阅 itemDelegate (), setIconProvider (),和 QFileSystemModel .
设置 text shown in the filedialog in the specified label .
另请参阅 labelText ().
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++");
该函数在 Qt 4.4 引入。
另请参阅 setNameFilters ().
设置 filters used in the file dialog.
QStringList filters; filters << "Image files (*.png *.xpm *.jpg)" << "Text files (*.txt)" << "Any files (*)"; QFileDialog dialog(this); dialog.setNameFilters(filters); dialog.exec_();
该函数在 Qt 4.4 引入。
另请参阅 nameFilters ().
设置给定 option to be enabled if on is true; otherwise, clears the given option .
该函数在 Qt 4.5 引入。
另请参阅 options and testOption ().
model argument has it's ownership transferred to Qt.
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 .
该函数在 Qt 4.3 引入。
另请参阅 proxyModel ().
设置 urls 位于侧边栏中。
例如:
QList<QUrl> urls;
urls << QUrl.fromLocalFile("/home/gvatteka/dev/qt-45")
<< QUrl.fromLocalFile(QDesktopServices.storageLocation(QDesktopServices.MusicLocation));
QFileDialog dialog;
dialog.setSidebarUrls(urls);
dialog.setFileMode(QFileDialog.AnyFile);
if(dialog.exec()) {
// ...
}
文件对话框看起来就像这样:
该函数在 Qt 4.3 引入。
另请参阅 sidebarUrls ().
重实现自 QWidget.setVisible ().
返回目前在侧边栏中的 URL 列表
该函数在 Qt 4.3 引入。
另请参阅 setSidebarUrls ().
Returns true if the given option is enabled; otherwise, returns false.
该函数在 Qt 4.5 引入。
另请参阅 options and setOption ().
This is the default overload of this signal.
When the current file changes, this signal is emitted with the new file name as the path 参数。
另请参阅 filesSelected ().
This is the default overload of this signal.
此信号被发射当用户键入 directory .
该函数在 Qt 4.3 引入。
This is the default overload of this signal.
When the selection changes and the dialog is accepted, this signal is emitted with the (possibly empty) selected file .
另请参阅 currentChanged () 和 QDialog.Accepted .
This is the default overload of this signal.
When the selection changes and the dialog is accepted, this signal is emitted with the (possibly empty) list of selected files.
另请参阅 currentChanged () 和 QDialog.Accepted .
This is the default overload of this signal.
This signal is emitted when the user selects a filter .
该函数在 Qt 4.3 引入。