//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QTextEdit>
/*程序退出的事件*/
#include <QCloseEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QTextEdit * edit1;
/*打开*/
QMenu *menufile;
/*编辑*/
QMenu *menuedit;
/*帮助*/
QMenu *menuhelper;
/*打开文件*/
QAction *openfilea;
/*创建文件*/
QAction *newfile;
/*保存文件*/
QAction *savefilea;
/*退出*/
QAction * exitprof;
/*复制*/
QAction *copyfile;
/*剪切*/
QAction *cutfile;
/*粘贴*/
QAction *pastefile;
/*关于记事本*/
QAction *lookinfo;
/*子类中重写父类退出函数,实现自定义退出*/
void closeEvent(QCloseEvent *event);
private slots:
void myopenfile();
void mycreatefile();
void mysavefile();
void myclosewin();
void mycopyfilen();
void mycutfile();
void mypastefile();
void mylookinfo();
};
#endif // MAINWINDOW_H
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(800,600);
w.show();
return a.exec();
}
//mainwindow.cpp
#include "mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setWindowTitle(tr("记事本"));
edit1=new QTextEdit();
this->setCentralWidget(edit1);
/*设置默认字体大小*/
edit1->setFontPointSize(15);
openfilea=new QAction(tr("打开"),this);
openfilea->setShortcut(tr("Ctrl+O"));
newfile=new QAction(tr("新建"),this);
newfile->setShortcut(tr("Ctrl+N"));
savefilea=new QAction(tr("保存"),this);
savefilea->setShortcut(tr("Ctrl+S"));
exitprof=new QAction(tr("退出"),this);
exitprof->setShortcut(tr("Ctrl+Q"));
menufile=this->menuBar()->addMenu(tr("文件"));
menufile->addAction(openfilea);
menufile->addAction(newfile);
menufile->addAction(savefilea);
/*在保存和退出之间加入一个分隔符,为了防止用户点击过快,直接点击退出程序了,友好设计*/
menufile->addSeparator();
menufile->addAction(exitprof);
connect(openfilea,SIGNAL(triggered()),this,SLOT(myopenfile()));
connect(newfile,SIGNAL(triggered()),this,SLOT(mycreatefile()));
connect(savefilea,SIGNAL(triggered()),this,SLOT(mysavefile()));
connect(exitprof,SIGNAL(triggered()),this,SLOT(myclosewin()));
copyfile=new QAction(tr("复制"),this);
copyfile->setShortcut(tr("Ctrl+C"));
cutfile=new QAction(tr("剪切"),this);
cutfile->setShortcut(tr("Ctrl+X"));
pastefile=new QAction(tr("粘贴"),this);
pastefile->setShortcut(tr("Ctrl+V"));
menuedit=this->menuBar()->addMenu(tr("编辑"));
menuedit->addAction(copyfile);
menuedit->addAction(cutfile);
menuedit->addAction(pastefile);
connect(copyfile,SIGNAL(triggered()),this,SLOT(mycopyfilen()));
connect(cutfile,SIGNAL(triggered()),this,SLOT(mycutfile()));
connect(pastefile,SIGNAL(triggered()),this,SLOT(mypastefile()));
lookinfo=new QAction(tr("关于记事本"),this);
lookinfo->setShortcut(tr("Ctrl+H"));
menuhelper=this->menuBar()->addMenu(tr("帮助"));
menuhelper->addAction(lookinfo);
connect(lookinfo,SIGNAL(triggered()),this,SLOT(mylookinfo()));
}
MainWindow::~MainWindow()
{
}
//打开文件
void MainWindow::myopenfile()
{
/*getOpenFileName不设置默认路径,那么默认路径就是当前目录*/
QString spath=QFileDialog::getOpenFileName(this,"打开文件","","文本文档(*.txt)");
if(spath.isEmpty()) return;
/*将中心控件内容清空*/
edit1->clear();
//读文本
QFile myfile(spath);
/*只读方式打开文本*/
if(myfile.open(QFile::ReadOnly))
{
QTextStream mystream(&myfile);
while(!mystream.atEnd())
{
edit1->append(mystream.readLine());//在text控件内容尾部追加从文件中读到的内容
}
/*读完文件后,关闭文件*/
myfile.close();
}else{
/*打开文件失败*/
QMessageBox::critical(this,"错误信息",myfile.errorString());
}
}
//新建文件
void MainWindow::mycreatefile()
{
//清空文本
edit1->clear();
}
//保存文件
void MainWindow::mysavefile()
{
/*打开保存文件对话框*/
QString spath=QFileDialog::getSaveFileName(this,"保存文件","/","文本文档(*.txt)");
if(spath.isEmpty()) return;
/*写文件*/
QFile myfile(spath);
if(myfile.open(QFile::WriteOnly|QFile::Truncate))
{
//定义文件流
QTextStream mystream(&myfile);
/*将edit1的文本内容转化成QString后写入文件*/
mystream<<edit1->toPlainText();
/*关闭文件*/
myfile.close();
}else{
//保存文件失败
QMessageBox::critical(this,"错误信息",myfile.errorString());
}
}
//退出程序
void MainWindow::myclosewin()
{
this->close();
}
//复制
void MainWindow::mycopyfilen()
{
//
edit1->copy();
}
//剪切
void MainWindow::mycutfile()
{
//
edit1->cut();
}
//粘贴
void MainWindow::mypastefile()
{
//
edit1->paste();
}
//帮助信息
void MainWindow::mylookinfo()
{
//
QMessageBox::information(this,"版权信息","版权所有,禁止盗版!");
}
//程序退出事件
void MainWindow::closeEvent(QCloseEvent *event)
{
if(QMessageBox::question(this,tr("退出程序"),tr("是否退出程序"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No)==QMessageBox::Yes)
{
event->accept();/*接受退出信号,程序退出*/
}else
{
event->ignore();/*忽略退出信号,程序继续运行*/
}
}