You Are Here:

Community: Wiki

This page was last modified on 6 November 2009, at 08:35.

Save and Open Files with QFileDialogBox

From Forum Nokia Wiki

Reviewer Approved   



ID ... Creation date 13 July 2009
Platform S60 5th Edition Tested on devices S60 Emulator
Category Qt for S60 Subcategory Application


Keywords (APIs, classes, methods, functions): QFileDialog,QFile

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

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fComboBoE78E5fwithE5fcolorE5fpalettesE5finE5fE51tE5fforE5fSymbianX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZE71tQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxE71tX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx3E2E0000X