The Q3FileDialog class provides dialogs that allow users to select files or directories. More...
#include <Q3FileDialog>
This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.
The Q3FileDialog class provides dialogs that allow users to select files or directories.
The Q3FileDialog class enables a user to traverse their file system in order to select one or many files or a directory.
The easiest way to create a Q3FileDialog 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.
QString s = Q3FileDialog::getOpenFileName(
"/home",
"Images (*.png *.xpm *.jpg)",
this,
"open file dialog",
"Choose a file");
In the above example, a modal Q3FileDialog is created using a static function. The startup directory is set to "/home". The file filter is set to "Images (*.png *.xpm *.jpg)". The parent of the file dialog is set to this and it is given the identification name - "open file dialog". The caption at the top of file dialog is set to "Choose a file". If you want to use multiple filters, separate each one with two semicolons, e.g.
You can create your own Q3FileDialog without using the static functions. By calling setMode(), you can set what can be returned by the Q3FileDialog.
Q3FileDialog* fd = new Q3FileDialog(this, "file dialog", true);
fd->setMode(Q3FileDialog::AnyFile);
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 "File Save As" file dialog. Use ExistingFile if the user must select an existing file or Directory if only a directory may be selected. (See the Q3FileDialog::Mode enum for the complete list of modes.)
You can retrieve the dialog's mode with mode(). Use setFilter() to set the dialog's file filter, e.g.
fd->setFilter("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 or jpg will be shown in the Q3FileDialog. You can apply several filters by using setFilters() and add additional filters with addFilter(). Use setSelectedFilter() to select one of the filters you've given as the file dialog's default filter. Whenever the user changes the filter the filterSelected() signal is emitted.
The file dialog has two view modes, Q3FileDialog::List which simply lists file and directory names and Q3FileDialog::Detail which displays additional information alongside each name, e.g. file size, modification date, etc. Set the mode with setViewMode().
fd->setViewMode(Q3FileDialog::Detail);
The last important function you will need to use when creating your own file dialog is selectedFile().
QString fileName;
if (fd->exec() == QDialog::Accepted)
fileName = fd->selectedFile();
In the above example, a modal file dialog is created and shown. If the user clicked OK, then the file they selected is put in fileName.
The dialog's working directory can be set with setDir(). The display of hidden files is controlled with setShowHiddenFiles(). The dialog can be forced to re-read the directory with rereadDir() and re-sort the directory with resortDir(). All the files in the current directory can be selected with selectAll().
Creating and using preview widgets
There are two kinds of preview widgets that can be used with Q3FileDialogs: content preview widgets and information preview widgets. They are created and used in the same way except that the function names differ, e.g. setContentsPreview() and setInfoPreview().
A preview widget is a widget that is placed inside a Q3FileDialog so that the user can see either the contents of the file, or information about the file.
class Preview : public QLabel, public Q3FilePreview
{
public:
Preview(QWidget *parent=0) : QLabel(parent) {}
void previewUrl(const Q3Url &u)
{
QString path = u.path();
QPixmap pix(path);
if (pix.isNull())
setText("This is not a pixmap");
else
setPixmap(pix);
}
};
In the above snippet, we create a preview widget which inherits from QLabel and Q3FilePreview. File preview widgets must inherit from Q3FilePreview.
Inside the class we reimplement Q3FilePreview::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:
Preview* p = new Preview;
Q3FileDialog* fd = new Q3FileDialog(this);
fd->setContentsPreviewEnabled(true);
fd->setContentsPreview(p, p);
fd->setPreviewMode(Q3FileDialog::Contents);
fd->show();
The 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 Contents which will show the contents preview of the file that the user has selected.
If 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.
If one or more files are selected, selectedFiles contains their names including their absolute paths. If no files are selected or the mode isn't ExistingFiles selectedFiles is an empty list.
It is more convenient to use selectedFile() if the mode is ExistingFile, Directory or DirectoryOnly.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = myFileDialog.selectedFiles();
QStringList::Iterator it = list.begin();
while(it != list.end()) {
myProcessing(*it);
++it;
}
If you set the view mode to be Detail (the default), then you will see the file's details, such as the size of the file and the date the file was last modified in addition to the file's name.
If you set the view mode to be List, then you will just see a list of the files and folders.
Constructs a file dialog called name with the parent, parent. If modal is true then the file dialog is modal; otherwise it is modeless.
If dirName 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 filter is specified it will be used as the dialog's file filter.
Adds the filter filter to the list of filters and makes it the current filter.
Q3FileDialog* fd = new Q3FileDialog(this);
fd->addFilter("Images (*.png *.jpg *.xpm)");
fd->show();
In 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.
Adds the tool button b to the row of tool buttons at the top of the file dialog. The button is appended to the right of this row. If separator is true, a small space is inserted between the last button of the row and the new button b.
Adds the specified widgets to the bottom of the file dialog. The label l is placed underneath the "file name" and the "file types" labels. The widget w is placed underneath the file types combobox. The button b is placed underneath the Cancel push button.
MyFileDialog::MyFileDialog(QWidget* parent, const char* name) :
Q3FileDialog(parent, name)
{
QLabel* label = new QLabel("Added widgets", this);
QLineEdit* lineedit = new QLineEdit(this);
QPushButton* pushbutton = new QPushButton(this);
addWidgets(label, lineedit, pushbutton);
}
If you don't want to have one of the widgets added, pass 0 in that widget's position.
Every time you call this function, a new row of widgets will be added to the bottom of the file dialog.
This is a convenience static function that will return an existing directory selected by the user.
QString s = Q3FileDialog::getExistingDirectory(
"/home",
this,
"get existing directory",
"Choose a directory",
true);
This function creates a modal file dialog called name, with parent, parent. If parent is not 0, the dialog will be shown centered over the parent.
The dialog's working directory is set to dir, and the 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.
If dirOnly is true, then only directories will be shown in the file dialog; otherwise both directories and files will be shown.
Under 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 resolveSymlinks is false, 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.
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.
QString s = Q3FileDialog::getOpenFileName(
"/home",
"Images (*.png *.xpm *.jpg)",
this,
"open file dialog",
"Choose a file to open");
The function creates a modal file dialog called name, with parent, parent. If a parent is not 0, the dialog will be shown centered over the parent.
The file dialog's working directory will be set to startWith. If startWith 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 startWith, selectedFilter and filter may be an empty string.
The dialog's caption is set to caption. If 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 Q3FileDialog, 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 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 /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If resolveSymlinks is false, the file dialog will treat symlinks as regular directories.
This is a convenience static function that will return one or more existing files selected by the user.
QStringList files = Q3FileDialog::getOpenFileNames(
"Images (*.png *.xpm *.jpg)",
"/home",
this,
"open files dialog",
"Select one or more files to open");
This function creates a modal file dialog called name, with parent parent. If parent is not 0, the dialog will be shown centered over the parent.
The file dialog's working directory will be set to dir. If 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.
The dialog's caption is set to caption. If 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 Q3FileDialog, 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 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 /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If resolveSymlinks is false, the file dialog will treat symlinks as regular directories.
Note that if you want to iterate over the list of files, you should iterate over a copy, e.g.
QStringList list = files;
QStringList::Iterator it = list.begin();
while(it != list.end()) {
myProcessing(*it);
++it;
}
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 called name, with parent, parent. If a parent is not 0, the dialog will be shown centered over the parent.
QString s = Q3FileDialog::getSaveFileName(
"/home",
"Images (*.png *.xpm *.jpg)",
this,
"save file dialog",
"Choose a filename to save under");
The file dialog's working directory will be set to startWith. If startWith 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 startWith, selectedFilter and filter may be an empty string.
The dialog's caption is set to caption. If 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 Q3FileDialog, 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 and if parent is not 0 then it will position the dialog just under the parent's title bar. And on the Mac the filter argument is ignored).
Under 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 resolveSymlinks is false, the file dialog will treat symlinks as regular directories.
Rereads the current directory shown in the file dialog.
The 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.
Sets the filter used in the file dialog to newFilter.
If newFilter contains a pair of parentheses containing one or more of "anything*something" separated by spaces or by semicolons then only the text contained in the parentheses is used as the filter. This means that these calls are all equivalent:
fd->setFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)");
fd->setFilter("*.cpp *.cc *.C *.cxx *.c++");
fd->setFilter("All C++ files (*.cpp;*.cc;*.C;*.cxx;*.c++)");
fd->setFilter("*.cpp;*.cc;*.C;*.cxx;*.c++");
Sets the widget to be used for displaying information about the file to the widget w and a preview of that information to the Q3FilePreviewpreview.
Normally you would create a preview widget that derives from both QWidget and Q3FilePreview, so you should pass the same widget twice.
class Preview : public QLabel, public Q3FilePreview
{
public:
Preview(QWidget *parent=0) : QLabel(parent) {}
void previewUrl(const Q3Url &u)
{
QString path = u.path();
QPixmap pix(path);
if (pix.isNull())
setText("This is not a pixmap");
else
setText("This is a pixmap");
}
};
//...
int main(int argc, char** argv)
{
Preview* p = new Preview;
Q3FileDialog* fd = new Q3FileDialog(this);
fd->setInfoPreviewEnabled(true);
fd->setInfoPreview(p, p);
fd->setPreviewMode(Q3FileDialog::Info);
fd->show();
}
Sets the default selection to filename. If filename is absolute, setDir() is also called to set the file dialog's working directory to the filename's directory.
Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.
Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).
Qt dans le magazine
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter
ou par MP !