📄 qfiledialog.cpp
字号:
extern QString qt_win_get_save_file_name(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);extern QStringList qt_win_get_open_file_names(const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter);extern QString qt_win_get_existing_directory(const QFileDialogArgs &args);#elif defined(Q_WS_MAC)extern QStringList qt_mac_get_open_file_names(const QFileDialogArgs &args, QString *pwd, QString *selectedFilter);extern QString qt_mac_get_save_file_name(const QFileDialogArgs &args, QString *pwd, QString *selectedFilter);#endif/*! 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. \code QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Images (*.png *.xpm *.jpg)")); \endcode The function creates a modal file dialog with the given \a parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget. The file dialog's working directory will be set to \a dir. If \a dir includes a file name, the file will be selected. Only files that match the given \a filter are shown. The filter selected is set to \a selectedFilter. The parameters \a dir, \a selectedFilter, and \a filter may be empty strings. The \a 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 \a caption. If \a caption is not specified then a default caption will be used. Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. On Mac OS X, the \a dir argument is ignored, the native dialog always displays the last visited directory. Note that 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 under the parent's title bar. Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, the file dialog will change to \c{/var/tmp} after entering \c{/usr/tmp}. If \a options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories. \sa getOpenFileNames(), getSaveFileName(), getExistingDirectory()*/QString QFileDialog::getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options){ if (qt_filedialog_open_filename_hook) return qt_filedialog_open_filename_hook(parent, caption, dir, filter, selectedFilter, options); QFileDialogArgs args; args.parent = parent; args.caption = caption; args.directory = QFileDialogPrivate::workingDirectory(dir); args.selection = QFileDialogPrivate::initialSelection(dir); args.filter = filter; args.mode = ExistingFile; args.options = options;#if defined(Q_WS_WIN) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { return qt_win_get_open_file_name(args, &(args.directory), selectedFilter); }#elif defined(Q_WS_MAC) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { QStringList files = qt_mac_get_open_file_names(args, &(args.directory), selectedFilter); if (!files.isEmpty()) return files.first().normalized(QString::NormalizationForm_C); return QString(); }#endif // create a qt dialog QFileDialog dialog(args); if (selectedFilter) dialog.selectFilter(*selectedFilter); if (dialog.exec() == QDialog::Accepted) { if (selectedFilter) *selectedFilter = dialog.selectedFilter(); return dialog.selectedFiles().value(0); } return QString();}/*! This is a convenience static function that will return one or more existing files selected by the user. \code QStringList files = QFileDialog::getOpenFileNames( this, "Select one or more files to open", "/home", "Images (*.png *.xpm *.jpg)"); \endcode This function creates a modal file dialog with the given \a parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget. The file dialog's working directory will be set to \a dir. If \a dir includes a file name, the file will be selected. The filter is set to \a filter so that only those files which match the filter are shown. The filter selected is set to \a selectedFilter. The parameters \a dir, \a selectedFilter and \a filter may be empty strings. The dialog's caption is set to \a caption. If \a caption is not specified then a default caption will be used. Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. On Mac OS X, the \a dir argument is ignored, the native dialog always displays the last visited directory. Note that 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 under the parent's title bar. Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, the file dialog will change to \c{/var/tmp} after entering \c{/usr/tmp}. The \a 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. Note that if you want to iterate over the list of files, you should iterate over a copy. For example: \code QStringList list = files; QStringList::Iterator it = list.begin(); while(it != list.end()) { myProcessing(*it); ++it; } \endcode \sa getOpenFileName(), getSaveFileName(), getExistingDirectory()*/QStringList QFileDialog::getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options){ if (qt_filedialog_open_filenames_hook) return qt_filedialog_open_filenames_hook(parent, caption, dir, filter, selectedFilter, options); QFileDialogArgs args; args.parent = parent; args.caption = caption; args.directory = QFileDialogPrivate::workingDirectory(dir); args.selection = QFileDialogPrivate::initialSelection(dir); args.filter = filter; args.mode = ExistingFiles; args.options = options;#if defined(Q_WS_WIN) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { return qt_win_get_open_file_names(args, &(args.directory), selectedFilter); }#elif defined(Q_WS_MAC) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { QStringList files = qt_mac_get_open_file_names(args, &(args.directory), selectedFilter); for (int i = 0; i < files.count(); ++i) files.replace(i, files.at(i).normalized(QString::NormalizationForm_C)); return files; }#endif // create a qt dialog QFileDialog dialog(args); if (selectedFilter) dialog.selectFilter(*selectedFilter); if (dialog.exec() == QDialog::Accepted) { if (selectedFilter) *selectedFilter = dialog.selectedFilter(); return dialog.selectedFiles(); } return QStringList();}/*! 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 \a parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget. \code QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "/home/jana/untitled.png", tr("Images (*.png *.xpm *.jpg)")); \endcode The file dialog's working directory will be set to \a dir. If \a dir includes a file name, the file will be selected. Only files that match the \a filter are shown. The filter selected is set to \a selectedFilter. The parameters \a dir, \a selectedFilter, and \a filter may be empty strings. The \a 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 \a selectedFilter to the desired value. The dialog's caption is set to \a caption. If \a caption is not specified then a default caption will be used. Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. Note that 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 under the parent's title bar. On Mac OS X, the filter argument is ignored. Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, the file dialog will change to \c{/var/tmp} after entering \c{/usr/tmp}. If \a options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories. \sa getOpenFileName(), getOpenFileNames(), getExistingDirectory()*/QString QFileDialog::getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options){ if (qt_filedialog_save_filename_hook) return qt_filedialog_save_filename_hook(parent, caption, dir, filter, selectedFilter, options); QFileDialogArgs args; args.parent = parent; args.caption = caption; args.directory = QFileDialogPrivate::workingDirectory(dir); args.selection = QFileDialogPrivate::initialSelection(dir); args.filter = filter; args.mode = AnyFile; args.options = options;#if defined(Q_WS_WIN) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { return qt_win_get_save_file_name(args, &(args.directory), selectedFilter); }#elif defined(Q_WS_MAC) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { QString result = qt_mac_get_save_file_name(args, &(args.directory), selectedFilter); return result.normalized(QString::NormalizationForm_C); }#endif // create a qt dialog QFileDialog dialog(args); dialog.setAcceptMode(AcceptSave); if (selectedFilter) dialog.selectFilter(*selectedFilter); if (dialog.exec() == QDialog::Accepted) { if (selectedFilter) *selectedFilter = dialog.selectedFilter(); return dialog.selectedFiles().value(0); } return QString();}/*! This is a convenience static function that will return an existing directory selected by the user. \code QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); \endcode This function creates a modal file dialog with the given \a parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget. The dialog's working directory is set to \a dir, and the caption is set to \a caption. Either of these may be an empty string in which case the current directory and a default caption will be used respectively. The \a 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. Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog. On Mac OS X, the \a dir argument is ignored, the native dialog always displays the last visited directory. Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if \c{/usr/tmp} is a symlink to \c{/var/tmp}, the file dialog will change to \c{/var/tmp} after entering \c{/usr/tmp}. If \a options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories. Note that 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 under the parent's title bar. \sa getOpenFileName(), getOpenFileNames(), getSaveFileName()*/QString QFileDialog::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, Options options){ if (qt_filedialog_existing_directory_hook) return qt_filedialog_existing_directory_hook(parent, caption, dir, options); QFileDialogArgs args; args.parent = parent; args.caption = caption; args.directory = QFileDialogPrivate::workingDirectory(dir); args.mode = (options & ShowDirsOnly ? DirectoryOnly : Directory); args.options = options;#if defined(Q_WS_WIN) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog) && (options & ShowDirsOnly)) { return qt_win_get_existing_directory(args); }#elif defined(Q_WS_MAC) if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) { QStringList files = qt_mac_get_open_file_names(args, 0, 0); if (!files.isEmpty()) return files.first().normalized(QString::NormalizationForm_C); return QString(); }#endif // create a qt dialog QFileDialog dialog(args); if (dialog.exec() == QDialog::Accepted) { QString result = dialog.selectedFiles().value(0); if (!result.isEmpty() && result.right(1) != QLatin1String("/")) result += QLatin1Char('/'); return result; } return QString();}/* Get the initial directory path \sa initialSelection() */QString QFileDialogPrivate::workingDirectory(const QString &path){ if (!path.isEmpty()) { QFileInfo info(path); if (info.exists() && info.isDir()) return info.absoluteFilePath(); return info.absolutePath(); } return QDir::currentPath();}/* Get the initial selection given a path. The initial directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -