QFileDialog(3qt)
π QFileDialog π β the QFileDialog(3qt) manual, but make it make sense
okay bestie, sit down, grab a boba, because today we're learning about QFileDialog β the class that gives you dialogs so your users can go pick files or directories like the little main characters they are. ποΈβ¨
β¨ NAME β¨
QFileDialog β dialogs that let your users pick files or directories. that's it, that's the vibe. π
πΈ SYNOPSIS πΈ
#include <qfiledialog.h>
Inherits QDialog.
π Public Members π
QFileDialog ( const QString & dirName, const QString & filter =
QString::null, QWidget * parent = 0, const char * name = 0, bool
modal = FALSE )
QFileDialog ( QWidget * parent = 0, const char * name = 0, bool modal =
FALSE )
~QFileDialog ()
QString selectedFile () const
QString selectedFilter () const
virtual void setSelectedFilter ( const QString & mask )
virtual void setSelectedFilter ( int n )
void setSelection ( const QString & filename )
void selectAll ( bool b )
QStringList selectedFiles () const
QString dirPath () const
void setDir ( const QDir & dir )
const QDir * dir () const
void setShowHiddenFiles ( bool s )
bool showHiddenFiles () const
void rereadDir ()
void resortDir ()
enum Mode { AnyFile, ExistingFile, Directory, ExistingFiles,
DirectoryOnly }
void setMode ( Mode )
Mode mode () const
enum ViewMode { Detail, List }
enum PreviewMode { NoPreview, Contents, Info }
void setViewMode ( ViewMode m )
ViewMode viewMode () const
void setPreviewMode ( PreviewMode m )
PreviewMode previewMode () const
bool isInfoPreviewEnabled () const
bool isContentsPreviewEnabled () const
void setInfoPreviewEnabled ( bool )
void setContentsPreviewEnabled ( bool )
void setInfoPreview ( QWidget * w, QFilePreview * preview )
void setContentsPreview ( QWidget * w, QFilePreview * preview )
QUrl url () const
void addFilter ( const QString & filter )
π Public Slots π
void setDir ( const QString & pathstr )
void setUrl ( const QUrlOperator & url )
void setFilter ( const QString & newFilter )
void setFilters ( const QString & filters )
void setFilters ( const char ** types )
void setFilters ( const QStringList & )
π Signals π
void fileHighlighted ( const QString & )
void fileSelected ( const QString & )
void filesSelected ( const QStringList & )
void dirEntered ( const QString & )
void filterSelected ( const QString & )
π Static Public Members π
QString 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 )
QString 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 )
QString getExistingDirectory ( const QString & dir = QString::null,
QWidget * parent = 0, const char * name = 0, const QString & caption
= QString::null, bool dirOnly = TRUE, bool resolveSymlinks = TRUE )
QStringList 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 )
void setIconProvider ( QFileIconProvider * provider )
QFileIconProvider * iconProvider ()
π Properties π
bool contentsPreviewβ whether the file dialog can show a contents preview of whatever file is currently selected. yes queen, live previews. πQString dirPathβ the file dialog's working directory. (read only, so don't even try it)bool infoPreviewβ whether the file dialog can show preview information about the file that's selected rn.Mode modeβ the file dialog's mode.PreviewMode previewModeβ the preview mode for the file dialog.QString selectedFileβ the name of the selected file. (read only)QStringList selectedFilesβ the whole list of selected files. (read only)QString selectedFilterβ the filter the user picked inside the file dialog. (read only)bool showHiddenFilesβ whether hidden files are shown in the file dialog. spooky season toggle π«£ViewMode viewModeβ the file dialog's view mode.
π Protected Members π
void addWidgets ( QLabel * l, QWidget * w, QPushButton * b )
void addToolButton ( QButton * b, bool separator = FALSE )
void addLeftWidget ( QWidget * w )
void addRightWidget ( QWidget * w )
π DESCRIPTION π
So. The QFileDialog class gives you dialogs that let users select files or directories. iconic behavior honestly.
The QFileDialog class lets a user traverse their file system so they can pick one file, many files, or a whole directory. wander around, explore, choose your destiny. π§
The easiest way to make a QFileDialog is to use the static functions. And lowkey important: on Windows, those static functions will call the native Windows file dialog, and on Mac OS X, they'll call the native Mac OS X file dialog. so it just fits in wherever you are. πͺπ
QString s = QFileDialog::getOpenFileName(
"/home",
"Images (*.png *.xpm *.jpg)",
this,
"open file dialog",
"Choose a file" );
In that example up there, a modal QFileDialog gets 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 gets the identification name β "open file dialog". The caption at the top of the file dialog is set to "Choose a file". β¨
Now, if you want multiple filters, separate each one with two semi-colons, like:
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
You can also build your own QFileDialog without the static functions at all. By calling setMode(), you decide what the QFileDialog is allowed to return.
QFileDialog* fd = new QFileDialog( this, "file dialog", TRUE );
fd->setMode( QFileDialog::AnyFile );
In that example, the mode of the file dialog is set to AnyFile, meaning the user can select any file, or even name a file that doesn't exist yet. This mode is literally made for a "File Save As" dialog. Use ExistingFile if the user has to pick a file that actually exists, or Directory if only a directory may be selected. (Peep the QFileDialog::Mode enum for the complete list of modes, bestie β it's coming in a later part.)
You can grab the dialog's mode with mode(). Use setFilter() to set the dialog's file filter, like:
fd->setFilter( "Images (*.png *.xpm *.jpg)" );
In that example, the filter is set to "Images (*.png *.xpm *.jpg)", which means only files with the extension png, xpm or jpg will show up in the QFileDialog. Want several filters? Use setFilters(), and add extras with addFilter(). Use setSelectedFilter() to pick one of the filters you gave as the file dialog's default filter. And whenever the user switches filters, the filterSelected() signal fires. π£
The file dialog has two view modes: QFileDialog::List, which just lists file and directory names, and QFileDialog::Detail, which shows extra info next to each name β file size, modification date, etc. Set it with setViewMode().
fd->setViewMode( QFileDialog::Detail );
The last big function you'll need when you build your own file dialog is selectedFile().
QString fileName;
if ( fd->exec() == QDialog::Accepted )
fileName = fd->selectedFile();
In that example, a modal file dialog is created and shown. If the user clicked OK, then the file they selected gets dropped into fileName. slay. π
If you're using the ExistingFiles mode then you'll need selectedFiles() instead, which returns the selected files in a QStringList.
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(). And all the files in the current directory can be selected with selectAll(). π
π¦ Creating and using preview widgets π¦
There are two kinds of preview widgets you can use with QFileDialogs: content preview widgets and information preview widgets. They get created and used the same way, except the function names differ, e.g. setContentsPreview() and setInfoPreview().
A preview widget is a widget you place inside a QFileDialog so the user can see either the contents of the file, or information about the file. π
class Preview : public QLabel, public QFilePreview
{
public:
Preview( QWidget *parent=0 ) : QLabel( parent ) {}
void previewUrl( const QUrl &u )
{
QString path = u.path();
QPixmap pix( path );
if ( pix.isNull() )
setText( "This is not a pixmap" );
else
setPixmap( pix );
}
};
In that snippet we create a preview widget that inherits from QLabel and QFilePreview. Heads up: file preview widgets must inherit from QFilePreview. no exceptions, bestie.
Inside the class we reimplement QFilePreview::previewUrl(), and that's where we decide what happens when a file gets selected. In the example above we only show a preview of the file if it's a valid pixmap. Here's how to make a file dialog actually use a preview widget:
Preview* p = new Preview;
QFileDialog* fd = new QFileDialog( this );
fd->setContentsPreviewEnabled( TRUE );
fd->setContentsPreview( p, p );
fd->setPreviewMode( QFileDialog::Contents );
fd->show();
The first line creates an instance of our preview widget. Then we create our file dialog and call setContentsPreviewEnabled( TRUE ) β that tells the file dialog to preview the contents of the currently selected file. Then we call setContentsPreview() β and yes, notice that we pass the same preview widget twice. π«Ά Finally, before showing the file dialog, we call setPreviewMode() and set the mode to Contents, which will show the contents preview of the file the user selected.
If you make another preview widget for displaying information about a file, create it the exact same way as the contents preview widget, then call setInfoPreviewEnabled() and setInfoPreview(). After that the user can switch between the two preview modes. π
For more info about creating a QFilePreview widget, see QFilePreview.
[Image Omitted]
[Image Omitted]
See also Dialog Classes.
β Member Type Documentation β
π QFileDialog::Mode π
okay bestie so this enum is literally just telling the dialog what the user is allowed to pick, like what does the dialog hand back to you when they smash that OK button β¨
QFileDialog::AnyFileβ the name of a file, and honestly? doesn't even have to exist. living its fictional truth fr fr πQFileDialog::ExistingFileβ the name of one single file that, you know, actually exists. no imaginary friends allowed πQFileDialog::Directoryβ the name of a directory. and both files and directories get shown, so it's giving "look at everything" energy πQFileDialog::DirectoryOnlyβ the name of a directory, and this time the dialog is only showing directories. nothing else. tunnel vision. iconic πΈQFileDialog::ExistingFilesβ the names of zero or more existing files. yes zero, yes more, it's flexible like that π
See setMode().
π QFileDialog::PreviewMode π
this one describes the preview mode of the file dialog β aka the little sneak peek situation π
QFileDialog::NoPreviewβ no preview at all. nothing. the void. πQFileDialog::Contentsβ shows a preview of the contents of the current file, using the contents preview widget πQFileDialog::Infoβ shows information about the current file, using the info preview widget β¨
See setPreviewMode(), setContentsPreview() and setInfoPreview().
π¦ QFileDialog::ViewMode π¦
this enum describes the view mode of the file dialog, i.e. what info about each file is gonna be displayed, no cap π«Ά
QFileDialog::Listβ displays file and directory names with icons. simple, clean, slay β¨QFileDialog::Detailβ displays file and directory names with icons plus extra info like file size and modification date. the full dossier π
See setViewMode().
π MEMBER FUNCTION DOCUMENTATION π
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 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 gets used as the dialog's working directory, i.e. it's the directory that's showing when the dialog pops up. And if filter is specified, that becomes the dialog's file filter. easy π
QFileDialog::QFileDialog ( QWidget * parent = 0, const char * name = 0, bool modal = FALSE )
Constructs a file dialog called name, with the parent, parent. If modal is TRUE then the file dialog is modal; otherwise it is modeless. same vibe, just without the dir and filter preloaded β¨
QFileDialog::~QFileDialog ()
Destroys the file dialog. bye bestie, it was real π
void QFileDialog::addFilter ( const QString & filter )
Adds the filter filter to the list of filters and makes it the current filter.
QFileDialog* fd = new QFileDialog( this );
fd->addFilter( "Images (*.png *.jpg *.xpm)" );
fd->show();
so in that example above, a file dialog gets created, and the file filter "Images (*.png *.jpg *.xpm)" is added and set as the current filter. and the original filter, "All Files (*)", is still available. nothing lost, everything gained π«Ά
See also setFilter() and setFilters().
void QFileDialog::addLeftWidget ( QWidget * w ) [protected]
Adds the widget w to the left-hand side of the file dialog. left side bestie π
See also addRightWidget(), addWidgets(), and addToolButton().
void QFileDialog::addRightWidget ( QWidget * w ) [protected]
Adds the widget w to the right-hand side of the file dialog. matching set, obviously β¨
See also addLeftWidget(), addWidgets(), and addToolButton().
void QFileDialog::addToolButton ( QButton * b, bool separator = FALSE ) [protected]
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. little breathing room, we love to see it πΈ
See also addWidgets(), addLeftWidget(), and addRightWidget().
void QFileDialog::addWidgets ( QLabel * l, QWidget * w, QPushButton * b ) [protected]
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 pushbutton.
MyFileDialog::MyFileDialog( QWidget* parent, const char* name ) :
QFileDialog( 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 one of the widgets added, just pass 0 in that widget's position. no pressure π
Every time you call this function, a new row of widgets will be added to the bottom of the file dialog. every. single. time. it stacks π
See also addToolButton(), addLeftWidget(), and addRightWidget().
const QDir * QFileDialog::dir () const
Returns the current directory shown in the file dialog.
The ownership of the QDir pointer is transferred to the caller, so it must be deleted by the caller when no longer required. yes bestie that's on you, don't forget or it's a leak π
See also setDir().
void QFileDialog::dirEntered ( const QString & ) [signal]
This signal is emitted when the user enters a directory. πͺβ¨
See also dir().
QString QFileDialog::dirPath () const
Returns the file dialog's working directory. See the "dirPath" property for details. π
void QFileDialog::fileHighlighted ( const QString & ) [signal]
This signal is emitted when the user highlights a file, i.e. makes it the current file. not selecting, just hovering with intent π
See also fileSelected() and filesSelected().
void QFileDialog::fileSelected ( const QString & ) [signal]
This signal is emitted when the user selects a file. now it's official π
See also filesSelected(), fileHighlighted(), and selectedFile.
void QFileDialog::filesSelected ( const QStringList & ) [signal]
This signal is emitted when the user selects one or more files in ExistingFiles mode. plural girlies rise π«Ά
See also fileSelected(), fileHighlighted(), and selectedFiles.
void QFileDialog::filterSelected ( const QString & ) [signal]
This signal is emitted when the user selects a filter. π
See also selectedFilter.
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 ) [static]
This is a convenience static function that will return an existing directory selected by the user. one line, whole dialog, we stan convenience π
QString s = QFileDialog::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 QString::null in which case the current directory and a default caption will be used respectively.
Note on Windows that if dir is QString::null then the dialog's working directory will be set to the user's My Documents directory. yes really πͺ
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. plot twist π
Under 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 and if parent is not 0 then it will position the dialog just under the parent's titlebar). π that's a whole situation
See also getOpenFileName(), getOpenFileNames(), and getSaveFileName().
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 ) [static]
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. no file for you bestie π
QString s = QFileDialog::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 QString::null. β¨
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 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 and if parent is not 0 then it will position the dialog just under the parent's titlebar). same drama, different function π
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.
See also getOpenFileNames(), getSaveFileName(), and getExistingDirectory().
Examples:
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 ) [static]
okay bestie this one is a convenience static function and it hands you back one or more existing files that the user picked π
QStringList files = QFileDialog::getOpenFileNames(
"Images (*.png *.xpm *.jpg)",
"/home",
this,
"open files dialog",
"Select one or more files to open" );
the way it works: it makes a modal file dialog called name, with parent parent. if parent is not 0, the dialog gets shown centered over the parent π
the file dialog's working directory gets set to dir. if dir includes a file name, that file gets selected. the filter is set to filter so only the files that match it show up. the filter that's selected gets set to selectedFilter. and listen β dir, selectedFilter and filter may all be QString::null, no stress π«Ά
the dialog's caption is set to caption. if caption isn't specified, a default caption gets used β¨
under Windows and Mac OS X, this static function uses the native file dialog and not a QFileDialog, unless the style of the application is set to something other than the native style. (heads up: on Windows the dialog spins a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it positions the dialog just under the parent's titlebar π)
under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. like, 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 treats symlinks as regular directories π
note that if you want to iterate over the list of files, you should iterate over a copy, like this:
QStringList list = files;
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
see also getOpenFileName(), getSaveFileName(), and getExistingDirectory() πΈ
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 ) [static]
another convenience static function, this time returning a file name the user selected. and here's the slay part β 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 is shown centered over the parent β¨
QString s = QFileDialog::getSaveFileName(
"/home",
"Images (*.png *.xpm *.jpg)",
this,
"save file dialog",
"Choose a filename to save under" );
the file dialog's working directory gets set to startWith. if startWith includes a file name, that file gets selected. the filter is set to filter so only matching files show. the filter selected is set to selectedFilter. the parameters startWith, selectedFilter and filter may be QString::null π¦
the dialog's caption is set to caption. if caption isn't specified, a default caption is used π
under Windows and Mac OS X, this static function uses 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 spins a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it positions the dialog just under the parent's titlebar π)
under Unix/X11, the normal behavior is to resolve and follow symlinks. e.g. 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 treats symlinks as regular directories β
see also getOpenFileName(), getOpenFileNames(), and getExistingDirectory() π
Examples:
QFileIconProvider * QFileDialog::iconProvider () [static]
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 π
see also setIconProvider() and QFileIconProvider π
bool QFileDialog::isContentsPreviewEnabled () const
returns TRUE if the file dialog can provide a contents preview of the currently selected file; otherwise returns FALSE. peep the "contentsPreview" property for the deets π
bool QFileDialog::isInfoPreviewEnabled () const
returns TRUE if the file dialog can provide preview information about the currently selected file; otherwise returns FALSE. check the "infoPreview" property for details β¨
Mode QFileDialog::mode () const
returns the file dialog's mode. go look at the "mode" property for details π
PreviewMode QFileDialog::previewMode () const
returns the preview mode for the file dialog. the "previewMode" property has the details π
void QFileDialog::rereadDir ()
rereads the current directory shown in the file dialog π
the only time you'll ever need to call this is if the contents of the directory change and you want to refresh the file dialog to reflect that change, no cap
see also resortDir() πΈ
void QFileDialog::resortDir ()
re-sorts the displayed directory π₯
see also rereadDir() β
void QFileDialog::selectAll ( bool b )
if b is TRUE then all the files in the current directory are selected; otherwise, they get deselected π«Ά
QString QFileDialog::selectedFile () const
returns the name of the selected file. the "selectedFile" property has the details β¨
QStringList QFileDialog::selectedFiles () const
returns the list of selected files. the "selectedFiles" property has the details π
QString QFileDialog::selectedFilter () const
returns the filter which the user has selected in the file dialog. the "selectedFilter" property has the details π
void QFileDialog::setContentsPreview ( QWidget * w, QFilePreview * preview )
sets the widget to be used for displaying the contents of the file to the widget w, and a preview of those contents to the QFilePreview preview π
normally you'd create a preview widget that derives from both QWidget and QFilePreview, so you should pass the same widget twice. if you don't, you must remember to delete the preview object to avoid memory leaks π
class Preview : public QLabel, public QFilePreview
{
public:
Preview( QWidget *parent=0 ) : QLabel( parent ) {}
void previewUrl( const QUrl &u )
{
QString path = u.path();
QPixmap pix( path );
if ( pix.isNull() )
setText( "This is not a pixmap" );
else
setPixmap( pix );
}
};
//...
int main( int argc, char** argv )
{
Preview* p = new Preview;
QFileDialog* fd = new QFileDialog( this );
fd->setContentsPreviewEnabled( TRUE );
fd->setContentsPreview( p, p );
fd->setPreviewMode( QFileDialog::Contents );
fd->show();
}
see also contentsPreview, setInfoPreview(), and previewMode π
example: qdir/qdir.cpp π¦
void QFileDialog::setContentsPreviewEnabled ( bool )
sets whether the file dialog can provide a contents preview of the currently selected file. the "contentsPreview" property has the details π
void QFileDialog::setDir ( const QDir & dir )
sets the file dialog's working directory to dir π
see also dir() β¨
void QFileDialog::setDir ( const QString & pathstr ) [slot]
this is an overloaded member function, provided for convenience. it behaves essentially like the function above π
sets the file dialog's working directory to pathstr π
see also dir() πΈ
void QFileDialog::setFilter ( const QString & newFilter ) [slot]
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 semi-colons, then only the text contained in the parentheses is used as the filter. so all of these calls are 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++" );
see also setFilters() β
void QFileDialog::setFilters ( const QString & filters ) [slot]
sets the filters used in the file dialog to filters. each group of filters must be separated by ;; (two semi-colons) π«Ά
QString types("Image files (*.png *.xpm *.jpg);;"
"Text files (*.txt);;"
"Any files (*)");
QFileDialog fd = new QFileDialog( this );
fd->setFilters( types );
fd->show();
void QFileDialog::setFilters ( const char ** types ) [slot]
this is an overloaded member function, provided for convenience. it behaves essentially like the function above β¨
types must be a null-terminated list of strings π
void QFileDialog::setFilters ( const QStringList & ) [slot]
this is an overloaded member function, provided for convenience. it behaves essentially like the function above πΈ
void QFileDialog::setIconProvider ( QFileIconProvider * provider ) [static]
sets the QFileIconProvider used by the file dialog to provider π
the default is that there is no QFileIconProvider, and QFileDialog just draws a folder icon next to each directory and nothing next to files π
see also QFileIconProvider and iconProvider() π
example: showimg/main.cpp π
void QFileDialog::setInfoPreview ( QWidget * w, QFilePreview * preview )
bestie, this one hooks up your info-preview situation π
it sets the widget used for showing info about the file to w, and it sets the preview of that info to the QFilePreview called preview.
Real talk: normally you'd build a preview widget that inherits from both QWidget and QFilePreview, so you'd pass the same widget in twice. If you don't do that, you must remember to delete the preview object yourself, or you're gonna leak memory and that's giving messy π
class Preview : public QLabel, public QFilePreview
{
public:
Preview( QWidget *parent=0 ) : QLabel( parent ) {}
void previewUrl( const QUrl &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;
QFileDialog* fd = new QFileDialog( this );
fd->setInfoPreviewEnabled( TRUE );
fd->setInfoPreview( p, p );
fd->setPreviewMode( QFileDialog::Info );
fd->show();
}
See also setContentsPreview(), infoPreview, and previewMode. π¦
void QFileDialog::setInfoPreviewEnabled ( bool )
Sets whether the file dialog can provide preview info about whatever file is currently selected β¨ peek the "infoPreview" property for the deets.
void QFileDialog::setMode ( Mode )
Sets the file dialog's mode. the "mode" property has the full story π
void QFileDialog::setPreviewMode ( PreviewMode m )
Sets the preview mode for the file dialog to m. go read the "previewMode" property for more πΈ
void QFileDialog::setSelectedFilter ( const QString & mask )[virtual]
Sets the current filter selected in the file dialog to the first one that contains the text mask.
void QFileDialog::setSelectedFilter ( int n )[virtual]
ok so this is an overloaded member function, provided for convenience β it behaves essentially like the function above π«Ά
It sets the current filter selected in the file dialog to the n-th filter in the filter list.
See also filterSelected(), selectedFilter, selectedFiles, and selectedFile. β¨
void QFileDialog::setSelection ( const QString & filename )
Sets the default selection to filename π and heads up: if filename is absolute, setDir() gets called too, to set the file dialog's working directory to the directory that filename lives in.
Example: qdir/qdir.cpp.
void QFileDialog::setShowHiddenFiles ( bool s )
Sets whether hidden files are shown in the file dialog to s π the "showHiddenFiles" property has the details.
void QFileDialog::setUrl ( const QUrlOperator & url )[slot]
Sets the file dialog's working directory to the directory specified at url.
See also url(). π
void QFileDialog::setViewMode ( ViewMode m )
Sets the file dialog's view mode to m. check the "viewMode" property for more π
bool QFileDialog::showHiddenFiles () const
Returns TRUE if hidden files are shown in the file dialog; otherwise returns FALSE π the "showHiddenFiles" property has the details.
QUrl QFileDialog::url () const
Returns the URL of the current working directory in the file dialog π₯
See also setUrl().
Example: network/networkprotocol/view.cpp.
ViewMode QFileDialog::viewMode () const
Returns the file dialog's view mode π the "viewMode" property has the deets.
β¨ Property Documentation β¨
π bool contentsPreview
This property holds whether the file dialog can provide a contents preview of the currently selected file.
The default is FALSE.
See also setContentsPreview() and infoPreview.
Set this property's value with setContentsPreviewEnabled() and get this property's value with isContentsPreviewEnabled(). π
πΈ QString dirPath
This property holds the file dialog's working directory.
Get this property's value with dirPath().
See also dir() and setDir(). π¦
π bool infoPreview
This property holds whether the file dialog can provide preview information about the currently selected file.
The default is FALSE.
Set this property's value with setInfoPreviewEnabled() and get this property's value with isInfoPreviewEnabled(). β¨
π Mode mode
This property holds the file dialog's mode.
The default mode is ExistingFile.
Set this property's value with setMode() and get this property's value with mode(). π
π PreviewMode previewMode
This property holds the preview mode for the file dialog.
ngl this one matters: if you set the mode to anything other than NoPreview, you must use setInfoPreview() or setContentsPreview() to set the dialog's preview widget to your preview widget, and enable the preview widget(s) with setInfoPreviewEnabled() or setContentsPreviewEnabled() π«Ά
See also infoPreview, contentsPreview, and viewMode.
Set this property's value with setPreviewMode() and get this property's value with previewMode(). β
π QString selectedFile
This property holds the name of the selected file.
If a file was selected, selectedFile contains the file's name including its absolute path; otherwise selectedFile is empty π
See also QString::isEmpty(), selectedFiles, and selectedFilter.
Get this property's value with selectedFile(). π
π§ QStringList selectedFiles
This property holds the list of selected files.
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, like:
QStringList list = myFileDialog.selectedFiles();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
See also selectedFile, selectedFilter, and QValueList::empty().
Get this property's value with selectedFiles(). π₯
π QString selectedFilter
This property holds the filter which the user has selected in the file dialog.
Get this property's value with selectedFilter().
See also filterSelected(), selectedFiles, and selectedFile. β¨
π bool showHiddenFiles
This property holds whether hidden files are shown in the file dialog.
The default is FALSE, i.e. don't show hidden files.
Set this property's value with setShowHiddenFiles() and get this property's value with showHiddenFiles(). π
π¦ ViewMode viewMode
This property holds the file dialog's view mode.
If you set the view mode to be Detail (which is the default), then you'll see the file's details β stuff like 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'll just see a list of the files and folders.
See also QFileDialog::ViewMode
Set this property's value with setViewMode() and get this property's value with viewMode(). π
π SEE ALSO
Β©οΈ COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
βοΈ AUTHOR
Generated automatically from the source code.
π BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech π
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qfiledialog.3qt) and the Qt version (3.3.8). π
okay that's the whole thing bestie, we did it π β¨ this page came from Trolltech AS, dated 2 February 2007, and it's the manual for QFileDialog(3qt) β go forth and pick files, you absolute icon πππ¦
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms