最近在学QT,模仿windows记事本写了一个记事本。只是为了练习一下,很多细节都没有处理,讲究看一下。
myntebook.h
<span style="font-size:18px;">#ifndef MYNOTEBOOK_H
#define MYNOTEBOOK_H
#include <QtGui/QtGui>
#include "finddialog.h"
class MyNoteBook:public QMainWindow
{
Q_OBJECT
public:
MyNoteBook(QWidget *parent=0);
void CreateAction();
void CreateMenu();
void CreateStatusBar();
bool isoktocontinue();
QString getselectstr();
void changehighlight();
private slots:
void New();
void Open();
bool Save();
void SetPage();
void Print();
void Cut();
void Copy();
void Paste();
void Find();
void SwitchStatusBar();
void SetFont();
void Date();
void InsertImage();
void findnextslot(const QString &str);
void findpreviousslot(const QString &str);
void modifystatus();
private:
QAction *newfile;
QAction *openfile;
QAction *savefile;
QAction *savefileas;
QAction *pageset;
QAction *print;
QAction *exit;
QAction *cut;
QAction *copy;
QAction *paste;
QAction *deletetext;
QAction *find;
QAction *findnext;
QAction *instead;
QAction *turnto;
QAction *selectall;
QAction *date;
QAction *font;
QAction *switchstatus;
QAction *aboutnote;
QAction *insertimage;
QAction *inserttable;
QMenu *file;
QMenu *edit;
QMenu *style;
QMenu *check;
QMenu *help;
QMenu *insert;
QLabel *statuslabel;
QTextEdit *textedit;
QTextDocument *editdocument;
bool statusexit=true;
bool autoline=true;
};
#endif // MYNOTEBOOK
</span>
mynotebook.cpp
#include "mynotebook.h"
#include "finddialog.h"
MyNoteBook::MyNoteBook(QWidget *parent):QMainWindow(parent)
{
textedit=new QTextEdit;
editdocument=textedit->document();
CreateAction();
CreateMenu();
CreateStatusBar();
changehighlight();
resize(QSize(400,350));
setCentralWidget(textedit);
}
void MyNoteBook::CreateAction()
{
newfile=new QAction("New",this);
newfile->setStatusTip("create a new file");
newfile->setShortcut(tr("Ctrl+N"));
connect(newfile,SIGNAL(triggered()),this,SLOT(New()));
openfile=new QAction("Open",this);
openfile->setStatusTip("open a file");
openfile->setShortcut(tr("Ctrl+O"));
connect(openfile,SIGNAL(triggered()),this,SLOT(Open()));
savefile=new QAction("Save",this);
savefile->setStatusTip("save a file");
savefile->setShortcut(tr("Ctrl+S"));
connect(savefile,SIGNAL(triggered()),this,SLOT(Save()));
pageset=new QAction("SetPage",this);
pageset->setStatusTip("set page");
pageset->setShortcut(tr("Ctrl+P"));
connect(pageset,SIGNAL(triggered()),this,SLOT(SetPage()));
print=new QAction("Print",this);
print->setStatusTip("print file");
connect(print,SIGNAL(triggered()),this,SLOT(Print()));
exit=new QAction("Exit",this);
exit->setStatusTip("exit appliacation");
connect(exit,SIGNAL(triggered()),this,SLOT(close()));
cut=new QAction("Cut",this);
cut->setStatusTip("Cut text");
cut->setShortcut(tr("Ctrl+X"));
connect(cut,SIGNAL(triggered()),this,SLOT(Cut()));
copy=new QAction("Copy",this);
copy->setStatusTip("copy text");
copy->setShortcut(tr("Ctrl+C"));
connect(copy,SIGNAL(triggered()),this,SLOT(Copy()));
paste=new QAction("Paste",this);
paste->setStatusTip("paste text");
paste->setShortcut(tr("Ctrl+V"));
connect(newfile,SIGNAL(triggered()),this,SLOT(Paste()));
find=new QAction("Find",this);
find->setStatusTip("find a word");
connect(find,SIGNAL(triggered()),this,SLOT(Find()));
findnext=new QAction("Find Next",this);
findnext->setStatusTip("find next text");
connect(newfile,SIGNAL(triggered()),this,SLOT(Find()));
selectall=new QAction("SelectAll",this);
date=new QAction(tr("Date"),this);
date->setStatusTip(tr("date"));
connect(date,SIGNAL(triggered()),this,SLOT(Date()));
insertimage=new QAction(tr("Insert Image"),this);
insertimage->setStatusTip(tr("Insert image"));
connect(insertimage,SIGNAL(triggered()),this,SLOT(InsertImage()));
font=new QAction("Font",this);
font->setStatusTip("set font");
connect(font,SIGNAL(triggered()),this,SLOT(SetFont()));
switchstatus=new QAction("StatusBar",this);
switchstatus->setCheckable(true);
switchstatus->setChecked(true);
switchstatus->setStatusTip("switch statusbar");
connect(switchstatus,SIGNAL(triggered()),this,SLOT(SwitchStatusBar()));
aboutnote=new QAction("About",this);
}
void MyNoteBook::CreateMenu()
{
file=menuBar()->addMenu("File");
file->addAction(newfile);
file->addAction(openfile);
file->addAction(savefile);
file->addAction(pageset);
file->addAction(print);
file->addSeparator();
file->addAction(exit);
edit=menuBar()->addMenu("Edit");
edit->addAction(cut);
edit->addAction(copy);
edit->addAction(paste);
edit->addAction(find);
edit->addAction(findnext);
edit->addSeparator();
edit->addAction(selectall);
edit->addAction(date);
insert=menuBar()->addMenu("Insert");
insert->addAction(insertimage);
style=menuBar()->addMenu("Style");
style->addAction(font);
check=menuBar()->addMenu("Check");
check->addAction(switchstatus);
menuBar()->addSeparator();
help=menuBar()->addMenu("Help");
help->addAction(aboutnote);
}
void MyNoteBook::CreateStatusBar()
{
statuslabel=new QLabel("MyNoteBook");
statuslabel->setAlignment(Qt::AlignHCenter);
statuslabel->setIndent(3);
statuslabel->setMinimumSize(statuslabel->sizeHint());
statusBar()->addWidget(statuslabel,0);
statusBar()->setStyleSheet(QString("QStatusBar::item{border:0px}"));
connect(textedit,SIGNAL(cursorPositionChanged()),this,SLOT(modifystatus()));
}
QString MyNoteBook::getselectstr()
{
return textedit->textCursor().selectedText();
}
void MyNoteBook::New()
{
if(isoktocontinue())
textedit->clear();
}
bool MyNoteBook::isoktocontinue()
{
if(textedit->isWindowModified())
{
int r=QMessageBox::warning(this,"NoteBook","Do you want to save your change?",
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
if(r==QMessageBox::Yes)
return Save();
else if(r==QMessageBox::Cancel)
return false;
}
return true;
}
void MyNoteBook::Open()
{
QString filename=QFileDialog::getOpenFileName(this,tr("Open File"),".","NoteBook(*txt)");
if(!filename.isEmpty())
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QMessageBox::warning(this,"NoteBook",
tr("cant read file %1:\n%2")
.arg(file.fileName()).arg(file.errorString()));
return;
}
QString filecontent;
while(!file.atEnd())
{
filecontent=file.readLine();
textedit->append(filecontent);
}
file.close();
textedit->moveCursor(QTextCursor::Start);
}
}
bool MyNoteBook::Save()
{
QString filename=QFileDialog::getSaveFileName(this,"Save File",".","NoteBook(*txt)");
if(!filename.isEmpty())
{
QFile file(filename);
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QMessageBox::warning(this,"NoteBook",
tr("cant create file %1:\n%2")
.arg(file.fileName()).arg(file.errorString()));
return false;
}
QTextStream out(&file);
out<<textedit->toPlainText();
}
return true;
}
void MyNoteBook::SetPage()
{
QPrinter printer;
QPageSetupDialog dlg(&printer,this);
dlg.setWindowTitle("PageSet");
if(dlg.exec()==QDialog::Accepted)
{
}
}
void MyNoteBook::Print()
{
QPrinter printer;
QPrintDialog printdialog(&printer,this);
printdialog.setWindowTitle("print");
if(printdialog.exec()==QDialog::Accepted)
{
}
}
void MyNoteBook::Cut()
{
textedit->cut();
}
void MyNoteBook::Copy()
{
textedit->copy();
}
void MyNoteBook::Paste()
{
textedit->paste();
}
void MyNoteBook::Find()
{
FindDialog *finddialog=new FindDialog(this);
connect(finddialog,SIGNAL(findnextsignal(const QString &)),
this,SLOT(findnextslot(const QString &)));
connect(finddialog,SIGNAL(findprevioussignal(const QString &)),
this,SLOT(findpreviousslot(const QString &)));
finddialog->show();
}
void MyNoteBook::Date()
{
QDateTime currentdatetime=QDateTime::currentDateTime();
QString currentdate=currentdatetime.toString("yyyy-MM-dd hh:mm:ss ddd");
textedit->append(currentdate);
}
void MyNoteBook::SwitchStatusBar()
{
if(statusexit)
{
statusBar()->hide();
statusexit=false;
}
else
{
statusBar()->setVisible(true);
statusexit=true;
}
}
void MyNoteBook::SetFont()
{
bool ok;
const QFont &font=QFontDialog::getFont(&ok,
textedit->font(),
this,"FontDialog");
if(ok)
{
textedit->setFont(font);
}
}
void MyNoteBook::InsertImage()
{
QString file=QFileDialog::getOpenFileName(this,
tr("Select an image"),
".");
QUrl url(QString("file://%1").arg(file));
QImage image=QImageReader(file).read();
QTextDocument *textdocument=textedit->document();
textdocument->addResource(QTextDocument::ImageResource,url,QVariant(image));
QTextCursor cursor=textedit->textCursor();
QTextImageFormat imageformat;
imageformat.setWidth(image.width());
imageformat.setHeight(image.height());
imageformat.setName(url.toString());
cursor.insertImage(imageformat);
}
void MyNoteBook::findnextslot(const QString &str)
{
//textedit->moveCursor(QTextCursor::Start);
if(!textedit->find(str))
{
QMessageBox::warning(this,tr("Find"),tr("Cant find the text"));
}
}
void MyNoteBook::findpreviousslot(const QString &str)
{
//textedit->moveCursor(QTextCursor::End);
if(!textedit->find(str,QTextDocument::FindBackward))
{
QMessageBox::warning(this,tr("Find"),tr("Cant find the text"));
}
}
void MyNoteBook::changehighlight()
{
QPalette p=this->palette();
QColor color1=p.color(QPalette::Active,QPalette::Highlight);
QColor color2=p.color(QPalette::Active,QPalette::HighlightedText);
p.setColor(QPalette::Inactive,QPalette::Highlight,color1);
p.setColor(QPalette::Inactive,QPalette::HighlightedText,color2);
this->setPalette(p);
}
void MyNoteBook::modifystatus()
{
int row,col;
QTextCursor cursor=textedit->textCursor();
row=cursor.blockNumber();
col=cursor.columnNumber();
statuslabel->setText(tr("%1 line, %2 col").arg(row).arg(col));
}
finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QtGui/QtGui>
class FindDialog:public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent=0);
private:
QLabel *findlabel;
QPushButton *findnextbutton;
QPushButton *cancel;
QCheckBox *smallbig;
QRadioButton *up;
QRadioButton *down;
QLineEdit *lineedit;
QGroupBox *groupbox;
private slots:
void FindNext();
void enablefind(const QString &);
signals:
void findnextsignal(const QString &);
void findprevioussignal(const QString &);
public:
bool IsUp();
bool IsDown();
bool IsSmallBig();
};
#endif // FINDDIALOG
finddialog.cpp
#include "finddialog.h"
#include <QtGui/QtGui>
FindDialog::FindDialog(QWidget *parent):QDialog(parent)
{
findlabel=new QLabel(tr("Find Content"));
findlabel->setBuddy(lineedit);
lineedit=new QLineEdit;
findnextbutton=new QPushButton(tr("Find Next"));
findnextbutton->setDefault(true);
findnextbutton->setEnabled(false);
cancel=new QPushButton(tr("Cancel"));
groupbox=new QGroupBox(tr("fangxiang"));
smallbig=new QCheckBox(tr("daxiaoxie"));
up=new QRadioButton(tr("up"));
down=new QRadioButton(tr("down"));
down->setChecked(true);
connect(findnextbutton,SIGNAL(clicked()),this,SLOT(FindNext()));
connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
connect(lineedit,SIGNAL(textChanged(const QString &)),
this,SLOT(enablefind(const QString &)));
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(findlabel);
layout->addWidget(lineedit);
layout->addWidget(findnextbutton);
QHBoxLayout *layout2=new QHBoxLayout;
layout2->addWidget(up);
layout2->addWidget(down);
groupbox->setLayout(layout2);
QHBoxLayout *layout3=new QHBoxLayout;
layout3->addWidget(smallbig);
layout3->addWidget(groupbox);
layout3->addWidget(cancel);
QVBoxLayout *mainlayout=new QVBoxLayout;
mainlayout->addLayout(layout);
mainlayout->addLayout(layout3);
setLayout(mainlayout);
setWindowTitle(tr("Find"));
}
void FindDialog::FindNext()
{
QString findtext=lineedit->text();
// Qt::CaseSensitivity cs=
// smallbig->isChecked()?Qt::CaseSensitive
// :Qt::CaseInsensitive;
if(IsUp())
{
emit findprevioussignal(findtext);
}
if(IsDown())
{
emit findnextsignal(findtext);
}
}
void FindDialog::enablefind(const QString &text)
{
findnextbutton->setEnabled(!text.isEmpty());
}
bool FindDialog::IsUp()
{
return up->isChecked();
}
bool FindDialog::IsDown()
{
return down->isChecked();
}
bool FindDialog::IsSmallBig()
{
return smallbig->isChecked();
}
mainapp.cpp
#include "mynotebook.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
MyNoteBook mynotebook;
mynotebook.show();
return app.exec();
}