qt实现记事本功能

使用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();
}

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运用Qt开发工具来实现一个记事本,用户图形界面操作,通过Qt提供的图形库来设计记事本的管理、应用基本窗口,主要实现打开、新建、保存、另存为、查找、居中、字体、颜色等功能设计,同时在软件内部实现对于这些功能的支持和实现。还有对用户剪切、复制、粘贴、等功能的支持 核心代码讲解 1、打开文件功能 void MainWindow::on_action_2_triggered() { QString filename = QFileDialog::getOpenFileName(this); loadFile(filename); } 打开文件on_action_2_triggered()通过转到槽实现首先定义一个Qstring类型的filename让它获取你文件名,其次寻找它的本地连接。 3、保存文件功能 void MainWindow::on_action_3_triggered() { if(isSaved){ saveFile(curFile); } else{ do_file_SaveAS(); } } 保存文件功能on_action_3_triggered()通过转到槽实现,先判断bool型的isSaved是否是正确的,如果错则保存当前文件,否则执行另存为。 5、关闭功能 void MainWindow::on_action_5_triggered() { if(close){ QMessageBox::information(this,"提示","文件尚未保存,关闭"); }else{ ui->textEdit->close(); } } 关闭功能on_action_5_triggered()先对文本进行判断是否是关闭的,如果是错弹出对话框提示消息文件尚未保存,关闭,否则对文档进行关闭 11、查找功能 //查找 void MainWindow::close_findhangshu() { ui->gridLayoutWidget->close(); } close_findhangshu()对查找对话框进行关闭函数。 void MainWindow::on_action_11_triggered() { QDialog *closefind=new QDialog(this); QDialog *findDlg=new QDialog(this); find_textLineEdit=new QLineEdit(findDlg); QPushButton *find_Bth=new QPushButton(tr("查找下一个"),findDlg); QPushButton *close_find=new QPushButton(tr("关闭"),closefind); ui->gridLayout->addWidget(find_textLineEdit); ui->gridLayout->addWidget(find_Bth); ui->gridLayout->addWidget(close_find); connect(find_Bth,SIGNAL(clicked()),this,SLOT(show_findText())); connect(close_find,SIGNAL(clicked()),this,SLOT(close_findhangshu())); } 查找功能on_action_11_triggered()函数通过转到槽实现下面部分:先定义两个QDialog类型 指针变量*closefind和*findDlg,对它们进行新创建空间, find_textLineEdit=new QLineEdit(findDlg);新建一个空间对QlineEdit它进行传参,将findDlg传入目的是查找在文档中对应的文字,QPushButton *find_Bth=new QPushButton(tr("查找下一个"),findDlg);QPushButton *close_find=new QPushButton(tr("关闭"),closefind);这两个代码作用相同定义两个Qpubutton当点击查找功能弹出对话框时在对话框中实现查找下一个和关闭connect(find_Bth,SIGNAL(clicked()),this,SLOT(show_findText()));connect(close_find,SIGNAL(clicked()),this,SLOT(close_findhangshu()));点击查找下一个按钮时对它进行连接,执行show_findText()函数中的查找操作,如果点击关闭执行上述所说的close_findhangshu()关闭对话框操作 void MainWindow:: show_findText() { QString findText=find_textLineEdit->text(); if(!ui->textEdit->find(findText,QTextDocument::FindBackward)) { QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findText)); } } show_findText()此函数先定义QString类型 findText值,对文本进行扫描,如果文本不能找到找到则提示找不到。 本人也只是一个学生,记得点赞哦!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值