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