Qt5项目实现功能丰富的文本编辑器Plus(实现部分wps功能)

1. 前言

这次花了大约4天的时间,去做了这个文本编辑器Plus,这个编辑器主要是为了让我去熟悉一些常用的操作,以及信号槽的使用,我上次做的简易版文本编辑器地址:Qt5实现文本编辑器

2. 主界面展示

在这里插入图片描述
界面演示了部分功能

3. 功能介绍

3.1 文件操作功能

  • 新建文件
  • 打开文件
  • 打印文件
  • 打印图片

3.2 图像坐标变换

  • 放大功能
  • 缩小功能
  • 旋转功能(90°,180°,270°)
  • 纵向镜像
  • 水平镜像

3.3 文本编辑功能

  • 设置字体
  • 设置字号
  • 设置文字加粗
  • 设置文字斜体
  • 设置文字下划线
  • 设置文字颜色
  • 设置字符格式
  • 撤销
  • 重做
  • 复制
  • 粘贴
  • 剪切

3.4 排版功能

  • 实现段落对齐(左对齐,右对齐,居中,两端对齐)
  • 实现文本排序
  • 实现自动生成列表

4. 代码展示

4.2 头文件

imgprocessor.h

#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H

#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QColorDialog>
#include <QTextList>
#include "showwidget.h"
class ImgProcessor : public QMainWindow {
    Q_OBJECT

public:
    ImgProcessor(QWidget* parent = nullptr);
    ~ImgProcessor();
    void createActions();//创建动作
    void createMenus();
    void createToolBars();
    void loadFile(QString fileName);
    void mergeFormat(QTextCharFormat format);

protected slots:
    void aboutQt();
    void ShowNewFile();
    void ShowOpenFile();
    // 打印文本
    void ShowPrintText();
    // 打印图像
    void ShowPrintImage();

    // 缩放功能
    void ShowZoomIn();
    void ShowZoomOut();

    // 旋转
    void ShowRotate90();
    void ShowRotate180();
    void ShowRotate270();

    // 镜像功能
    void ShowMirrorVertical();
    void ShowMirrorHorizontal();

    // 设置选定文字字体
    void ShowFontComboBox(QString comboStr);
    void ShowSizeSpinBox(QString spinValue);
    void ShowBoldBtn();
    void ShowItalicBtn();
    void ShowUnderlineBtn();
    void ShowColorBtn();
    void ShowCurrentFormatChanged(const QTextCharFormat& fmt);

    // 排版功能
    void ShowList(int index);
    void ShowAlignment(QAction* act);
    void ShowCursorPositionChanged();

private:
    // 各项菜单栏
    // 这里对应下面的文件,编辑,旋转...
    QMenu* fileMenu;
    QMenu* zoomMenu;
    QMenu* rotateMenu;
    QMenu* mirrorMenu;
    QImage img;
    QString fileName;

    // 自定义窗口
    ShowWidget* showWidget;

    // 文件菜单栏
    QAction* openFileAction;
    QAction* NewFileAction;
    QAction* PrintTextAction;
    QAction* PrintImageAction;
    QAction* exitAction;

    // 编辑菜单栏
    QAction* copyAction;
    QAction* cutAction;
    QAction* pasteAction;
    QAction* aboutAction;
    QAction* zoomInAction;
    QAction* zoomOutAction;

    // 旋转菜单栏
    QAction* rotate90Action;
    QAction* rotate180Action;
    QAction* rotate270Action;

    // 镜像菜单栏
    QAction* mirrorVerticalAction;
    QAction* mirrorHorizontalcutAction;
    QAction* undoAction;
    QAction* redoAction;

    // 工具栏
    QToolBar* fileTool;
    QToolBar* zoomTool;
    QToolBar* rotateTool;
    QToolBar* mirrorTool;
    QToolBar* doToolBar;

    QLabel* fontLabel1;
    QFontComboBox* fontComboBox;
    QLabel* fontLabel2;
    QComboBox* sizeComboBox;
    QToolButton* boldBtn;
    QToolButton* italicBtn;
    QToolButton* underlineBtn;
    QToolButton* colorBtn;
    QToolBar* fontToolBar;

