QT 多文档文本编辑器

QT 多文档文本编辑器_第1张图片
QT 多文档文本编辑器_第2张图片

一、设计目标

设计开发一个简单的多文档文本编辑器,具有新建、打开、保存一个文件、另存为一个文件、设置字体、设置字号、跨子窗口的剪切、复制、粘贴、查找、加粗、倾斜、下划线、设置字体颜色、左对齐、居中、右对齐、撤销、重做、窗口层叠、窗口平铺等功能。

二、设计思路

使用MainWindows类,创建一个主窗口,并且具有菜单栏和工具栏。使用MdiArea作为主窗口文件编辑的主要显示界面。自定义类childedit继承QTextEdit类,并且添加了保存为txt文件、打开txt文件并且读取到当前窗口中的功能。使用childedit类的对象作为子窗口添加到MdiArea中,并在其中对字体等进行各种各样的操作。
新建直接new创建一个childedit对象添加到MdiArea中。打开是使用QFile的对象通过getOpenFileName函数调用系统文件对话框,并且返回绝对路径给QFile对象。使用文件流将内容读取出来。保存和另存为也类似。
设置字体、颜色、字号、加粗、下划线、倾斜等功能均使用QTextCharFormat 对象获取字体格式,并且使用mergeCurrentCharFormat函数设置当前选中的文字的字体格式。

三、源代码

mainwindows.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include
#include
#include
#include
#include"childedit.h"
QT_BEGIN_NAMESPACE
namespace Ui {
      class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
     
    Q_OBJECT
    bool is_saved;
    bool is_MDI;
    bool Boldcheck;
    bool Italiccheck;
    bool UnderLinecheck;
    int childNumber;
    QDialog *newDialog;
    QLineEdit *line;
    QToolButton *undo;
    QToolButton *redo;

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    childEdit *activeEdit();

private:
    Ui::MainWindow *ui;
public slots:
    void Newfile();
    void Openfile();
    void Save();
    void Savefile(QString);
    void SaveAs();
    void Setfont(QString);
    void SetfontSize(int);
    void Font_Bold();
    void Font_Underline();
    void Font_Italic();
    void Setcolor();
    void Set_center();
    void Set_left();
    void Set_right();
    void Setfontfamily();
    void Search();
    void Find_text();
    void actionCascade();
    void actionTile();
    void actionMDI();
    void actionPrevious();
    void actionNext();
    void cut();
    void copy();
    void paste();
    void Undo();
    void Redo();
    void Close();
    void CloseAll();

};
#endif // MAINWINDOW_H

mainwindows.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"childwindows.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"childedit.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
     
    ui->setupUi(this);
    is_saved = false;
    is_MDI = true;
    Boldcheck = true;
    Italiccheck = true;
    UnderLinecheck = true;
    childNumber = 0;

    line = new QLineEdit(this);
    QPushButton *btn = new QPushButton(this);
    QHBoxLayout *layout = new QHBoxLayout(this);
    newDialog =new QDialog(this);
    btn->setText("搜索下一个");
    layout->addWidget(line);
    layout->addWidget(btn);
    newDialog->setLayout(layout);
    newDialog->setWindowTitle("查找");

    QFontComboBox *fontbox = new QFontComboBox(this);
    QSpinBox *fontsize = new QSpinBox(this);
    QToolButton *btn_Bold = new QToolButton(this);
    QToolButton *btn_Underline = new QToolButton(this);
    QToolButton *btn_Italic = new QToolButton(this);
    QToolButton *color = new QToolButton(this);
    QToolButton *center = new QToolButton(this);
    QToolButton *left = new QToolButton(this);
    QToolButton *right = new QToolButton(this);
    undo = new QToolButton(this);
    redo = new QToolButton(this);
    fontbox->setMinimumWidth(120);
    fontsize->setValue(13);

    btn_Bold->setText("加粗");
    btn_Underline->setText("下划线");
    btn_Italic->setText("倾斜");
    color->setText("颜色");
    center->setText("居中");
    left->setText("左对齐");
    right->setText("右对齐");
    undo->setText("撤销");
    redo->setText("重做");
    ui->actioncut->setShortcut(tr("Ctrl+X"));
    ui->actioncopy->setShortcut(tr("Ctrl+C"));
    ui->actionpaste->setShortcut(tr("Ctrl+V"));
    ui->actionsearch->setShortcut(tr("Ctrl+F"));
    ui->actionsave->setShortcut(tr("Ctrl+S"));

    ui->toolBar->addWidget(fontbox);
    ui->toolBar->addWidget(fontsize);
    ui->toolBar->addWidget(btn_Bold);
    ui->toolBar->addWidget(btn_Italic);
    ui->toolBar->addWidget(btn_Underline);
    ui->toolBar->addWidget(color);
    ui->toolBar->addWidget(left);
    ui->toolBar->addWidget(center);
    ui->toolBar->addWidget(right);
    ui->toolBar->addWidget(undo);
    ui->toolBar->addWidget(redo);

    ui->actionnew->setIcon(QIcon(":/res/xinjian.png"));
    ui->actionopen->setIcon(QIcon(":/res/dakai.png"));
    ui->actionsave->setIcon(QIcon(":/res/baocun.png"));
    btn_Bold->setIcon(QIcon(":/res/jiacu.png"));
    btn_Italic->setIcon(QIcon(":/res/xieti.png"));
    btn_Underline->setIcon(QIcon(":/res/xiahuaxian.png"));
    color->setIcon(QIcon(":/res/color.png"));
    ui->actioncut->setIcon(QIcon(":/res/jianqie.png"));
    ui->actioncopy->setIcon(QIcon(":/res/fuzhi.png"));
    ui->actionpaste->setIcon(QIcon(":/res/zhangtie.png"));
    ui->actionsearch->setIcon(QIcon(":/res/chazhao.jpg"));
    undo->setIcon(QIcon(":/res/chexiao.png"));
    redo->setIcon(QIcon(":/res/chongzuo.png"));
    left->setIcon(QIcon(":/res/left.png"));
    center->setIcon(QIcon(":/res/center.png"));
    right->setIcon(QIcon(":/res/right.png"));

    ui->mdiArea->setViewMode(QMdiArea::TabbedView);

    connect(ui->actionnew,SIGNAL(triggered()),this,SLOT(Newfile()));
    connect(ui->actionopen,SIGNAL(triggered()),this,SLOT(Openfile()));
    connect(ui->actionsave,SIGNAL(triggered()),this,SLOT(Save()));
    connect(ui->actionSaveas,SIGNAL(triggered()),this,SLOT(SaveAs()));
    connect(fontbox,SIGNAL(currentIndexChanged(QString)),this,SLOT(Setfont(QString)));
    connect(fontsize,SIGNAL(valueChanged(int)),this,SLOT(SetfontSize(int)));
    connect(btn_Bold,SIGNAL(clicked()),this,SLOT(Font_Bold()));
    connect(btn_Underline,SIGNAL(clicked()),this,SLOT(Font_Underline()));
    connect(btn_Italic,SIGNAL(clicked()),this,SLOT(Font_Italic()));
    connect(color,SIGNAL(clicked()),this,SLOT(Setcolor()));
    connect(center,SIGNAL(clicked()),this,SLOT(Set_center()));
    connect(left,SIGNAL(clicked()),this,SLOT(Set_left()));
    connect(right,SIGNAL(clicked()),this,SLOT(Set_right()));
    connect(undo,SIGNAL(clicked()),this,SLOT(Undo()));
    connect(redo,SIGNAL(clicked()),this,SLOT(Redo()));

    connect(ui->actioncut,SIGNAL(triggered()),this,SLOT(cut()));
    connect(ui->actioncopy,SIGNAL(triggered()),this,SLOT(copy()));
    connect(ui->actionpaste,SIGNAL(triggered()),this,SLOT(paste()));
    connect(ui->actioncolor,SIGNAL(triggered()),this,SLOT(Setcolor()));
    connect(ui->actionfont,SIGNAL(triggered()),this,SLOT(Setfontfamily()));
    connect(ui->actionsearch,SIGNAL(triggered()),this,SLOT(Search()));
    connect(btn,SIGNAL(clicked()),this,SLOT(Find_text()));

    connect(ui->actionclose,SIGNAL(triggered()),this,SLOT(Close()));
    connect(ui->actioncloseAll,SIGNAL(triggered()),this,SLOT(CloseAll()));
    connect(ui->actioncascade,SIGNAL(triggered()),this,SLOT(actionCascade()));
    connect(ui->actiontile,SIGNAL(triggered()),this,SLOT(actionTile()));
    connect(ui->actionmdi,SIGNAL(triggered()),this,SLOT(actionMDI()));
    connect(ui->actionprevious,SIGNAL(triggered()),this,SLOT(actionPrevious()));
    connect(ui->actionnext,SIGNAL(triggered()),this,SLOT(actionNext()));

}
MainWindow::~MainWindow()
{
     
    delete ui;
}
void MainWindow::Newfile()  //打开新窗口
{
     
    childEdit *edit = new childEdit(this);
    ui->mdiArea->addSubWindow(edit);
    edit->setFontPointSize(13);
    childNumber++;
    edit->setWindowTitle(tr("%1 未保存文件").arg(childNumber));
    edit->show();
}
void MainWindow::Openfile()     //打开文件
{
     
    Newfile();
    activeEdit()->OpenFile();
}
void MainWindow::Save()     //判断是否保存过文件
{
     
    activeEdit()->Save();
}
void MainWindow::SaveAs()   //另存为文件
{
     
    activeEdit()->SaveAs();
}
void MainWindow::Savefile(QString filename)     //保存文件
{
     
    QFile file(filename);
    file.open(QFile::WriteOnly | QFile::Text);
    QTextStream out(&file);
    out << activeEdit()->toPlainText();
}
void MainWindow::Setfont(QString fontStr)   //工具栏设置字体
{
     
    QTextCharFormat format;
    format.setFontFamily(fontStr);
    activeEdit()->mergeCurrentCharFormat(format);
}
void MainWindow::Setfontfamily()    //编辑框设置字体
{
     
    bool ifok;
    QFont font = QFontDialog::getFont(&ifok,this);
    QTextCharFormat format;
    format.setFont(font);
    activeEdit()->mergeCurrentCharFormat(format);
}
void MainWindow::SetfontSize(int FontSize)  //设置字号
{
     
    QTextCharFormat format;
    format.setFontPointSize(FontSize);
    activeEdit()->mergeCurrentCharFormat(format);
}
void MainWindow::Font_Bold()	//设置粗体
{
     
    QTextCharFormat format;
    format.setFontWeight(Boldcheck ? QFont::Bold : QFont::Normal);
    activeEdit()->mergeCurrentCharFormat(format);
    Boldcheck = !Boldcheck;
}
void MainWindow::Font_Underline()	//设置下划线
{
     
    QTextCharFormat format;
    format.setFontUnderline(UnderLinecheck);
    activeEdit()->mergeCurrentCharFormat(format);
    UnderLinecheck = !UnderLinecheck;
}
void MainWindow::Font_Italic()	//设置倾斜
{
     
    QTextCharFormat format;
    format.setFontItalic(Italiccheck);
    activeEdit()->mergeCurrentCharFormat(format);
    Italiccheck = !Italiccheck;
}
void MainWindow::Setcolor()	//设置颜色
{
     
    QColor color = QColorDialog::getColor(Qt::red, this);
    QTextCharFormat format;
    format.setForeground(color);
    activeEdit()->mergeCurrentCharFormat(format);
}
void MainWindow::Set_center()	//居中
{
     
    activeEdit()->setAlignment(Qt::AlignCenter);
}
void MainWindow::Set_left()		//左对齐
{
     
    activeEdit()->setAlignment(Qt::AlignLeft);
}
void MainWindow::Set_right()	//右对齐
{
     
    activeEdit()->setAlignment(Qt::AlignRight);
}
void MainWindow::Search()	//搜索
{
     
    newDialog->show();
}
void MainWindow::Find_text()
{
     
    QString findtext=line->text();
    if(activeEdit()->find(findtext,QTextDocument::FindBackward))
    {
     
        QPalette palette = activeEdit()->palette();
        palette.setColor(QPalette::Highlight,palette.color(QPalette::Active,QPalette::Highlight));
        activeEdit()->setPalette(palette);
    }
    else
    {
     
        QMessageBox::information(this,tr("注意"),tr("没有找到内容"),QMessageBox::Ok);
    }
}
childEdit *MainWindow::activeEdit()		//返回当前活动窗口指针
{
     
    if(QMdiSubWindow * window = ui->mdiArea->activeSubWindow()){
     
        return qobject_cast<childEdit *>(window->widget());
    }
    return 0;
}
void MainWindow::actionCascade()	//窗口层叠
{
     
    ui->mdiArea->cascadeSubWindows();
}
void MainWindow::actionTile()	//窗口平铺
{
     
    ui->mdiArea->tileSubWindows();
}
void MainWindow::actionMDI()
{
     
    if(is_MDI){
     
        ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
        is_MDI = false;
    }
    else{
     
        ui->mdiArea->setViewMode(QMdiArea::TabbedView);
        is_MDI = true;
    }
}
void MainWindow::actionPrevious()
{
     
    ui->mdiArea->activatePreviousSubWindow();
}
void MainWindow::actionNext()
{
     
    ui->mdiArea->activateNextSubWindow();
}
void MainWindow::cut()
{
     
    if(activeEdit()){
     
        activeEdit()->cut();
    }
}
void MainWindow::copy()
{
     
    if(activeEdit()){
     
        activeEdit()->copy();
    }
}
void MainWindow::paste()
{
     
    if(activeEdit()){
     
        activeEdit()->paste();
    }
}
void MainWindow::Undo()		//撤销
{
     
    if(activeEdit()){
     
        activeEdit()->undo();
    }
}
void MainWindow::Redo()		//重做
{
     
    if(activeEdit()){
     
        activeEdit()->redo();
    }
}
void MainWindow::Close()
{
     
    ui->mdiArea->closeActiveSubWindow();
}
void MainWindow::CloseAll()
{
     
    ui->mdiArea->closeAllSubWindows();
}

childedit.h

#ifndef CHILDEDIT_H
#define CHILDEDIT_H

#include 

class childEdit : public QTextEdit
{
     
    Q_OBJECT
public:
    explicit childEdit(QWidget *parent = nullptr);
    bool is_saved;
    QString lastfile;

    void OpenFile();
    void Save();
    void Loadfile(QString);
    void SaveAs();
    void Savefile(QString);

signals:

};

#endif // CHILDEDIT_H

childedit.cpp

#include "childedit.h"
#include
#include
#include
childEdit::childEdit(QWidget *parent) : QTextEdit(parent)
{
     
    setAttribute(Qt::WA_DeleteOnClose);
}
void childEdit::OpenFile()	//打开文件
{
     
    QString filename = QFileDialog::getOpenFileName(this,tr("文件打开"),"D:/Downloads",tr("*.*"));
    if(!filename.isEmpty()){
     
        Loadfile(filename);
        setWindowTitle(filename);
    }
}
void childEdit::Save()	//保存文件
{
     
    if(!is_saved){
     
        lastfile = QFileDialog::getSaveFileName(this,tr("文件保存"),"D:/Downloads",tr("TextFiles(*.txt)"));
        is_saved = true;
        setWindowTitle(lastfile);
        Savefile(lastfile);
    }
    else{
     
        Savefile(lastfile);
    }
}
void childEdit::Loadfile(QString filename)	//加载文件
{
     
    QFile file(filename);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("utf-8");
    setPlainText(in.readAll());
}
void childEdit::SaveAs()
{
     
    QString filename = QFileDialog::getSaveFileName(this,tr("file open"),"D:/Downloads",tr("TextFiles(*.txt)"));
    Savefile(filename);
}
void childEdit::Savefile(QString filename)
{
     
    QFile file(filename);
    file.open(QFile::WriteOnly | QFile::Text);
    QTextStream out(&file);
    out.setCodec("utf-8");
    out <<toPlainText();
}

你可能感兴趣的:(QT 多文档文本编辑器)