| 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 |
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.
QFileDialog *filedialog=new QFileDialog(); dialog->setAcceptMode(QFileDialog::AcceptSave);//To save files,default is open mode.
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;
}
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);
}
No related wiki articles found