📄 qfiledialog.3qt
字号:
.fi.PPIn the above snippet, we create a preview widget which inherits from QLabel and QFilePreview. File preview widgets \fImust\fR inherit from QFilePreview..PPInside the class we reimplement QFilePreview::previewUrl(), this is where we determine what happens when a file is selected. In the above example we only show a preview of the file if it is a valid pixmap. Here's how to make a file dialog use a preview widget:.PP.nf.br Preview* p = new Preview;.br.br QFileDialog* fd = new QFileDialog( this );.br fd->setContentsPreviewEnabled( TRUE );.br fd->setContentsPreview( p, p );.br fd->setPreviewMode( QFileDialog::Contents );.br fd->show();.br.fi.PPThe first line creates an instance of our preview widget. We then create our file dialog and call setContentsPreviewEnabled( TRUE ), this tell the file dialog to preview the contents of the currently selected file. We then call setContentsPreview() -- note that we pass the same preview widget twice. Finally, before showing the file dialog, we call setPreviewMode() setting the mode to \fIContents\fR which will show the contents preview of the file that the user has selected..PPIf you create another preview widget that is used for displaying information about a file, create it in the same way as the contents preview widget and call setInfoPreviewEnabled(), and setInfoPreview(). Then the user will be able to switch between the two preview modes..PPFor more information about creating a QFilePreview widget see QFilePreview..PP.ce 1.B "[Image Omitted]".PP.ce 1.B "[Image Omitted]".PPSee also Dialog Classes..SS "Member Type Documentation".SH "QFileDialog::Mode"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..TP\fCQFileDialog::AnyFile\fR - The name of a file, whether it exists or not..TP\fCQFileDialog::ExistingFile\fR - The name of a single existing file..TP\fCQFileDialog::Directory\fR - The name of a directory. Both files and directories are displayed..TP\fCQFileDialog::DirectoryOnly\fR - The name of a directory. The file dialog will only display directories..TP\fCQFileDialog::ExistingFiles\fR - The names of zero or more existing files..PPSee setMode()..SH "QFileDialog::PreviewMode"This enum describes the preview mode of the file dialog..TP\fCQFileDialog::NoPreview\fR - No preview is shown at all..TP\fCQFileDialog::Contents\fR - Show a preview of the contents of the current file using the contents preview widget..TP\fCQFileDialog::Info\fR - Show information about the current file using the info preview widget..PPSee setPreviewMode(), setContentsPreview() and setInfoPreview()..SH "QFileDialog::ViewMode"This enum describes the view mode of the file dialog, i.e. what information about each file will be displayed..TP\fCQFileDialog::List\fR - Display file and directory names with icons..TP\fCQFileDialog::Detail\fR - Display file and directory names with icons plus additional information, such as file size and modification date..PPSee setViewMode()..SH MEMBER FUNCTION DOCUMENTATION.SH "QFileDialog::QFileDialog ( const QString & dirName, const QString & filter = QString::null, QWidget * parent = 0, const char * name = 0, bool modal = FALSE )"Constructs a file dialog called \fIname\fR with the parent, \fIparent\fR. If \fImodal\fR is TRUE then the file dialog is modal; otherwise it is modeless..PPIf \fIdirName\fR is specified then it will be used as the dialog's working directory, i.e. it will be the directory that is shown when the dialog appears. If \fIfilter\fR is specified it will be used as the dialog's file filter..SH "QFileDialog::QFileDialog ( QWidget * parent = 0, const char * name = 0, bool modal = FALSE )"Constructs a file dialog called \fIname\fR, with the parent, \fIparent\fR. If \fImodal\fR is TRUE then the file dialog is modal; otherwise it is modeless..SH "QFileDialog::~QFileDialog ()"Destroys the file dialog..SH "void QFileDialog::addFilter ( const QString & filter )"Adds the filter \fIfilter\fR to the list of filters and makes it the current filter..PP.nf.br QFileDialog* fd = new QFileDialog( this );.br fd->addFilter( "Images (*.png *.jpg *.xpm)" );.br fd->show();.br.fi.PPIn the above example, a file dialog is created, and the file filter "Images (*.png *.jpg *.xpm)" is added and is set as the current filter. The original filter, "All Files (*)", is still available..PPSee also setFilter() and setFilters()..SH "void QFileDialog::addLeftWidget ( QWidget * w )\fC [protected]\fR"Adds the widget \fIw\fR to the left-hand side of the file dialog..PPSee also addRightWidget(), addWidgets(), and addToolButton()..SH "void QFileDialog::addRightWidget ( QWidget * w )\fC [protected]\fR"Adds the widget \fIw\fR to the right-hand side of the file dialog..PPSee also addLeftWidget(), addWidgets(), and addToolButton()..SH "void QFileDialog::addToolButton ( QButton * b, bool separator = FALSE )\fC [protected]\fR"Adds the tool button \fIb\fR to the row of tool buttons at the top of the file dialog. The button is appended to the right of this row. If \fIseparator\fR is TRUE, a small space is inserted between the last button of the row and the new button \fIb\fR..PPSee also addWidgets(), addLeftWidget(), and addRightWidget()..SH "void QFileDialog::addWidgets ( QLabel * l, QWidget * w, QPushButton * b )\fC [protected]\fR"Adds the specified widgets to the bottom of the file dialog. The label \fIl\fR is placed underneath the "file name" and the "file types" labels. The widget \fIw\fR is placed underneath the file types combobox. The button \fIb\fR is placed underneath the Cancel pushbutton..PP.nf.br MyFileDialog::MyFileDialog( QWidget* parent, const char* name ) :.br QFileDialog( parent, name ).br {.br QLabel* label = new QLabel( "Added widgets", this );.br QLineEdit* lineedit = new QLineEdit( this );.br QToolButton* toolbutton = new QToolButton( this );.br.br addWidgets( label, lineedit, toolbutton );.br }.br.fi.PPIf you don't want to have one of the widgets added, pass 0 in that widget's position..PPEvery time you call this function, a new row of widgets will be added to the bottom of the file dialog..PPSee also addToolButton(), addLeftWidget(), and addRightWidget()..SH "const QDir * QFileDialog::dir () const"Returns the current directory shown in the file dialog..PPThe ownership of the QDir pointer is transferred to the caller, so it must be deleted by the caller when no longer required..PPSee also setDir()..SH "void QFileDialog::dirEntered ( const QString & )\fC [signal]\fR"This signal is emitted when the user enters a directory..PPSee also dir()..SH "QString QFileDialog::dirPath () const"Returns the file dialog's working directory. See the "dirPath" property for details..SH "void QFileDialog::fileHighlighted ( const QString & )\fC [signal]\fR"This signal is emitted when the user highlights a file..PPSee also fileSelected() and filesSelected()..SH "void QFileDialog::fileSelected ( const QString & )\fC [signal]\fR"This signal is emitted when the user selects a file..PPSee also filesSelected(), fileHighlighted(), and selectedFile..SH "void QFileDialog::filesSelected ( const QStringList & )\fC [signal]\fR"This signal is emitted when the user selects one or more files in \fIExistingFiles\fR mode..PPSee also fileSelected(), fileHighlighted(), and selectedFiles..SH "void QFileDialog::filterSelected ( const QString & )\fC [signal]\fR"This signal is emitted when the user selects a filter..PPSee also selectedFilter..SH "QString QFileDialog::getExistingDirectory ( const QString & dir = QString::null, QWidget * parent = 0, const char * name = 0, const QString & caption = QString::null, bool dirOnly = TRUE, bool resolveSymlinks = TRUE )\fC [static]\fR"This is a convenience static function that will return an existing directory selected by the user..PP.nf.br QString s = QFileDialog::getExistingDirectory(.br "/home",.br this,.br "get existing directory".br "Choose a directory",.br TRUE );.br.fi.PPThis function creates a modal file dialog called \fIname\fR, with parent, \fIparent\fR. If parent is not 0, the dialog will be shown centered over the parent..PPThe dialog's working directory is set to \fIdir\fR, and the caption is set to \fIcaption\fR. Either of these may be QString::null in which case the current directory and a default caption will be used respectively..PPIf \fIdirOnly\fR is TRUE, then only directories will be shown in the file dialog; otherwise both directories and files will be shown..PPUnder Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories..PPNote that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers..PPSee also getOpenFileName(), getOpenFileNames(), and getSaveFileName()..SH "QString QFileDialog::getOpenFileName ( const QString & startWith = QString::null, const QString & filter = QString::null, QWidget * parent = 0, const char * name = 0, const QString & caption = QString::null, QString * selectedFilter = 0, bool resolveSymlinks = TRUE )\fC [static]\fR"This is a convenience static function that returns an existing file selected by the user. If the user pressed Cancel, it returns a null string..PP.nf.br QString s = QFileDialog::getOpenFileName(.br "/home",.br "Images (*.png *.xpm *.jpg)",.br this,.br "open file dialog",.br "Choose a file to open" );.br.fi.PPThe function creates a modal file dialog called \fIname\fR, with parent, \fIparent\fR. If a parent is not 0, the dialog will be shown centered over the parent..PPThe file dialog's working directory will be set to \fIstartWith\fR. If \fIstartWith\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIstartWith\fR, \fIselectedFilter\fR and \fIfilter\fR may be QString::null..PPThe dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used..PPUnder Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog, unless the style of the application is set to something other than the native style (Note that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers)..PPUnder Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories..PPSee also getOpenFileNames(), getSaveFileName(), and getExistingDirectory()..PPExamples:.)l action/application.cpp, addressbook/mainwindow.cpp, application/application.cpp, chart/chartform.cpp, network/ftpclient/ftpmainwindow.ui.h, qwerty/qwerty.cpp, and showimg/showimg.cpp..SH "QStringList QFileDialog::getOpenFileNames ( const QString & filter = QString::null, const QString & dir = QString::null, QWidget * parent = 0, const char * name = 0, const QString & caption = QString::null, QString * selectedFilter = 0, bool resolveSymlinks = TRUE )\fC [static]\fR"This is a convenience static function that will return one or more existing files selected by the user..PP.nf.br QStringList files = QFileDialog::getOpenFileNames(.br "Images (*.png *.xpm *.jpg)",.br "/home",.br this,.br "open files dialog".br "Select one or more files to open" );.br.fi.PPThis function creates a modal file dialog called \fIname\fR, with parent \fIparent\fR. If \fIparent\fR is not 0, the dialog will be shown centered over the parent..PPThe file dialog's working directory will be set to \fIdir\fR. If \fIdir\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIdir\fR, \fIselectedFilter\fR and \fIfilter\fR may be QString::null..PPThe dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used..PPUnder Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog, unless the style of the application is set to something other than the native style. (Note that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers)..PPUnder Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories..PPNote that if you want to iterate over the list of files, you should iterate over a copy, e.g..PP.nf.br QStringList list = files;.br QStringList::Iterator it = list.begin();.br while( it != list.end() ) {.br myProcessing( *it );.br ++it;.br }.br.fi.PPSee also getOpenFileName(), getSaveFileName(), and getExistingDirectory()..SH "QString QFileDialog::getSaveFileName ( const QString & startWith = QString::null, const QString & filter = QString::null, QWidget * parent = 0, const char * name = 0, const QString & caption = QString::null, QString * selectedFilter = 0, bool resolveSymlinks = TRUE )\fC [static]\fR"This is a convenience static function that will return a file name selected by the user. The file does not have to exist..PPIt creates a modal file dialog called \fIname\fR, with parent, \fIparent\fR. If a parent is not 0, the dialog will be shown centered over the parent..PP.nf.br QString s = QFileDialog::getSaveFileName(.br "/home",.br "Images (*.png *.xpm *.jpg)",.br this,.br "save file dialog".br "Choose a filename to save under" );.br.fi.PPThe file dialog's working directory will be set to \fIstartWith\fR. If \fIstartWith\fR includes a file name, the file will be selected. The filter is set to \fIfilter\fR so that only those files which match the filter are shown. The filter selected is set to \fIselectedFilter\fR. The parameters \fIstartWith\fR, \fIselectedFilter\fR and \fIfilter\fR may be QString::null..PPThe dialog's caption is set to \fIcaption\fR. If \fIcaption\fR is not specified then a default caption will be used..PPUnder Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog, unless the style of the application is set to something other than the native style. (Note that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers)..PPUnder Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If \fIresolveSymlinks\fR is FALSE, the file dialog will treat symlinks as regular directories..PPSee also getOpenFileName(), getOpenFileNames(), and getExistingDirectory()..PPExamples:.)l action/application.cpp, addressbook/mainwindow.cpp, application/application.cpp, network/ftpclient/ftpmainwindow.ui.h, qmag/qmag.cpp, qwerty/qwerty.cpp, and showimg/showimg.cpp..SH "QFileIconProvider * QFileDialog::iconProvider ()\fC [static]\fR"Returns a pointer to the icon provider currently set on the file dialog. By default there is no icon provider, and this function returns 0..PPSee also setIconProvider() and QFileIconProvider..SH "bool QFileDialog::isContentsPreviewEnabled () const"Returns TRUE if the file dialog can provide a contents preview of the currently selected file; otherwise returns FALSE. See the "contentsPreview" property for details..SH "bool QFileDialog::isInfoPreviewEnabled () const"Returns TRUE if the file dialog can provide preview information about the currently selected file; otherwise returns FALSE. See the "infoPreview" property for details..SH "Mode QFileDialog::mode () const"Returns the file dialog's mode. See the "mode" property for details..SH "PreviewMode QFileDialog::previewMode () const"Returns the preview mode for the file dialog. See the "previewMode" property for details..SH "void QFileDialog::rereadDir ()"Rereads the current directory shown in the file dialog..PPThe only time you will need to call this function is if the contents of the directory change and you wish to refresh the file dialog to reflect the change..PPSee also resortDir()..SH "void QFileDialog::resortDir ()"Re-sorts the displayed directory..PPSee also rereadDir()..SH "void QFileDialog::selectAll ( bool b )"If \fIb\fR is TRUE then all the files in the current directory are selected; otherwise, they are deselected..SH "QString QFileDialog::selectedFile () const"Returns the name of the selected file. See the "selectedFile" property for details..SH "QStringList QFileDialog::selectedFiles () const"Returns the list of selected files. See the "selectedFiles" property for details..SH "QString QFileDialog::selectedFilter () const"Returns the filter which the user has selected in the file dialog. See the "selectedFilter" property for details..SH "void QFileDialog::setContentsPreview ( QWidget * w, QFilePreview * preview )"Sets the widget to be used for displaying the contents of the file to the widget \fIw\fR and a preview of those contents to the QFilePreview \fIpreview\fR..PPNormally you would create a preview widget that derives from both QWidget and QFilePreview, so you should pass the same widget twice.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -