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

Archived:Save and Open Files with QFileDialogBox

Jump to: navigation, search


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

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

311 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved