使用Qt creator 实现记事本功能
.h代码如下:
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QLineEdit>
#include <QTextEdit>
#include <QVBoxLayout>
class notepad : public QMainWindow
{
Q_OBJECT
public:
notepad(QWidget *parent = 0);
~notepad();
private:
//新建
QMenu *fileMenu, *editMenu, *helpMenu,*setMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar; //新建两个工具栏,用以存放四个菜单动作
QAction *newAct;
QAction *openAct;
QAction *saveAct;
QAction *saveasAct;
QAction *printAct;
QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;
QAction *undoAct;
QAction *redoAct;
QAction *aboutQtAct;
QAction *exitAct;
QAction *fontAct;
QAction *colorAct;
QTextEdit *textEdit;
QLineEdit *lineEdit;
QVBoxLayout *vboxlayout;
private slots:
void newFile();
void openFile();
void saveFile();
void saveasFile();
void printFile();
void cutAction();
void copyAction();
void pasteAction();
void undoAction();
void redoAction();
void exitAction();
void fontset();
void colorset();
};
#endif // NOTEPAD_H
.
cpp代码:
#include "notepad.h"
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QIcon>
#include<QApplication>
#include<QFontDialog>
#include<QColorDialog>
#include<QFontDialog>
notepad::notepad(QWidget *parent)
: QMainWindow(parent)
{
this->resize(500,400);
this->setWindowTitle("记事本");
this->setWindowIcon(QIcon("://file/notepad.png"));
//实例菜单和控件
fileMenu = new QMenu(this);
editMenu = new QMenu(this);
helpMenu = new QMenu(this);
setMenu = new QMenu(this);
textEdit = new QTextEdit(this);
vboxlayout = new QVBoxLayout(this);
this->setCentralWidget(textEdit);
//填充菜单子节点
newAct = new QAction(QIcon("://file/new.png"), tr( "新建" ), this );
newAct->setShortcut(tr("Ctrl+N" )); //快捷键
newAct->setStatusTip(tr("新建文件" )); //底部状态提示
openAct = new QAction(QIcon("://file/open.png"), tr( "打开" ), this );
openAct->setShortcut(tr("Ctrl+O" ));
openAct->setStatusTip(tr("打开文件" ));
saveAct = new QAction(QIcon("://file/save.png" ), tr( "保存" ), this );
saveAct->setShortcut(tr("Ctrl+S" ));
saveAct->setStatusTip(tr("保存文件" ));
saveasAct = new QAction(QIcon("://file/savese.png"), tr( "另存为" ), this );
saveasAct->setShortcut(tr("Ctrl+Shift+S" ));
saveasAct->setStatusTip(tr("另存为文件" ));
printAct = new QAction(QIcon("://file/print.png"), tr( "打印" ), this );
printAct->setShortcut(tr("Ctrl+P" ));
printAct->setStatusTip(tr("打印" ));
exitAct = new QAction(QIcon("://file/quie.png"), tr( "退出" ), this );
exitAct->setShortcut(tr("Ctrl+E"));
exitAct->setStatusTip(tr("退出"));
fontAct = new QAction(QIcon(),tr("字体"),this);
fontAct->setStatusTip("字体设置");
colorAct = new QAction(QIcon(),tr("颜色"),this);
colorAct->setStatusTip("颜色设置");
//信号与槽
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
connect(openAct,SIGNAL(triggered()),this,SLOT(openFile()));
connect(saveAct,SIGNAL(triggered()),this,SLOT(saveFile()));
connect(saveasAct,SIGNAL(triggered()),this,SLOT(saveasFile()));
connect(printAct,SIGNAL(triggered()),this,SLOT(printFile()));
connect(fontAct,SIGNAL(triggered()),this,SLOT(fontset()));
connect(colorAct,SIGNAL(triggered()),this,SLOT(colorset()));
cutAct = new QAction(QIcon(), tr( "剪切" ), this );
cutAct->setShortcut(tr("Ctrl+X" ));
cutAct->setStatusTip(tr("剪切内容" ));
copyAct = new QAction(QIcon("://file/copy.png"), tr( "复制" ), this );
copyAct->setShortcut(tr("Ctrl+C" ));
copyAct->setStatusTip(tr("复制内容" ));
pasteAct = new QAction(QIcon( ), tr( "粘贴" ), this );
pasteAct->setShortcut(tr("Ctrl+V" ));
pasteAct->setStatusTip(tr("粘贴内容" ));
undoAct = new QAction(QIcon(), tr( "向后" ), this );
undoAct->setShortcut(tr("Ctrl+Z" ));
undoAct->setStatusTip(tr("向后一步" ));
redoAct = new QAction(QIcon("://file/forward.png"), tr( "向前" ), this );
redoAct->setShortcut(tr("Ctrl+Y" ));
redoAct->setStatusTip(tr("向前一步" ));
aboutQtAct = new QAction(QIcon(),tr( "关于 Qt" ), this );
aboutQtAct->setStatusTip(tr("关于Qt信息" ));
//信号与槽
connect(copyAct, SIGNAL(triggered()), this, SLOT(copyAction()));
connect(cutAct, SIGNAL(triggered()), this, SLOT(cutAction()));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(pasteAction()));
connect(undoAct, SIGNAL(triggered()), this, SLOT(undoAction()));
connect(redoAct, SIGNAL(triggered()), this, SLOT(redoAction()));
connect(aboutQtAct,SIGNAL(trigerred()),this,SLOT(aboutQt()));// connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(exitAct, SIGNAL(triggered()), this, SLOT(exitAction()));
//填充菜单
fileMenu = menuBar()->addMenu(tr( "文件" ));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveasAct);
fileMenu->addAction(exitAct);
fileMenu->addSeparator();
editMenu = menuBar()->addMenu(tr("编辑" ));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
menuBar()->addSeparator();
setMenu = menuBar()->addMenu("格式设置");
setMenu->addAction(fontAct);
setMenu->addAction(colorAct);
helpMenu = menuBar()->addMenu(tr("帮助" ));
helpMenu->addAction(aboutQtAct);
//toolBar 工具条
//fileTooBar添加
fileToolBar = addToolBar(tr("新建")); //右键添加改变
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
fileToolBar->addAction(saveasAct);
fileToolBar->addAction(printAct);
//editToolBar添加
editToolBar = addToolBar(tr("修改"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
editToolBar->addAction(undoAct);
editToolBar->addAction(redoAct);
editToolBar->addAction(exitAct);
}
notepad::~notepad()
{
}
/*槽函数实现*/
//子菜单事件
void notepad::newFile()
{
/*
*
QMessageBox::warning(this,tr("Warning"),
tr("创建新文件?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No);
*
*/
textEdit->clear();
textEdit->setText(QString());
}
void notepad::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "打开文件");
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
textEdit->setText(text);
file.close();
}
void notepad::saveFile()
{
QString fileName;
// 若没有文件,重新创建一个
if (fileName.isEmpty()) {
fileName = QFileDialog::getSaveFileName(this, "Save");
//currentFile = fileName;
} else {
//fileName = currentFile;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = textEdit->toPlainText();
out << text;
file.close();
}
void notepad::saveasFile()
{
QString fileName = QFileDialog::getSaveFileName(this, "另存为");
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
//currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = textEdit->toPlainText();
out << text;
file.close();
}
void notepad::fontset(){
bool ok;
QFont currentFont=QFontDialog::getFont(&ok);
if(ok){
textEdit->setFont(currentFont);
}
}
void notepad::colorset(){
QColor currentcolor=QColorDialog::getColor(Qt::black,this);
if(currentcolor.isValid()){
textEdit->setTextColor(currentcolor);
}
}
void notepad::printFile()
{
//暂时空
/*
#if QT_CONFIG(printer)
QPrinter printDev;
#if QT_CONFIG(printdialog)
QPrintDialog dialog(&printDev, this);
if (dialog.exec() == QDialog::Rejected)
return;
#endif // QT_CONFIG(printdialog)
ui->textEdit->print(&printDev);
#endif // QT_CONFIG(printer)
*/
}
void notepad::cutAction()
{
textEdit->cut();
}
void notepad::pasteAction()
{
textEdit->paste();
}
void notepad::copyAction()
{
textEdit->copy();
}
void notepad::undoAction()
{
textEdit->undo();
}
void notepad::redoAction()
{
textEdit->redo();
}
void notepad::exitAction()
{
this->close();
}