Archived:Save and Open Files with QFileDialogBox
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: QFileDialog,QFile
Created: (13 Jul 2009)
Last edited: croozeus
(03 Sep 2009)
Overview
Here is an example code in which we will develop an application. This application will demonstrate how to perform operations like file open , save.
This example makes the use of following classes
QFile - Performs the actual file operations
QFileDialog - Responsible for the FileDialog GUI.
Preconditions
- Install latest Qt for S60 see Qt for S60 - Installation packages
- Check this link for installation guide: How to install the package.
Mode selection
- This property holds the accept mode of the dialog.The action mode defines whether the dialog is for opening or saving files.
QFileDialog *filedialog=new QFileDialog(); dialog->setAcceptMode(QFileDialog::AcceptSave);//To save files,default is open mode.
Source Code
Code to create open dialog Box to load file
void Widget::loadme()//load a specific file
{
QString filename = QFileDialog::getOpenFileName(this);//getting the file name
QFile file(filename);
if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {//if file is already open do nothing
ui->textEdit->setPlainText(QString::fromUtf8(file.readAll()));
FilePath = filename;
}
Code for "Save" and "saveAs" to save file
void Widget::saveme()
{
if(FilePath.isEmpty())
saveFileAs();
else
saveFile(FilePath);
}
void Widget::saveFile(const QString &name) //save the existing file
{
QFile file(name);
if (file.open(QIODevice::WriteOnly|QIODevice::Text))
{
file.write(ui->textEdit->toPlainText().toUtf8());
}
}
void Widget::saveFileAs() //save as a new file
{
FilePath = QFileDialog::getSaveFileName(this);
if(FilePath.isEmpty())
return;
saveFile(mFilePath);
}

