📄 qfiledialog.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <qvariant.h>#include <private/qwidgetitemdata_p.h>#include "qfiledialog.h"#ifndef QT_NO_FILEDIALOG#include "qfiledialog_p.h"#include <qfontmetrics.h>#include <qaction.h>#include <qheaderview.h>#include <qshortcut.h>#include <qgridlayout.h>#include <qmenu.h>#include <qmessagebox.h>#include <qinputdialog.h>#include <stdlib.h>#include <qsettings.h>#include <qdebug.h>#include <qapplication.h>#include "ui_qfiledialog.h"/* \internal Exported hooks that can be used to customize the static functions. */typedef QString (*_qt_filedialog_existing_directory_hook)(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options);Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook = 0;typedef QString (*_qt_filedialog_open_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook = 0;typedef QStringList (*_qt_filedialog_open_filenames_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook = 0;typedef QString (*_qt_filedialog_save_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook = 0;/*! \class QFileDialog \brief The QFileDialog class provides a dialog that allow users to select files or directories. \ingroup dialogs \mainclass 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, these static functions will call the native Windows file dialog, and on Mac OS X these static function will call the native Mac OS X file dialog. \code fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)")); \endcode 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 \e this, and the window title is set to "Open Image". If you want to use multiple filters, separate each one with \e two semicolons. For example: \code "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" \endcode 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: \code QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile); \endcode In the above example, the mode of the file dialog is set to 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 if the user must select an existing file, or \l Directory if only a directory may be selected. See the \l QFileDialog::FileMode enum for the complete list of modes. The fileMode property contains the mode of operation for the dialog; this indicates what types of objects the user is expected to select. Use setFilter() to set the dialog's file filter. For example: \code dialog.setFilter(tr("Images (*.png *.xpm *.jpg)")); \endcode In the above example, the filter is set to \c{"Images (*.png *.xpm *.jpg)"}, this means that only files with the extension \c png, \c xpm, or \c jpg will be shown in the QFileDialog. You can apply several filters by using setFilters(). Use selectFilter() to select one of the filters you've given as the file dialog's default filter. The file dialog has two view modes: \l{QFileDialog::}{List} and \l{QFileDialog::}{Detail}. \l{QFileDialog::}{List} presents the contents of the current directory as a list of file and directory names. \l{QFileDialog::}{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(): \code dialog.setViewMode(QFileDialog::Detail); \endcode The last important function you will need to use when creating your own file dialog is selectedFiles(). \code QStringList fileNames; if (dialog.exec()) fileNames = dialog.selectedFiles(); \endcode In the above example, a modal file dialog is created and shown. If the user clicked OK, the file they selected is put in \c fileName. The dialog's working directory can be set with setDirectory(). Each file in the current directory can be selected using the selectFile() function. The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use QFileDialog as well as other built-in Qt dialogs. \sa QDir, QFileInfo, QFile, QPrintDialog, QColorDialog, QFontDialog, {Standard Dialogs Example}, {Application Example}*//*! \enum QFileDialog::AcceptMode \value AcceptOpen \value AcceptSave*//*! \enum QFileDialog::ViewMode This enum describes the view mode of the file dialog; i.e. what information about each file will be displayed. \value Detail Displays an icon, a name, and details for each item in the directory. \value List Displays only an icon and a name for each item in the directory. \sa setViewMode()*//*! \enum QFileDialog::FileMode 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. \value AnyFile The name of a file, whether it exists or not. \value ExistingFile The name of a single existing file. \value Directory The name of a directory. Both files and directories are displayed. \value DirectoryOnly The name of a directory. The file dialog will only display directories. \value ExistingFiles The names of zero or more existing files. \sa setFileMode()*//*! \enum QFileDialog::Option \value ShowDirsOnly Only show directories in the file dialog. By default both files and directories are shown. \value DontResolveSymlinks Don't resolve symlinks in the file dialog. By default symlinks are resolved. \value DontConfirmOverwrite Don't ask for confirmation if an existing file is selected. By default confirmation is requested. \value DontUseSheet Don't make the native file dialog a sheet. By default on Mac OS X, the native file dialog is made a sheet if it has a parent that can take a sheet. \value DontUseNativeDialog Don't use the native file dialog. By default on Mac OS X and Windows, the native file dialog is used.*//*! \enum QFileDialog::DialogLabel \value LookIn \value FileName \value FileType \value Accept \value Reject*//*! \fn void QFileDialog::filesSelected(const QStringList &selected) When the selection changes, this signal is emitted with the (possibly empty) list of \a selected files. \sa currentChanged()*//*! \fn void QFileDialog::currentChanged(const QString &path) When the current file changes, this signal is emitted with the new file name as the \a path parameter. \sa filesSelected()*//*! \fn void QFileDialog::directoryEntered(const QString &directory) \since 4.3 This signal is emitted when the user enters a \a directory.*//*! \fn void QFileDialog::filterSelected(const QString &filter) \since 4.3 This signal is emitted when the user selects a \a filter.*/#if defined(Q_WS_WIN) || defined(Q_WS_MAC)bool Q_GUI_EXPORT qt_use_native_dialogs = true; // for the benefit of testing tools, until we have a proper API#endif#ifdef Q_WS_WIN#include <qwindowsstyle.h>#endif#include <qshortcut.h>#ifdef Q_WS_MAC#include <private/qunicodetables_p.h>#include <qmacstyle_mac.h>#endif/*! \fn QFileDialog::QFileDialog(QWidget *parent, Qt::WindowFlags flags) Constructs a file dialog with the given \a parent and widget \a flags.*/QFileDialog::QFileDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(*new QFileDialogPrivate, parent, f){ Q_D(QFileDialog); d->init();}/*! Constructs a file dialog with the given \a parent and \a caption that initially displays the contents of the specified \a directory. The contents of the directory are filtered before being shown in the dialog, using a semicolon-separated list of filters specified by \a filter.*/QFileDialog::QFileDialog(QWidget *parent, const QString &caption, const QString &directory, const QString &filter) : QDialog(*new QFileDialogPrivate, parent, 0){ Q_D(QFileDialog); d->init(directory, filter, caption);}/*! \internal*/QFileDialog::QFileDialog(const QFileDialogArgs &args) : QDialog(*new QFileDialogPrivate, args.parent, 0){ Q_D(QFileDialog); d->init(args.directory, args.filter, args.caption); setFileMode(args.mode); setConfirmOverwrite(!(args.options & DontConfirmOverwrite)); setResolveSymlinks(!(args.options & DontResolveSymlinks)); selectFile(args.selection); d->lineEdit()->selectAll();}/*! Destroys the file dialog.*/QFileDialog::~QFileDialog(){ Q_D(QFileDialog);#ifndef QT_NO_SETTINGS QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("Qt")); settings.setValue(QLatin1String("filedialog"), saveState());#endif delete d->qFileDialogUi;}/*! \since 4.3 Sets the \a urls that are located in the sidebar*/void QFileDialog::setSidebarUrls(const QList<QUrl> &urls){ Q_D(QFileDialog); d->qFileDialogUi->sidebar->setUrls(urls);}/*! \since 4.3 Returns a list of urls that are currently in the sidebar*/QList<QUrl> QFileDialog::sidebarUrls() const{ Q_D(const QFileDialog); return d->qFileDialogUi->sidebar->urls();}static const qint32 QFileDialogMagic = 0xbe;/*! \since 4.3 Saves the state of the dialog's layout, history and current directory. Typically this is used in conjunction with QSettings to remember the size for a future session. A version number is stored as part of the data.*/QByteArray QFileDialog::saveState() const{ Q_D(const QFileDialog); int version = 3; QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream << qint32(QFileDialogMagic); stream << qint32(version); stream << d->qFileDialogUi->splitter->saveState(); stream << d->qFileDialogUi->sidebar->urls(); stream << history(); stream << directory().absolutePath(); stream << d->qFileDialogUi->treeView->header()->saveState(); stream << qint32(viewMode()); return data;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -