文章目录
Qt5 主窗口构成
基本元素
示例:文本编辑器
示例步骤及代码
显示结果
Qt5 文件操作功能
新建文件
打开文件
打印文件
Qt5 图像坐标变换
缩放功能
旋转功能
Qt5 文本编辑功能
实现文本编辑的具体步骤:
Qt5 排版功能
最终实现效果及全部代码
全部代码
界面效果
Qt5 主窗口构成
基本元素
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器、图片编辑器等。
接下来将详细介绍关于这些的基本元素
- 菜单栏
- 菜单是一系列命令的列表。为了实现菜单、工具栏按钮、键盘快捷方式等命令的一致性,Qt使用动作(Action)来表示这些命令。Qt的菜单就是由一系列的QAction动作对象构成的列表,而菜单栏则是包容菜单的面板,它位于主窗口顶部、主窗口标题栏的下面。一个主窗口最多只有一个菜单栏。
- 状态栏
- 状态栏通常显示GUI应用程序的一些状态信息,它位于主窗口的底部。用户可以在状态栏上添加、使用Qt窗口部件。一个主窗口最多只能有一个状态栏。
- 工具栏
- 工具栏是由一系列的类似于按钮的动作排列而成的面板,它通常由一些经常使用的命令(动作)组成。工具栏位于菜单栏的下面、状态栏的上面,可以停靠在主窗口的上、下、左、右四个方向上。一个主窗口可以包含多个工具栏。
- 锚接部件
- 锚接部件作为一个容器使用,以包容其他窗口部件来实现某些功能。例如:Qt设计器的属性编辑器、对象监视器等都是由锚接部件包容其他的Qt窗口部件来实现的。它位于工具栏区的内部,可以作为一个窗口自由浮动在主窗口上面,也可以像工具栏一样停靠在主窗口的上、下、左、右四个方向上。一个主窗口可以包含多个锚接部件。
- 中心部件
- 中心部件处于锚接部件区的内部、主窗口的中心。一个主窗口只有一个中心部件。
- 注:主窗口QMainWindow具有自己的布局管理器,因此在QMainWindow窗口上设置布局管理器或者创建一个父窗口部件作为QMainWindow的布局管理器都是不允许的。但可以在主窗口的中心部件上设置管理器。
示例:文本编辑器
接下来通过完成一个文本编辑器应用实例,来介绍关于QMainWindow主窗体的创建流程核各种功能的开发。
首先,极少文件操作功能,包括新建一个文件,理由标准文件对话框QFileDialog类打开一个已存在的文件,利用QFile和QTextStream读取文件内容,打印文件(分文本打印和图像打印)。通过示例介绍标准打印对话框QPrintDialog类的使用方法,以QPrinter作为QPaintDevice画图实现图像打印。
接着,介绍图像处理软件中的常用功能,包括图片的缩放、旋转及镜像等坐标变换,使用QMatrix实现图像的各种坐标变换。
然后,开发文本编辑功能,通过在工具栏上设置文字字体、字号大小、加粗、斜体、下划线及字体颜色等快捷按钮的实现,介绍了在工具栏中嵌入控件的方法。其中,通过设置字体颜色功能,介绍了标准颜色对话框QColorDialog类的使用方法。
最后是排版功能,通过选择某种排序方式实现对文本排序,以及实现文本对齐(包括左对齐、右对齐、居中对齐和两端对齐)和撤销、重做的方法。
示例步骤及代码
首先简历项目的框架代码:
- 新建Qt Widgets Application项目,项目名称为“ImageProcessor”,基类选择“QMainWindow”,类名命名为“ImgProcessor”,取消“创建界面”复选框的选中状态。完成项目工程的建立。
- 添加工程的提供主要显示文本编辑框函数所在的文件,点击右键添加新文件,选择“C++Class”选项,选择基类名“QWidget”,类命名为“ShowWidget”,在源文件和头文件添加以下代码:
- showwidget.h
#ifndef SHOWWIDGET_H #define SHOWWIDGET_H #include #include #include #include class ShowWidget : public QWidget { Q_OBJECT public: explicit ShowWidget(QWidget *parent = nullptr); QImage img; QLabel *imageLabel; QTextEdit *text; signals: public slots: }; #endif // SHOWWIDGET_H
- showwidget.cpp
#include "showwidget.h" #include 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 类声明中,createActions() 函数用于创建所有的动作,createMenus() 函数用于创建菜单、createToolBars() 函数用于创建工具栏;接着声明实现主窗口所需的各个元素,包括菜单、工具栏及各个动作等;最后声明用到的槽函数,打开“imgprocessor.h”文件,添加以下代码:
#ifndef IMGPROCESSOR_H #define IMGPROCESSOR_H #include #include #include #include #include #include #include #include #include #include #include #include #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); private: QMenu *fileMenu; QMenu *zoomMenu; QMenu *rotateMenu; QMenu *mirrorMenu; QImage img; QString fileName; ShowWidget *showWidget; //文件菜单项 QAction *openFileAction; QAction *NewFileAction; QAction *printTRextAction; 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 *mirrorHorizontalAction; QAction *undoAction; QAction *redoAction; QToolBar *fileTool; //工具栏 QToolBar *zoomTool; QToolBar *rotateTool; QToolBar *mirrorTool; QToolBar *doToolBar; }; #endif // IMGPROCESSOR_H
- 下面是主窗口的部分代码:
#include "imgprocessor.h" ImgProcessor::ImgProcessor(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("Easy Word")); //窗口标题 showWidget = new ShowWidget(this); setCentralWidget(showWidget); //创建动作、菜单、工具栏 createActions(); createMenus(); createToolBars(); if(img.load("mage.png")) { //在imagelabel中放置图像 showWidget->imageLabel->setPixmap(QPixmap::fromImage(img)); } } ImgProcessor::~ImgProcessor() { } void ImgProcessor::createActions() { //“打开”动作 openFileAction = new QAction(QIcon("open.png"), tr("打开"), this); openFileAction->setShortcut(tr("Ctrl+O")); //设置此动作的组合键 openFileAction->setStatusTip(tr("打开一个文件"));//设定状态栏显示。 //“新建”动作 NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this); NewFileAction->setShortcut(tr("Ctrl+N")); NewFileAction->setStatusTip(tr("新建一个文件")); //“退出”动作 exitAction = new QAction(tr("退出"), this); exitAction->setShortcut(tr("Ctrl+Q")); exitAction->setStatusTip(tr("退出程序")); connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); //“复制”动作 copyAction = new QAction(QIcon("copy.png"), tr("复制"), this); copyAction->setShortcut(tr("Ctrl+C")); copyAction->setStatusTip(tr("复制文件")); connect(copyAction, SIGNAL(triggered()), showWidget->text, SLOT(copy())); //“剪切”动作 cutAction = new QAction(QIcon("cut.png"), tr("剪切"), this); cutAction->setShortcut(tr("Ctrl+X")); cutAction->setStatusTip(tr("剪切文件")); connect(cutAction, SIGNAL(truggered()), showWidget->text, SLOT(cut())); //“粘贴”动作 pasteAction = new QAction(QIcon("paste.png"), tr("粘贴"), this); pasteAction->setShortcut(tr("Ctrl+V")); pasteAction->setStatusTip(tr("粘贴文件")); connect(pasteAction, SIGNAL(triggered()), showWidget->text, SLOT(paste())); //“关于”动作 aboutAction = new QAction(tr("关于"), this); connect(aboutAction, SIGNAL(triggered()), this, SLOT(QApplication::aboutQt())); //“打印文本”动作 printTRextAction = new QAction(QIcon("printText.png"), tr("打印文本"), this); printTRextAction->setStatusTip(tr("打印一个文本")); //“打印图像”动作 printImageAction = new QAction(QIcon("printImage.png"), tr("打印图像"), this); printImageAction->setStatusTip(tr("打印一幅图像")); //“放大”动作 zoomInAction = new QAction(QIcon("zoomin.png"), tr("放大"), this); zoomInAction->setStatusTip(tr("放大一张照片")); //“缩小”动作 zoomOutAction = new QAction(QIcon("zoomout.png"), tr("缩小"), this); zoomOutAction->setStatusTip(tr("缩小一张照片")); //实现图像旋转的动作 //90 rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋转90"), this); rotate90Action->setStatusTip(tr("将一幅图像旋转90度")); //180 rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋转180"), this); rotate180Action->setStatusTip(tr("将一幅图像旋转180度")); //270 rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270"), this); rotate270Action->setStatusTip(tr("将一副图像旋转270度")); //实现图像镜像的动作 //纵向 mirrorVerticalAction = new QAction(QIcon("mirrorV.png"), tr("纵向镜像"), this); mirrorVerticalAction->setStatusTip(tr("对一幅图像纵向镜像")); //横向 mirrorHorizontalAction = new QAction(QIcon("mirrorH.png"), tr("横向镜像"), this); mirrorHorizontalAction->setStatusTip(tr("对一副图像横向镜像")); //实现撤销和恢复动作 undoAction = new QAction(QIcon("undo.png"), tr("撤销"), this); connect(undoAction, SIGNAL(triggered()), showWidget->text, SLOT(undo())); redoAction = new QAction(QIcon("redo.png"), tr("重做"), this); connect(redoAction, SIGNAL(triggered()), showWidget->text, SLOT(redo())); } void ImgProcessor::createMenus() { //文件菜单 fileMenu = menuBar()->addMenu(tr("文件")); //menuBar()得到主窗口的菜单栏指针,调用函数addMenu()插入一个新的菜单。 fileMenu->addAction(openFileAction);//调用addAction()函数向菜单中加入菜单条目 fileMenu->addAction(NewFileAction); fileMenu->addAction(printTRextAction); 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); //旋转菜单 rotateMenu = menuBar()->addMenu(tr("旋转")); rotateMenu->addAction(rotate90Action); rotateMenu->addAction(rotate180Action); rotateMenu->addAction(rotate270Action); //镜像菜单 mirrorMenu = menuBar()->addMenu(tr("镜像")); mirrorMenu->addAction(mirrorVerticalAction); mirrorMenu->addAction(mirrorHorizontalAction); } void ImgProcessor::createToolBars() { //文件工具条 fileTool = addToolBar("File"); //QMainWindow直接调用addToolBar()函数获得主窗口的工具条对象。 fileTool->addAction(openFileAction);//向工具条查出属于该工具条的工作。 fileTool->addAction(NewFileAction); fileTool->addAction(printTRextAction); 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); //撤销和重做工具条 doToolBar = addToolBar("doEdit"); doToolBar->addAction(undoAction); doToolBar->addAction(redoAction); }
显示结果
Qt5 文件操作功能
新建文件
实现新建空白文件的功能,步骤如下:
- 打开“imgprocessor.h”头文件,添加槽函数及实现如下:
private slots: void showNewFile();//新建文件
void ImgProcessor::createActions() { //“新建”动作 NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this); NewFileAction->setShortcut(tr("Ctrl+N")); NewFileAction->setStatusTip(tr("新建一个文件")); connect(NewFileAction, SIGNAL(triggered()), this, SLOT(showNewFile())); } void ImgProcessor::showNewFile() { ImgProcessor *newImgProcessor = new ImgProcessor; newImgProcessor->show(); }
打开文件
打开功能是读取一个已经存在的文件,将文件内容显示在窗口中。
在头文件和源文件中添加对应的槽函数及实现:
void showOpenFile();//打开文件
void ImgProcessor::createActions() { //“打开”动作 openFileAction = new QAction(QIcon("open.png"), tr("打开"), this); openFileAction->setShortcut(tr("Ctrl+O")); openFileAction->setStatusTip(tr("打开一个文件")); connect(openFileAction, SIGNAL(triggered()), this, SLOT(showOpenFile())); } void ImgProcessor::loadFile(QString filename) { qDebug() << "file name :" << filename; QFile file(filename); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream textStream(&file); while(!textStream.atEnd()) { showWidget->text->append(textStream.readLine()); qDebug() << "read line"; } } } 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); } } }
打印文件
打印的文件分为文本和图片两种格式,下面分别介绍。
- 文本打印
使用Qt提供的标准打印对话框,QPrintDialog。具体如下:
void showPrintText();//打印文本文件
connect(printTRextAction, SIGNAL(triggered()), this, SLOT(showPrintText())); void ImgProcessor::showPrintText() { QPrinter printer; QPrintDialog printDialog(&printer, this);//创建一个QPrintDialog对象。 if(printDialog.exec()) { //获得QTextEdit 对象的文档 QTextDocument *doc = showWidget->text->document(); doc->print(&printer); } }
- 图像打印
添加槽函数。
void showPrintImage();//打印图片
connect(printImageAction, SIGNAL(triggered()), this, SLOT(showPrintImage())); void ImgProcessor::showPrintImage() { QPrinter printer; QPrintDialog printDialog(&printer, this); if(printDialog.exec()) { QPainter painter(&printer); QRect rect = painter.viewport();//获得QPainter 对象的视图矩形区域 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); } }
Qt5 图像坐标变换
QMatrix类提供了世界坐标系统的二维转换功能,可以使窗体转换变形,经常在绘图程序中使用,还可以实现坐标系统的移动、缩放、变形及旋转功能。
setScaledContents 用来设置该控件的scaledContents属性,确定是否根据其大小自动调节内容大小,以便内容充满整个有效区域。若设置值为true,则当现实图片时,控件会根据其大小对图片进行调节。该属性默认值为false,另外,可以通过hasScaledContents() 来获取该属性的值。
缩放功能
下面将实现缩放功能。
void showZoomIn();//放大 void showZoomOut();//缩小 void ImgProcessor::showZoomIn() { if(img.isNull())//有效判断 { return; } QMatrix martix;//声明一个QMatrix类的实例 martix.scale(2, 2); //该参数决定x方向和y方向的缩放比例 img = img.transformed(martix); 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 showRotate90(); //旋转90 void showRotate180(); void showRotate270(); //实现图像旋转的动作 //90 rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋转90"), this); rotate90Action->setStatusTip(tr("将一幅图像旋转90度")); connect(rotate90Action, SIGNAL(triggered()), this, SLOT(showRotate90())); //180 rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋转180"), this); rotate180Action->setStatusTip(tr("将一幅图像旋转180度")); connect(rotate180Action, SIGNAL(triggered()), this, SLOT(showRotate180())); //270 rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270"), this); rotate270Action->setStatusTip(tr("将一副图像旋转270度")); connect(rotate270Action, SIGNAL(triggered()), this, SLOT(showRotate270())); 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)); }
Qt5 文本编辑功能
在编写包含格式设置的文本编辑程序时,经常用到的Qt类有QTextEdit、QTextDocument、QTextBlock、QTextList、QTextFrame、QTextTable、QTextCharFormat、QTextBlockFormat、QTextListFormat、QTextFrameFormat和QTextTableFormat等。
任何一个文本编辑的程序都要用到QTextEdit作为输入文本的容器,在它里面输入可编辑文本由QTextDocument作为载体,而用来表示QTextDocument的元素的QTextBlock、QTextList、QTextFrame等都是QTextDocument的不同表现形式,可以表示为字符串、段落、列表、表格或图片等。
每种元素都有自己的个数,这些格式则是用这些类来实现的。
实现文本编辑的具体步骤:
- 在“imgprocessor.h”头文件中添加“private”变量:
QLabel *fontLabel; QFontComboBox *fontComboBox; QLabel *fontLabel2; QComboBox *sizeComboBox; QToolButton *boldBtn; QToolButton *italicBtn; QToolButton *underlineBtn; QToolButton *colorBtn; QToolBar *fontToolBar;
- 在“imgprocessor.h”头文件中添加槽函数:
protected slots: void ShowFontComboBox(QString comboStr); void ShowSizeSpinBox(QString spinValue); void ShowBoldBtn(); void ShowItalicBtn(); void ShowUnderlineBtn(); void ShowColorBtn(); void ShowCurrentFormatChanged(const QTextCharFormat &fmt);
- 在相对应的构造函数中,添加以下语句:
ImgProcessor::ImgProcessor(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("Easy Word")); //窗口标题 showWidget = new ShowWidget(this); setCentralWidget(showWidget); //在工具栏上嵌入控件 //设置字体 fontLabel = 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("bold.png")); boldBtn->setCheckable(true); italicBtn = new QToolButton; italicBtn->setIcon(QIcon("italic.png")); italicBtn->setCheckable(true); underlineBtn = new QToolButton; underlineBtn->setIcon(QIcon("under.png")); underlineBtn->setCheckable(true); colorBtn = new QToolButton; colorBtn->setIcon(QIcon("color.png")); colorBtn->setCheckable(true); //创建动作、菜单、工具栏 createActions(); createMenus(); createToolBars(); if(img.load("mage.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()), 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(const QTextCharFormat &))); } void ImgProcessor::ShowFontComboBox(QString comboStr) { //设置字体 QTextCharFormat fmt;//创建一个QTextCharFormat对象 fmt.setFontFamily(comboStr);//选择的字体名称设置给QTextCharFormat对象 mergeFormat(fmt);//将新的格式应用到光标选区内的字符。 } 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) { //设置字号 qDebug() << spinValue << "--" << spinValue.toFloat(); 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()); }
Qt5 排版功能
//排版 void showList(int); void showAlignment(QAction *act); void showCursorPositionChanged(); //排版功能 QLabel *listLabel; QComboBox *listComboBox; QActionGroup *actGrp; QAction *leftAction; QAction *rightAction; QAction *centerAction; QAction *justifyAction; QToolBar *listToolBar;
//排版格式 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"); //排版格式 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->document(), SIGNAL(cursorPositionChanged()), this, SLOT(showCursorPositionChanged())); 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::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); } void ImgProcessor::showList(int index) { //获得编辑框的QTextCursor对象指针 QTextCursor cursor = showWidget->text->textCursor(); if(index != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc;//从下拉列表中旋转确定QTextlistformat的style属性值。 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; } //设置缩进值 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); } }
- main.cpp
#include "imgprocessor.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); QFont f("ZYSong18030", 12); a.setFont(f); ImgProcessor w; w.show(); return a.exec(); }
最终实现效果及全部代码
全部代码
- main.cpp
#include "imgprocessor.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); QFont f("ZYSong18030", 12); a.setFont(f); ImgProcessor w; w.show(); return a.exec(); }
- imgprocessor.h
#ifndef IMGPROCESSOR_H #define IMGPROCESSOR_H #include #include #include #include #include #include #include #include #include #include #include #include #include "showwidget.h" #include 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); private slots: void showNewFile();//新建文件 void showOpenFile();//打开文件 void showPrintText();//打印文本文件 void showPrintImage();//打印图片 void showZoomIn();//放大 void showZoomOut();//缩小 void showRotate90(); //旋转90 void showRotate180(); void showRotate270(); protected slots: //字体格式 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(); private: QMenu *fileMenu; QMenu *zoomMenu; QMenu *rotateMenu; QMenu *mirrorMenu; QImage img; QString fileName; ShowWidget *showWidget; //文件菜单项 QAction *openFileAction; QAction *NewFileAction; QAction *printTRextAction; 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 *mirrorHorizontalAction; 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; }; #endif // IMGPROCESSOR_H
- imgprocessor.cpp
#include "imgprocessor.h" #include #include #include #include #include #include #include #include #include #include #include ImgProcessor::ImgProcessor(QWidget *parent) : QMainWindow(parent) { setWindowTitle(tr("Easy Word")); //窗口标题 showWidget = new ShowWidget(this); setCentralWidget(showWidget); //在工具栏上嵌入控件 //设置字体 fontLabel = 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("bold.png")); boldBtn->setCheckable(true); italicBtn = new QToolButton; italicBtn->setIcon(QIcon("italic.png")); italicBtn->setCheckable(true); underlineBtn = new QToolButton; underlineBtn->setIcon(QIcon("under.png")); underlineBtn->setCheckable(true); colorBtn = new QToolButton; colorBtn->setIcon(QIcon("color.png")); colorBtn->setCheckable(true); //排版格式 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"); //创建动作、菜单、工具栏 createActions(); createMenus(); createToolBars(); if(img.load("mage.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()), 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(const 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->document(), SIGNAL(cursorPositionChanged()), this, SLOT(showCursorPositionChanged())); } ImgProcessor::~ImgProcessor() { } void ImgProcessor::createActions() { //“打开”动作 openFileAction = new QAction(QIcon("open.png"), tr("打开"), this); openFileAction->setShortcut(tr("Ctrl+O")); openFileAction->setStatusTip(tr("打开一个文件")); connect(openFileAction, SIGNAL(triggered()), this, SLOT(showOpenFile())); //“新建”动作 NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this); NewFileAction->setShortcut(tr("Ctrl+N")); NewFileAction->setStatusTip(tr("新建一个文件")); connect(NewFileAction, SIGNAL(triggered()), this, SLOT(showNewFile())); //“退出”动作 exitAction = new QAction(tr("退出"), this); exitAction->setShortcut(tr("Ctrl+Q")); exitAction->setStatusTip(tr("退出程序")); connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); //“复制”动作 copyAction = new QAction(QIcon("copy.png"), tr("复制"), this); copyAction->setShortcut(tr("Ctrl+C")); copyAction->setStatusTip(tr("复制文件")); connect(copyAction, SIGNAL(triggered()), showWidget->text, SLOT(copy())); //“剪切”动作 cutAction = new QAction(QIcon("cut.png"), tr("剪切"), this); cutAction->setShortcut(tr("Ctrl+X")); cutAction->setStatusTip(tr("剪切文件")); connect(cutAction, SIGNAL(truggered()), showWidget->text, SLOT(cut())); //“粘贴”动作 pasteAction = new QAction(QIcon("paste.png"), tr("粘贴"), this); pasteAction->setShortcut(tr("Ctrl+V")); pasteAction->setStatusTip(tr("粘贴文件")); connect(pasteAction, SIGNAL(triggered()), showWidget->text, SLOT(paste())); //“关于”动作 aboutAction = new QAction(tr("关于"), this); connect(aboutAction, SIGNAL(triggered()), this, SLOT(QApplication::aboutQt())); //“打印文本”动作 printTRextAction = new QAction(QIcon("printText.png"), tr("打印文本"), this); printTRextAction->setStatusTip(tr("打印一个文本")); connect(printTRextAction, SIGNAL(triggered()), this, SLOT(showPrintText())); //“打印图像”动作 printImageAction = new QAction(QIcon("printImage.png"), tr("打印图像"), this); printImageAction->setStatusTip(tr("打印一幅图像")); connect(printImageAction, SIGNAL(triggered()), this, SLOT(showPrintImage())); //“放大”动作 zoomInAction = new QAction(QIcon("zoomin.png"), tr("放大"), this); zoomInAction->setStatusTip(tr("放大一张照片")); connect(zoomInAction, SIGNAL(triggered()), this, SLOT(showZoomIn())); //“缩小”动作 zoomOutAction = new QAction(QIcon("zoomout.png"), tr("缩小"), this); zoomOutAction->setStatusTip(tr("缩小一张照片")); connect(zoomOutAction, SIGNAL(triggered()), this, SLOT(showZoomOut())); //实现图像旋转的动作 //90 rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋转90"), this); rotate90Action->setStatusTip(tr("将一幅图像旋转90度")); connect(rotate90Action, SIGNAL(triggered()), this, SLOT(showRotate90())); //180 rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋转180"), this); rotate180Action->setStatusTip(tr("将一幅图像旋转180度")); connect(rotate180Action, SIGNAL(triggered()), this, SLOT(showRotate180())); //270 rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270"), this); rotate270Action->setStatusTip(tr("将一副图像旋转270度")); connect(rotate270Action, SIGNAL(triggered()), this, SLOT(showRotate270())); //实现图像镜像的动作 //纵向 mirrorVerticalAction = new QAction(QIcon("mirrorV.png"), tr("纵向镜像"), this); mirrorVerticalAction->setStatusTip(tr("对一幅图像纵向镜像")); //横向 mirrorHorizontalAction = new QAction(QIcon("mirrorH.png"), tr("横向镜像"), this); mirrorHorizontalAction->setStatusTip(tr("对一副图像横向镜像")); //实现撤销和恢复动作 undoAction = new QAction(QIcon("undo.png"), tr("撤销"), this); connect(undoAction, SIGNAL(triggered()), showWidget->text, SLOT(undo())); redoAction = new QAction(QIcon("redo.png"), tr("重做"), this); connect(redoAction, SIGNAL(triggered()), showWidget->text, SLOT(redo())); //排序:左对齐、右对齐、居中和两端对齐 actGrp = new QActionGroup(this); leftAction = new QAction(QIcon("left.png"), "左对齐", actGrp); leftAction->setCheckable(true); rightAction = new QAction(QIcon("right.png"), "右对齐", actGrp); rightAction->setCheckable(true); centerAction = new QAction(QIcon("center.png"), "居中", actGrp); centerAction->setCheckable(true); justifyAction = new QAction(QIcon("justify.png"), "两端对齐", 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(printTRextAction); 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); //旋转菜单 rotateMenu = menuBar()->addMenu(tr("旋转")); rotateMenu->addAction(rotate90Action); rotateMenu->addAction(rotate180Action); rotateMenu->addAction(rotate270Action); //镜像菜单 mirrorMenu = menuBar()->addMenu(tr("镜像")); mirrorMenu->addAction(mirrorVerticalAction); mirrorMenu->addAction(mirrorHorizontalAction); } void ImgProcessor::createToolBars() { //文件工具条 fileTool = addToolBar("File"); fileTool->addAction(openFileAction); fileTool->addAction(NewFileAction); fileTool->addAction(printTRextAction); 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); //撤销和重做工具条 doToolBar = addToolBar("doEdit"); doToolBar->addAction(undoAction); doToolBar->addAction(redoAction); //字体工具条 fontToolBar = addToolBar("font"); fontToolBar->addWidget(fontLabel); 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::showNewFile() { ImgProcessor *newImgProcessor = new ImgProcessor; newImgProcessor->show(); } void ImgProcessor::loadFile(QString filename) { qDebug() << "file name :" << filename; QFile file(filename); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream textStream(&file); while(!textStream.atEnd()) { showWidget->text->append(textStream.readLine()); qDebug() << "read line"; } } } 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);//创建一个QPrintDialog对象。 if(printDialog.exec()) { //获得QTextEdit 对象的文档 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();//获得QPainter 对象的视图矩形区域 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 martix;//声明一个QMatrix类的实例 martix.scale(2, 2); //该参数决定x方向和y方向的缩放比例 img = img.transformed(martix); 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::ShowFontComboBox(QString comboStr) { //设置字体 QTextCharFormat fmt;//创建一个QTextCharFormat对象 fmt.setFontFamily(comboStr);//选择的字体名称设置给QTextCharFormat对象 mergeFormat(fmt);//将新的格式应用到光标选区内的字符。 } 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) { //设置字号 qDebug() << spinValue << "--" << spinValue.toFloat(); 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::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); } void ImgProcessor::showList(int index) { //获得编辑框的QTextCursor对象指针 QTextCursor cursor = showWidget->text->textCursor(); if(index != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc;//从下拉列表中旋转确定QTextlistformat的style属性值。 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; } //设置缩进值 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); } }
- showwidget.h
#ifndef SHOWWIDGET_H #define SHOWWIDGET_H #include #include #include #include class ShowWidget : public QWidget { Q_OBJECT public: explicit ShowWidget(QWidget *parent = nullptr); QImage img; QLabel *imageLabel; QTextEdit *text; signals: public slots: }; #endif // SHOWWIDGET_H
- showwidget.cpp
#include "showwidget.h" #include 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); }
界面效果