    // 排序设置项
    QLabel* listLabel;
    QComboBox* listComboBox;
    QActionGroup* actGrp;
    QAction* leftAction;
    QAction* rightAction;
    QAction* centerAction;
    QAction* justifyAction;
    QToolBar* listToolBar; // 排序工具栏






};
#endif // IMGPROCESSOR_H

showwidget.h

#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QImage>
class ShowWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ShowWidget(QWidget *parent = nullptr);
    QImage img;
    QLabel* imageLabel;
    QTextEdit* text;
signals:

public slots:

};

#endif // SHOWWIDGET_H

4.2 源文件

imgprocessor.cpp

#include "imgprocessor.h"
#include <QFileDialog>
#include <QTextStream>
ImgProcessor::ImgProcessor(QWidget* parent)
    : QMainWindow(parent) {
    setWindowTitle(tr("Easy word"));
    showWidget = new ShowWidget (this);
    setCentralWidget(showWidget);
    // 排序
    listLabel = new QLabel(tr("排序"));
    listComboBox = new QComboBox;
    listComboBox->addItem("Standard");
    listComboBox->addItem("QTextListFormat::ListDisc");
    listComboBox->addItem("QTextListFormat::ListCircle");
    listComboBox->addItem("QTextListFormat::ListSquare");
    listComboBox->addItem("QTextListFormat::ListDecimal");
    listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    listComboBox->addItem("QTextListFormat::ListUpperAlpha");
    listComboBox->addItem("QTextListFormat::ListLowerRoman");
    listComboBox->addItem("QTextListFormat::ListUpperRoman");
    // 在工具栏上嵌入控件
    // 设置字体
    fontLabel1 = new QLabel(tr("字体"));
    fontComboBox = new QFontComboBox;
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
    fontLabel2 = new QLabel(tr("字号"));
    sizeComboBox = new QComboBox;
    QFontDatabase db;
    foreach(int size, db.standardSizes()) {
        sizeComboBox->addItem(QString::number(size));
    }
    boldBtn = new QToolButton;
    boldBtn->setIcon(QIcon(":/resouses/Bold.jpg"));
    boldBtn->setCheckable(true);
    italicBtn = new QToolButton;
    italicBtn->setIcon(QIcon(":/resouses/xieti.jfif"));
    italicBtn->setCheckable(true);
    underlineBtn = new QToolButton;
    underlineBtn->setIcon(QIcon(":/resouses/underline.jfif"));
    underlineBtn->setCheckable(true);
    colorBtn = new QToolButton;
    colorBtn->setIcon(QIcon(":/resouses/color.jfif"));
    colorBtn->setCheckable(true);
    // 创建动作,菜单,工具栏的函数
    createActions();
    createMenus();
    createToolBars();
    if(img.load(":/resouses/dog.jpg")) {
        //放置图片
        showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    }
    connect(fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString)));
    connect(sizeComboBox, SIGNAL(activated (QString)), this, SLOT (ShowSizeSpinBox(QString)));
    connect (boldBtn, SIGNAL (clicked()), this, SLOT (ShowBoldBtn ()));
    connect (italicBtn, SIGNAL (clicked ()), this, SLOT (ShowItalicBtn()));
    connect (underlineBtn, SIGNAL(clicked()), this, SLOT (ShowUnderlineBtn()));
    connect(colorBtn, SIGNAL(clicked()), this, SLOT (ShowColorBtn()));
    connect (showWidget->text, SIGNAL(currentCharFormatChanged(QTextCharFormat&)),
             this, SLOT(ShowCurrentFormatChanged(QTextCharFormat&)));
    connect(listComboBox, SIGNAL(activated(int)), this, SLOT(ShowList(int)));
    connect(showWidget->text->document(), SIGNAL(undoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text->document(), SIGNAL(redoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
    connect(showWidget->text, SIGNAL(cursorPositionChanged()), this, SLOT(ShowCursorPositionChanged()));
}

ImgProcessor::~ImgProcessor() {
}

void ImgProcessor::createActions() {
    // 打开
    openFileAction = new QAction(QIcon(":/resouses/open.jpg"), tr("打开"), this);
    openFileAction->setShortcut(tr("Ctrl+O"));
    openFileAction->setStatusTip(tr("打开一个文件"));
    connect(openFileAction, &QAction::triggered, this, &ImgProcessor::ShowOpenFile);
    // 新建
    NewFileAction = new QAction(QIcon(":/resouses/new.jfif"), tr("新建"), this);
    NewFileAction->setShortcut(tr("Ctrl+N"));
    NewFileAction->setStatusTip(tr("新建一个文件"));
    connect(NewFileAction, &QAction::triggered, this, &ImgProcessor::ShowNewFile);
    // 退出
    exitAction = new QAction(QIcon(":/resouses/exit.jfif"), tr("退出"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("退出程序"));
    connect(exitAction, &QAction::triggered, this, &ImgProcessor::close);
    // 复制
    copyAction = new QAction(QIcon(":/resouses/copy.jpg"), tr("复制"), this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("复制文件"));
    connect(copyAction, &QAction::triggered, showWidget->text, &QTextEdit::copy);
    // 剪切
    cutAction = new QAction(QIcon(":/resouses/cut1.jpg"), tr("剪切"), this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切文件"));
    connect(cutAction, &QAction::triggered, showWidget->text, &QTextEdit::cut);
    // 粘贴
    pasteAction = new QAction(QIcon(":/resouses/paste.jpg"), tr("粘贴"), this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("粘贴文件"));
    connect(pasteAction, &QAction::triggered, showWidget->text, &QTextEdit::paste);
    // 关于
    aboutAction = new QAction(tr("关于"), this);
    connect(aboutAction, &QAction::triggered, this, &ImgProcessor::aboutQt);
    // 打印文本
    PrintTextAction = new QAction(QIcon(":/resouses/printText.jpg"), tr("打印文本"), this);
    PrintTextAction->setStatusTip(tr("打印一个文本"));
    connect(PrintTextAction, &QAction::triggered, this, &ImgProcessor::ShowPrintText);
    // 打印图片
    PrintImageAction = new QAction(QIcon(":/resouses/printImage.jpg"), tr("打印图片"), this);
    PrintImageAction->setStatusTip(tr("打印一幅图片"));
    connect(PrintImageAction, &QAction::triggered, this, &ImgProcessor::ShowPrintImage);
    // 放大
    zoomInAction = new QAction(QIcon(":/resouses/zoomin.jfif"), tr("放大"), this);
    zoomInAction->setStatusTip(tr("放大一幅图片"));
    connect(zoomInAction, &QAction::triggered, this, &ImgProcessor::ShowZoomIn);
    // 缩 小
    zoomOutAction = new QAction(QIcon(":/resouses/zoomout.jfif"), tr("缩小"), this);
    zoomOutAction->setStatusTip(tr("缩小一幅图片"));
    connect(zoomOutAction, &QAction::triggered, this, &ImgProcessor::ShowZoomOut);
    // 旋转
    // 90度
    rotate90Action = new QAction(QIcon(":/resouses/rotate90.jfif"), tr("旋转90度"), this);
    rotate90Action->setToolTip(tr("将一幅图片旋转90度"));
    connect(rotate90Action, &QAction::triggered, this, &ImgProcessor::ShowRotate90);
    // 180度
    rotate180Action = new QAction(QIcon(":/resouses/rotate180.jfif"), tr("旋转180度"), this);
    rotate180Action->setToolTip(tr("将一幅图片旋转180度"));
    connect(rotate180Action, &QAction::triggered, this, &ImgProcessor::ShowRotate180);
    // 270度
    rotate270Action = new QAction(QIcon(":/resouses/rotate270.jfif"), tr("旋转270度"), this);
    rotate270Action->setToolTip(tr("将一幅图片旋转270度"));
    connect(rotate270Action, &QAction::triggered, this, &ImgProcessor::ShowRotate270);
    // 实现图片镜像的动作
    // 纵向镜像
    mirrorVerticalAction = new QAction(QIcon(":/resouses/mirrorVertical.jfif"), tr("纵向镜像"), this);
    mirrorVerticalAction->setStatusTip(tr("对图片纵向镜像"));
    connect(mirrorVerticalAction, &QAction::triggered, this, &ImgProcessor::ShowMirrorVertical);
    // 横向镜像
    mirrorHorizontalcutAction = new QAction(QIcon(":/resouses/mirrorHorizontal.jfif"), tr("横向镜像"), this);
    mirrorHorizontalcutAction->setStatusTip(tr("对图片横向镜像"));
    connect(mirrorHorizontalcutAction, &QAction::triggered, this, &ImgProcessor::ShowMirrorHorizontal);
    // 实现撤销和重做
    // libpng warning: sBIT: invalid
    undoAction = new QAction(QIcon(":/resouses/left.jpg"), tr("撤销"), this);
    connect(undoAction, &QAction::triggered, showWidget->text, &QTextEdit::undo);
    redoAction = new QAction(QIcon(":/resouses/right.jpg"), tr("重做"), this);
    connect(redoAction, &QAction::triggered, showWidget->text, &QTextEdit::redo);
    // 排序: 左对齐,右对齐,居中和两端对齐
    actGrp = new QActionGroup(this);
    leftAction = new QAction(QIcon(":/resouses/zuoduiqi.jfif"), "左对齐", actGrp);
    leftAction->setCheckable(true);
    rightAction = new QAction(QIcon(":/resouses/youduiqi.jfif"), "右对齐", actGrp);
    rightAction->setCheckable(true);
    centerAction = new QAction(QIcon(":/resouses/juzhong.jfif"), "居中", actGrp);
    centerAction->setCheckable(true);
    justifyAction = new QAction(QIcon(":/resouses/juzhong.jfif"), "两端对齐", actGrp);
    justifyAction->setCheckable(true);
    connect(actGrp, SIGNAL(triggered(QAction*)), this, SLOT(ShowAlignment(QAction*)));
}

void ImgProcessor::createMenus() {
    // 文件菜单
    fileMenu = menuBar()->addMenu(tr("文件"));
    fileMenu->addAction(openFileAction);
    fileMenu->addAction(NewFileAction);
    fileMenu->addAction(PrintTextAction);
    fileMenu->addAction(PrintImageAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);
    // 缩放菜单
    zoomMenu = menuBar()->addMenu(tr("编辑"));
    zoomMenu->addAction(copyAction);
    zoomMenu->addAction(cutAction);
    zoomMenu->addAction(pasteAction);
    zoomMenu->addAction(aboutAction);
    zoomMenu->addSeparator();
    zoomMenu->addAction(zoomInAction);
    zoomMenu->addAction(zoomOutAction);
    // 镜像菜单
    mirrorMenu = menuBar()->addMenu(tr("镜像"));
    mirrorMenu->addAction(mirrorVerticalAction);
    mirrorMenu->addAction(mirrorHorizontalcutAction);
}

void ImgProcessor::createToolBars() {
    // 文件
    fileTool = addToolBar("File");
    fileTool->addAction(openFileAction);
    fileTool->addAction(NewFileAction);
    fileTool->addAction(PrintTextAction);
    fileTool->addAction(PrintImageAction);
    // 工具条
    zoomTool = addToolBar("Edit");
    zoomTool->addAction(copyAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(pasteAction);
    zoomTool->addSeparator();
    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction);
    // 旋转
    rotateTool = addToolBar("rotate");
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action);
    // undo redo
    doToolBar = addToolBar("doEdit");
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction);
    //字体工具条
    fontToolBar = addToolBar ("Font");
    fontToolBar->addWidget(fontLabel1);
    fontToolBar->addWidget (fontComboBox);
    fontToolBar->addWidget(fontLabel2);
    fontToolBar->addWidget (sizeComboBox);
    fontToolBar->addSeparator();
    fontToolBar->addWidget (boldBtn);
    fontToolBar->addWidget(italicBtn);
    fontToolBar->addWidget (underlineBtn);
    fontToolBar->addSeparator();
    fontToolBar->addWidget (colorBtn);
    // 排序工具条
    listToolBar = addToolBar("list");
    listToolBar->addWidget(listLabel);
    listToolBar->addWidget(listComboBox);
    listToolBar->addSeparator();
    listToolBar->addActions(actGrp->actions());
}

// 读取文件
void ImgProcessor::loadFile(QString fileName) {
    printf("file name: %s\n", (char*)(fileName.data()));
    QFile file(fileName);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream textStream(&file);
        while(!textStream.atEnd()) {
            showWidget->text->append(textStream.readLine());
            printf("read line\n");
        }
        printf("end\n");
    } else {
        printf("load File error");
    }
}


void ImgProcessor::aboutQt() {
}

void ImgProcessor::ShowNewFile() {
    ImgProcessor* newImgProcessor = new ImgProcessor;
    newImgProcessor->show();
}


void ImgProcessor::ShowOpenFile() {
    fileName = QFileDialog::getOpenFileName(this);
    if(!fileName.isEmpty()) {
        if(showWidget->text->document()->isEmpty()) {
            loadFile(fileName);
        } else {
            ImgProcessor* newImgProcessor = new ImgProcessor;
            newImgProcessor->show();
            newImgProcessor->loadFile(fileName);
        }
    }
}

void ImgProcessor::ShowPrintText() {
    QPrinter printer;
    QPrintDialog printDialog(&printer, this);
    // 判断标准打印对话框显示后用户是否单机“打印按钮”
    // 如果用户单击“取消”按钮,则不执行后续操作
    if(printDialog.exec()) {
        QTextDocument* doc = showWidget->text->document();
        doc->print(&printer);
    }
}

void ImgProcessor::ShowPrintImage() {
    QPrinter printer;
    QPrintDialog printDialog(&printer, this);
    if(printDialog.exec()) {
        QPainter painter(&printer);
        QRect rect = painter.viewport();
        QSize size = img.size();
        // 按照图形的比例大小,重新设置视图矩形区域
        size.scale(rect.size(), Qt::KeepAspectRatio);
        painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
        painter.setWindow(img.rect());
        painter.drawImage(0, 0, img);
    }
}

// 放大
void ImgProcessor::ShowZoomIn() {
    if(img.isNull()) {
        return;
    }
    QMatrix matrix;
    // 按照两倍比例对水平和垂直方向进行放大,并将当前显示的图形按照该坐标矩阵进行转换
    matrix.scale(2, 2);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowZoomOut() {
    if(img.isNull()) {
        return;
    }
    QMatrix matrix;
    // 同理缩小
    matrix.scale(0.5, 0.5);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate90() {
    if(img.isNull()) {
        return;
    }
    QMatrix matrix;
    matrix.rotate(90);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate180() {
    if(img.isNull()) {
        return;
    }
    QMatrix matrix;
    matrix.rotate(180);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate270() {
    if(img.isNull()) {
        return;
    }
    QMatrix matrix;
    matrix.rotate(270);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowMirrorVertical() {
    if(img.isNull()) {
        return;
    }
    // 第一个参数是水平,第二个是纵向
    img = img.mirrored(false, true);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowMirrorHorizontal() {
    if(img.isNull()) {
        return;
    }
    // 第一个参数是水平,第二个是纵向
    img = img.mirrored(true, false);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

// 设置选定文字字体
void ImgProcessor::ShowFontComboBox(QString comboStr) {
    QTextCharFormat fmt;
    // 选择字体名称设置给QTextCharFormat对象
    fmt.setFontFamily(comboStr);
    // 将新的格式运用到光标选区内的字符
    mergeFormat(fmt);
}

// 对QTextDocument进行的修改都通过QTextCursor类来完成
void ImgProcessor::mergeFormat(QTextCharFormat format) {
    QTextCursor cursor = showWidget->text->textCursor();
    // 若光标没有高量选区,则将光标所在处的词作为选取
    if(!cursor.hasSelection()) {
        cursor.select(QTextCursor::WordUnderCursor);
    }
    cursor.mergeCharFormat(format);
    showWidget->text->mergeCurrentCharFormat(format);
}


void ImgProcessor::ShowSizeSpinBox(QString spinValue) {
    QTextCharFormat fmt;
    fmt.setFontPointSize(spinValue.toFloat());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowBoldBtn() {
    QTextCharFormat fmt;
    fmt.setFontWeight(boldBtn->isChecked() ? QFont::Bold : QFont::Normal);
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowItalicBtn() {
    QTextCharFormat fmt;
    fmt.setFontItalic(italicBtn->isChecked());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowUnderlineBtn() {
    QTextCharFormat fmt;
    fmt.setFontUnderline(underlineBtn->isChecked());
    showWidget->text->mergeCurrentCharFormat(fmt);
}

void ImgProcessor::ShowColorBtn() {
    QColor color = QColorDialog::getColor(Qt::red, this);
    if(color.isValid()) {
        QTextCharFormat fmt;
        fmt.setForeground(color);
        showWidget->text->mergeCurrentCharFormat(fmt);
    }
}

// 当光标所在处的字符格式发生变化时调用这个槽函数,函数根据新的字符格式将工具栏上各个格式控件的显示更新
void ImgProcessor::ShowCurrentFormatChanged(const QTextCharFormat& fmt) {
    fontComboBox->setCurrentIndex (fontComboBox->findText (fmt.fontFamily()));
    sizeComboBox->setCurrentIndex (sizeComboBox->findText(QString::number(fmt.fontPointSize())));
    boldBtn->setChecked (fmt.font ().bold());
    italicBtn->setChecked (fmt.fontItalic());
    underlineBtn->setChecked(fmt.fontUnderline());
}

void ImgProcessor::ShowList(int index) {
    // 获得编辑框的QTextCursor指针
    QTextCursor cursor = showWidget->text->textCursor();
    if(index != 0) {
        QTextListFormat::Style style = QTextListFormat::ListDisc;
        switch(index) {
            default:
            case 1:
                style = QTextListFormat::ListDisc;
                break;
            case 2:
                style = QTextListFormat::ListCircle;
                break;
            case 3:
                style = QTextListFormat::ListSquare;
                break;
            case 4:
                style = QTextListFormat::ListDecimal;
                break;
            case 5:
                style = QTextListFormat::ListLowerAlpha;
                break;
            case 6:
                style = QTextListFormat::ListUpperAlpha;
                break;
            case 7:
                style = QTextListFormat::ListLowerRoman;
                break;
            case 8:
                style = QTextListFormat::ListUpperRoman;
                break;
        }
        // 设置缩进
        cursor.beginEditBlock();
        QTextBlockFormat blockFmt = cursor.blockFormat();
        QTextListFormat listFmt;
        if(cursor.currentList()) {
            listFmt = cursor.currentList()->format();
        } else {
            listFmt.setIndent(blockFmt.indent() + 1);
            blockFmt.setIndent(0);
            cursor.setBlockFormat(blockFmt);
        }
        listFmt.setStyle(style);
        cursor.createList(listFmt);
        cursor.endEditBlock();
    } else {
        QTextBlockFormat bfmt;
        bfmt.setObjectIndex(-1);
        cursor.mergeBlockFormat(bfmt);
    }
}

// 通过setAlignment
void ImgProcessor::ShowAlignment(QAction* act) {
    if(act == leftAction) {
        showWidget->text->setAlignment (Qt::AlignLeft) ;
    }
    if(act == rightAction) {
        showWidget->text->setAlignment (Qt::AlignRight);
    }
    if(act == centerAction) {
        showWidget->text->setAlignment (Qt::AlignCenter) ;
    }
    if(act == justifyAction) {
        showWidget->text->setAlignment (Qt::AlignJustify);
    }
}

void ImgProcessor::ShowCursorPositionChanged() {
    if (showWidget->text->alignment() == Qt::AlignLeft) {
        leftAction->setChecked(true);
    }
    if (showWidget->text->alignment() == Qt::AlignRight) {
        rightAction->setChecked(true);
    }
    if (showWidget->text->alignment() == Qt::AlignCenter) {
        centerAction->setChecked(true);
    }
    if (showWidget->text->alignment() == Qt::AlignJustify) {
        justifyAction->setChecked(true);
    }
}





showwidget.cpp

#include "showwidget.h"

#include <QHBoxLayout>

ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent) {
    imageLabel = new QLabel;
    imageLabel->setScaledContents(true);
    text = new QTextEdit;
    // 水平布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(imageLabel);
    mainLayout->addWidget(text);
}

main.cpp

#include "imgprocessor.h"

#include <QApplication>

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);
    QFont f("ZYSong18030", 12);
    a.setFont(f);
    ImgProcessor w;
    w.show();
    return a.exec();
}

5. 项目远程仓库

因为网络的问题,所以我选择将项目放到码云上,欢迎大家来star!

Qt文本编辑器Plus

posted @ 2021-07-12 17:06  进击的汪sir  阅读(1092)  评论(2编辑  收藏  举报