Reading and Saving with QFileDialog
Posted By : Satish Joshi | 28-Aug-2019
Introduction :
In Qt, there is an awesome module called QFileDialog which allow users for traversing the file system to select one or many files or directory. The main purpose of using this module is we can easily open, save the file or directory with the help of it's GUI interface. It is a child of QDialog as to manipulate any operation it triggers a window. So that we can either choose or save our files or directories.
Properties :
It inherits 2 properties inherited from QDialog 58 properties from QWidget and 1 property inherited from QObject which are easy to access. You can find more info on this particular link: https://doc.qt.io/qt-5/qfiledialog.html#QFileDialog-1.
You can also access its properties like acceptMode, defaultSuffix, fileMode, options, supportedSchemes, viewMode.
Implementation :
We are taking two examples of QFileDialog to describe it's usage. First is to open a file from a particular directory :
Here, I am using the easiest way to create a QFileDialog is to use its static functions. On different platforms like Windows, Mac OS X, KDE, and GNOME, these static functions will call the native file dialog.
Example :
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), DIR, tr("Image Files(*.png *.jpg)"));
In the above example, a modal is created with the help of getOpenFileName function. It will display the contents of the DIR path and displays files matching the patterns given on the string "Image Files(*.png *.jpg)". The parent of the dialog is set to "this", and the window title is set to "Open Image".
You can use multiple filters by separating each one with two semicolons like : "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)". Also, you can create your custom QFileDialog without using the static functions by calling setFileMode() to specify what the user selects like :
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
In the above example, the mode is set to AnyFile with the help of enum QFileDialog::FileMode meaning that the user can select any file, or even specify a file that doesn't exist. This mode is also useful for creating "Save As" file dialog which we do it later.
As we can set dialog's file filter by using setNameFilter. Also, we can set the view modes (List and Detail) to present the contents of the current directory, And the last important function when creating your file dialog is selectedFiles().
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
Here, In a modal, if a user clicked OK, the file they selected is put in fileNames.
Now, Come to the second most important functionality which is Save for a particular file or directory. We will be using the inbuilt function getSaveFileName() here to describe it's the flow:
Example :
QString saveFile = saveProjectDlg.getSaveFileName(this, tr("Project Save As"), temp, tr("System Files (*.txt)"));
In the above example, the dialog will be created but it will ask for an input on which the file or folder has to be saved. As we can see pattern will be the same for SaveAs as we have on Open but the requirement is different.
Conclusion :
Qt provides a very essential module and from the above examples, it shows how to use QFileDialog as well as other built-in Qt dialogs. These are the major operations performed in QFileDialog with easy access.
Thank You
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Satish Joshi
Satish has experience on web development believes in hard work and dedication. He is friendly and an honest person.