.por
#-------------------------------------------------
#
# Project created by QtCreator 2020-06-03T22:29:01
#
#-------------------------------------------------
QT += core gui
QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 172025230
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
imgprocessor.cpp \
aa.cpp
HEADERS += imgprocessor.h \
```c
aa.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 = 0);
QImage *img;
QLabel *imageLabel;
QTextEdit *text;
signals:
public slots:
};
#endif // SHOWWIDGET_H
```c
imgprocessor.h
#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H
#include "aa.h"
//#include "finddialog.h"
#include <QMainWindow>
#include <QApplication>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QSaveFile>
#include <QAction>
#include <QActionGroup>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QToolButton>
#include <QFontComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextCursor>
class ImgProcessor : public QMainWindow
{
Q_OBJECT
public:
ImgProcessor(QWidget *parent = 0);
~ImgProcessor();
void createActions(); //创建动作
void createMenus(); //创建菜单
void createToolBars(); //创建工具栏
void loadFile(QString filename); //加载文件
void mergeFormat(QTextCharFormat); //合并格式
private:
//各项菜单栏
QMenu *fileMenu; //文件菜单
QMenu *zoomMenu; //缩放菜单
QMenu *rotateMenu; //旋转
QMenu *mirrorMenu; //镜像
//放置图片
QImage img;
QString fileName; //打开文件名
QString curFileName; //保存文件名
//显示中心部件
ShowWidget *showWidget;
//文件菜单项
QAction *openFileAction; //打开文件动作
QAction *NewFileAction; //新建文件
QAction *SaveAction; //保存文件
QAction *SaveAsAction; //另存为文件
QAction *PrintTextAction; //打印文本动作
QAction *PrintImageAction; //打印图片
QAction *exitAction; //退出动作
//编辑菜单项
QAction *copyAction; //复制动作
QAction *cutAction; //剪贴
QAction *pasteAction; //粘贴
QAction *zoomInAction; //放大
QAction *zoomOutAction; //缩小
//旋转菜单栏--图像处理
QAction *rotate90Action; //90°
QAction *rotate180Action; //180°
QAction *rotate270Action; //270°
//镜像菜单栏
QAction *mirrorVerticalAction; //垂直镜像
QAction *mirrorHorizonAction; //水平镜像
QAction *undoAction; //撤销
QAction *redoAction; //取消撤销
//工具栏
QToolBar *fileTool; //文件-打开、新建、保存,另存为,打印文本(图像)
QToolBar *zoomTool; //缩放
QToolBar *rotateTool; //旋转
QToolBar *mirrorTool; //镜像
QToolBar *doToolBar; //撤销、回撤--操作
//文本编辑
QLabel *fontLabel; //字体设置项
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; //排序工具栏
protected slots:
void ShowNewFile(); //新建文件
void ShowOpenFile(); //打开文件
void ShowSaveFile(); //保存文件
void ShowSaveAsFile(); //另存为
void ShowPrintText(); //打印文本
void ShowPrintImage(); //打印图像
void ShowZoomIn(); //放大功能
void ShowZoomOut(); //缩小功能
void ShowRotate90(); //旋转90°
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); //排序
void ShowAlignment(QAction *act); //对齐方式
void ShowCursorPositionChanged(); //显示光标位置
};
#endif // IMGPROCESSOR_H
aa.cpp
#include "aa.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);
}
imgprocessor.cpp
#include "imgprocessor.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QColor>
#include <QColorDialog>
#include <QTextList>
#include <QMessageBox>
#include <QDebug>
ImgProcessor::ImgProcessor(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle (tr("文本框")); //设置窗体标题
//创建放置图像QLabel和文本编辑器QTextEdit的QWidget对象showWidget,并将该QWidget对象设置
//为中心部件
resize(800,600);
showWidget = new ShowWidget(this);
setCentralWidget (showWidget);
curFileName = tr(""); //初始化文件名
//排序
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"); //大写罗马字符
//在工具栏上嵌入控件:字体,大小,粗体,斜体,字体颜色
//字体--这里的字体,字号仅仅是下拉列表框改变
fontLabel = new QLabel(tr("字体:"));
fontComboBox = new QFontComboBox;
//setFontFilter--接口过滤(只在下拉列表框中显示某一类字体,默认情况下为QFontComboBox::AllFonts
//列出所有字体
fontComboBox->setFontFilters (QFontComboBox::ScalableFonts);
//字号
fontLabel2 = new QLabel(tr("字号:"));
sizeComboBox = new QComboBox;
QFontDatabase db;
//standardSize(): return a list of standard font size(返回可用标准字号的列表).
foreach (int size, db.standardSizes ())
sizeComboBox->addItem (QString::number (size)); //将它们插入到字号下拉框中
//加粗
boldBtn = new QToolButton;
boldBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/bold.png"));
//设置成是否是开关(toggle)按钮(true)---即可复选的
boldBtn->setCheckable (true);
//斜体
italicBtn = new QToolButton;
italicBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/italic.png"));
italicBtn->setCheckable (true);
//下划线
underlineBtn = new QToolButton;
underlineBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/underline.png"));
underlineBtn->setCheckable (true);
//颜色
colorBtn = new QToolButton;
colorBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/color.png"));
colorBtn->setCheckable (true);
/* 创建动作、菜单、工具栏函数 */
createActions ();
createMenus ();
createToolBars ();
if (img.load ("C:/Users/Administrator/Desktop/文本框/172025230/photo/image.png"))
{
//在imageLabel对象放置图像
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(bool)), this, SLOT(ShowBoldBtn()));
connect (italicBtn, SIGNAL(clicked(bool)), this, SLOT(ShowItalicBtn()));
connect (underlineBtn, SIGNAL(clicked(bool)), this, SLOT(ShowUnderlineBtn()));
connect (colorBtn, SIGNAL(clicked(bool)