×
Namespaces

Variants
Actions
Revision as of 09:29, 3 September 2009 by croozeus (Talk | contribs)

Archived:Save and Open Files with QFileDialogBox

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 5th Edition
Symbian
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

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);
}

Related Links

151 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×