Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Como criar um bloco de notas (notepad), em Qt

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

Article Metadata

Testado com
Aparelho(s): S60 Emulator

Compatibilidade
Plataforma(s): S60 5th Edition
Symbian

Artigo
Palavras-chave: QFile,QAction,QFiledialog,QTextEdit
Tradução:
Por valderind4
Última alteração feita por hamishwillee em 11 Oct 2012

Contents

Introdução

Neste artigo nós temos um simples bloco de notas como aplicação, a qual é usada para criar, abrir ou salvar novos arquivos. Possui muitas funcionalidades (load(),saveFileAs()), para diferentes propósitos como: carregar e salvar arquivos.

Nome de classes usadas:

QFile-Provêr uma interface para leitura e escrita de arquivos.

QAction-Provêr uma ação de interface do usuário abstrata, a qual pode ser inseridos dentro dos widgets.

QFiledialog-Provêr uma caixa de diálogo, que permite o usuário selecionar um arquivo ou diretório.

Pré-requisitos

  • Baixe e instale a versão atual do Qt segundo as instruções Qt (Portuguese).]

Código fonte

Arquivo CPP

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

Captura de tela

Writeboard.JPG


Writeboard1.JPG

Arquivo para Download

File:Writeboard.zip

This page was last modified on 11 October 2012, at 01:13.
417 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.

×