×
Namespaces

Variants
Actions
Revision as of 19:20, 12 September 2009 by deepikagohil (Talk | contribs)

Archived:Writeboard (Notepad) app in Qt

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}


Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 5th Edition
Symbian
Article
Keywords: QFile,QAction,QFiledialog,QTextEdit
Created: (15 Jul 2009)
Last edited: deepikagohil (12 Sep 2009)

Overview

This is a simple notepad(writeboard) like application,which is used to create,open and save new file. Their are several functions(load(),saveFileAs())for different purpose like loading file ,saving files.

Name of classes used:

QFile-Provides an interface for reading from and writing to files.

QAction-Provides an abstract user interface action that can be inserted into widgets.

QFiledialog-Provides a dialog that allow users to select files or directories.

Preconditions

Source Code

CPP file

#include "viral.h"
 
viral::viral(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("Writeboard");
edit=new QTextEdit(this);
menubar=new QMenu(this);
bar= new QMenuBar(this);
 
//setStyleSheet("* { background-color:rgb(25,0,2);color:rgb(255,255,255); padding: 7px}}");
bar->setMinimumWidth(2);
lay=new QVBoxLayout(this);
status=new QStatusBar(this);
newfile1=new QAction(("&New"),this);
connect(newfile1,SIGNAL(triggered()),this,SLOT(newfile()));
open=new QAction(("&Open"),this);
connect(open,SIGNAL(triggered()),this,SLOT(loadme()));
save=new QAction(("&Save"),this);
connect(save,SIGNAL(triggered()),this,SLOT(saveme()));
saveAs=new QAction(("SaveAs"),this);
connect(saveAs,SIGNAL(triggered()),this,SLOT(saveme()));
exi=new QAction(("&Exit"),this);
connect(exi,SIGNAL(triggered()),qApp,SLOT(quit()));
un=new QAction(("&Undo"),this);
connect(un,SIGNAL(triggered()),this,SLOT(undome()));
re=new QAction(("&redo"),this);
connect(re,SIGNAL(triggered()),this,SLOT(redome()));
cutme=new QAction(("&Cut"),this);
connect(cutme,SIGNAL(triggered()),this,SLOT(Cut()));
copyme=new QAction(("&Copy"),this);
connect(copyme,SIGNAL(triggered()),this,SLOT(Copy()));
pasteme=new QAction(("&Paste"),this);
connect(pasteme,SIGNAL(triggered()),this,SLOT(Paste()));
mystatus=new QAction(("&Status Bar"),this);
mystatus->setCheckable(1);
connect(mystatus,SIGNAL(toggled(bool)),status,SLOT(setHidden(bool)));
about=new QAction(("About App"),this);
connect(about,SIGNAL(triggered()),this,SLOT(aboutme()));
menubar=bar->addMenu(tr("&File"));
 
menubar1=bar->addMenu(tr("&Edit"));//Adding new menu to menubar
 
menubar2=bar->addMenu(tr("&View"));
 
menubar3=bar->addMenu(tr("&About"));
 
menubar->addAction(newfile1);
menubar->addSeparator();
menubar->addAction(open);
menubar->addSeparator();//Adding separator between to menu items
menubar->addAction(save);
menubar->addAction(saveAs);
menubar->addSeparator();
menubar->addAction(exi);
 
menubar1->addAction(un);
menubar1->addAction(re);
menubar1->addSeparator();
menubar1->addAction(cutme);//Merging Action in menubar
menubar1->addSeparator();
menubar1->addAction(copyme);
menubar1->addSeparator();
menubar1->addAction(pasteme);
 
menubar2->addAction(mystatus);
 
menubar3->addAction(about);
lay->addWidget(bar);
 
lay->addWidget(edit);
lay->addWidget(status);
setLayout(lay);
showMaximized();
 
 
 
}
 
viral::~viral()
{
 
}
void viral::loadme()
{
QString filename = QFileDialog::getOpenFileName(this,tr("Open File"),tr("*.txt"));
QFile file(filename);
if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
edit->setPlainText(QString::fromUtf8(file.readAll()));
mFilePath = filename;
status->showMessage(tr("File successfully loaded."), 3000);
}
}
void viral::saveme()
{
if(mFilePath.isEmpty())
saveFileAs();
else
saveFile(mFilePath);
}
void viral::saveFile(const QString &name)
{
QFile file(name);
if (file.open(QIODevice::WriteOnly|QIODevice::Text))
{
file.write(edit->toPlainText().toUtf8());
}
status->showMessage(tr("File successfully Saved."), 3000);
}
void viral::saveFileAs()
{
mFilePath = QFileDialog::getSaveFileName(this,tr("Save File"),tr(".txt"));
if(mFilePath.isEmpty())
return;
saveFile(mFilePath);
status->showMessage(tr("File Saved"), 3000);
 
}
bool viral::mayDiscardDocument()
{
if (edit->document()->isModified())
{
QString filename = mFilePath;
if (filename.isEmpty()) filename = tr("Unnamed");
if (QMessageBox::question(this, tr("Save Document?"),tr("You want to create a new document, but the ""changes in the current document ’%1’ have not ""been saved. How do you want to proceed?"),tr("Save Document"), tr("Cancel")))
{
 
return false;
}
else
{
saveme();
return true;
 
}
}
}
void viral::newfile()
{
if (!mayDiscardDocument()) return;
edit->setPlainText("");
mFilePath = "";
status->showMessage(tr("New File created"),3000);
}
void viral::undome()
{
edit->document()->undo();
}
void viral::redome()
{
edit->document()->redo();
}
void viral::Cut()
{
edit->cut();
status->showMessage(tr("Text Cut"),3000);
}
void viral::Copy()
{
edit->copy();
status->showMessage(tr("Text Copied"),3000);
}
void viral::Paste()
{
edit->paste();
status->showMessage(tr("Text pasted"),3000);
}
void viral::aboutme()
{
QMessageBox::about(this, tr("About Writeboard"),"Writeboard 1.0\n A Qt application\n" "Copyrights at mind_freak@ovi.com");
}

Screenshots

Writeboard.JPG


Writeboard1.JPG

Zip File

Writeboard.zip

122 page views in the last 30 days.