下面的程序出自《Qt5开发及实例》陆文周。
效果图:
程序是一个文本编辑器的实例,主界面如下:
程序中的源文件和头文件:
源代码:
文件main.cpp的源代码:
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QFont font("楷体",15);
- a.setFont(font);
- MainWindow w;
- w.show();
- return a.exec();
- }
mainwindow.h源代码:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QImage>
- #include <QLabel>
- #include <QMenu>
- #include <QMenuBar>
- #include <QAction>
- #include <QComboBox>
- #include <QSpinBox>
- #include <QToolBox>
- #include <QFontComboBox>
- #include <QToolButton>
- #include <QTextCharFormat>
- #include <QActionGroup>
- #include <QAction>
- #include "showwidget.h"
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
- void createActions(); //创建动作
- void createMenus(); //创建菜单
- void createToolBars(); //创建工具栏
- void loadFile(QString filename);
- void mergeFormat(QTextCharFormat format);
- 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 *mirrorHorizontalAction;
- 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;
- protected slots:
- 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 showSizeSpinBix(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 // MAINWINDOW_H
mainwindow.cpp源代码:
- #include "mainwindow.h"
- #include "showwidget.h"
- #include <QApplication>
- #include <QToolBar>
- #include <QFileDialog>
- #include <QTextStream>
- #include <QtPrintSupport/QPrinter>
- #include <QtPrintSupport/QPrintDialog>
- #include <QPainter>
- #include <QRect>
- #include <QSize>
- #include <QGraphicsItem>
- #include <QColorDialog>
- #include <QColor>
- #include <QAction>
- #include <QTextList>
- #include <QTextBlockFormat>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- setWindowTitle(tr("Easy Word"));
- showWidget = new ShowWidget(this);
- setCentralWidget(showWidget);
- 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("bold.png"));
- boldBtn->setCheckable(true);
- italicBtn = new QToolButton;
- italicBtn->setIcon(QIcon("italic.png"));
- italicBtn->setCheckable(true);
- underlineBtn = new QToolButton;
- underlineBtn->setIcon(QIcon("underline.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("image.png"))
- {
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- //各字体设置相关功能的信号与槽
- connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(showFontComboBox(QString)));
- connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(showSizeSpinBix(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)),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()));
- 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()));
- }
- void MainWindow::createActions()
- {
- //“打开”动作
- openFileAction = new QAction(QIcon("open.png") , tr("打开") , this);
- openFileAction->setShortcut(tr("Ctrl + O"));
- openFileAction->setStatusTip(tr("打开一个文件"));
- connect(openFileAction,SIGNAL(triggered(bool)),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(bool)),this,SLOT(close()));
- //"复制"动作
- copyAction = new QAction(QIcon("copy.png") , tr("复制") , this);
- copyAction->setShortcut(tr("Ctrl + C"));
- copyAction->setStatusTip(tr("复制文件"));
- connect(copyAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(copy()));
- //剪切操作
- cutAction = new QAction(QIcon("cut.png") , tr("剪切") ,this);
- cutAction->setShortcut(tr("Ctrl + X"));
- cutAction->setStatusTip(tr("剪切文件"));
- connect(cutAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(cut()));
- //粘贴操作
- pasteAction = new QAction(QIcon("paste.png") , tr("粘贴") ,this);
- pasteAction->setShortcut(tr("Ctrl + V"));
- pasteAction->setStatusTip(tr("粘贴文件"));
- connect(pasteAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(paste()));
- //“关于”操作
- aboutAction = new QAction(tr("关于") ,this);
- connect(aboutAction, SIGNAL(triggered(bool)),this, SLOT(Qapplication::aboutQt()));
- //打印文本
- printTextAction = new QAction(QIcon("printText.png") , tr("打印文本") ,this);
- printTextAction->setStatusTip(tr("打印一个文本"));
- connect(printTextAction,SIGNAL(triggered(bool)),this,SLOT(showPrintText()));
- //打印图片
- printImageAction = new QAction(QIcon("printImage.png") , tr("打印图像") , this);
- printImageAction->setStatusTip(tr("打印一幅图像"));
- connect(printImageAction,SIGNAL(triggered(bool)),this,SLOT(showPrintImage()));
- //放大图片
- zoomInAction = new QAction(QIcon("zoomin.png") , tr("放大") ,this);
- zoomInAction->setStatusTip(tr("放大一张图片"));
- connect(zoomInAction,SIGNAL(triggered(bool)),this,SLOT(showZoomIn()));
- //缩小图片
- zoomOutAction = new QAction(QIcon("zoomout.png") ,tr("缩小") ,this);
- zoomOutAction->setStatusTip(tr("缩小一张图片"));
- connect(zoomOutAction,SIGNAL(triggered(bool)),this,SLOT(showZoomOut()));
- //旋转图片
- rotate90Action = new QAction(QIcon("rotate90.png") , tr("旋转90") ,this);
- rotate90Action->setStatusTip(tr("将一幅图旋转90度"));
- connect(rotate90Action,SIGNAL(triggered(bool)),this,SLOT(showRotate90()));
- rotate180Action = new QAction(QIcon("rotate180.png") ,tr("旋转180") ,this);
- rotate180Action->setStatusTip(tr("将一幅图片旋转180度"));
- connect(rotate180Action,SIGNAL(triggered(bool)),this,SLOT(showRotate180()));
- rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270") ,this);
- rotate270Action->setStatusTip(tr("将一幅图片旋转270度"));
- connect(rotate270Action,SIGNAL(triggered(bool)),this,SLOT(showRotate270()));
- //垂直镜像
- mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png") , tr("纵向镜像") ,this);
- mirrorVerticalAction->setStatusTip(tr("对一张图片做纵向镜像"));
- connect(mirrorVerticalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorVertical()));
- //水平镜像
- mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png") ,tr("横向镜像") ,this);
- mirrorHorizontalAction->setStatusTip(tr("对一张图片做横向镜像"));
- connect(mirrorHorizontalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorHorizontal()));
- //撤销操作
- undoAction = new QAction(QIcon("undo.png"), tr("撤销") ,this);
- connect(undoAction, SIGNAL(triggered(bool)),showWidget->text,SLOT(undo()));
- //重做操作
- redoAction = new QAction(QIcon("redo.png") ,tr("重做") ,this);
- connect(redoAction,SIGNAL(triggered(bool)),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 MainWindow::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);
- rotateMenu = menuBar()->addMenu(tr("旋转"));
- rotateMenu->addAction(rotate90Action);
- rotateMenu->addAction(rotate180Action);
- rotateMenu->addAction(rotate270Action);
- mirrorMenu = menuBar()->addMenu(tr("镜像"));
- mirrorMenu->addAction(mirrorVerticalAction);
- mirrorMenu->addAction(mirrorHorizontalAction);
- }
- void MainWindow::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);
- 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 MainWindow::showNewFile() //新建文件操作
- {
- MainWindow *newMainWindow = new MainWindow;
- newMainWindow->show();
- }
- void MainWindow::showOpenFile() //读文本是打开新窗口
- {
- fileName = QFileDialog::getOpenFileName(this);
- if(!fileName.isEmpty())
- {
- if(showWidget->text->document()->isEmpty())
- {
- loadFile(fileName);
- }
- else
- {
- MainWindow *newMainWindow = new MainWindow;
- newMainWindow->show(); //当一个窗口已经打开了一个文件,还需要再打开文件时,新生成窗口
- newMainWindow->loadFile(fileName);
- }
- }
- }
- void MainWindow::loadFile(QString fileName) //载入文件
- {
- printf("File name:%s\n",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");
- }
- }
- void MainWindow::showPrintText() //打印文本
- {
- QPrinter printer;
- QPrintDialog printDialog;
- if(printDialog.exec())
- {
- QTextDocument *doc = showWidget->text->document();
- doc->print(&printer);
- }
- }
- void MainWindow::showPrintImage() //打印图片
- {
- QPrinter printer;
- QPrintDialog printDialog;
- 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 MainWindow::showZoomIn() //图片放大
- {
- if(img.isNull())
- {
- return ;
- }
- QMatrix martix;
- martix.scale(2,2);
- img = img.transformed(martix);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showZoomOut() //图片缩小
- {
- if(img.isNull())
- {
- return ;
- }
- QMatrix martix;
- martix.scale(0.5,0.5);
- img = img.transformed(martix);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showRotate90() //旋转图片功能
- {
- if(img.isNull())
- {
- return;
- }
- QMatrix matrix;
- matrix.rotate(90);
- img = img.transformed(matrix);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showRotate180()
- {
- if(img.isNull())
- {
- return;
- }
- QMatrix matrix;
- matrix.rotate(180);
- img = img.transformed(matrix);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showRotate270()
- {
- if(img.isNull())
- {
- return;
- }
- QMatrix matrix;
- matrix.rotate(270);
- img = img.transformed(matrix);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showMirrorHorizontal() //水平镜像
- {
- if(img.isNull())
- {
- return ;
- }
- img = img.mirrored(true,false);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showMirrorVertical() //垂直镜像
- {
- if(img.isNull())
- {
- return ;
- }
- img = img.mirrored(false, true);
- showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
- }
- void MainWindow::showFontComboBox(QString comboStr) //设置字体
- {
- QTextCharFormat fmt;
- fmt.setFontFamily(comboStr);
- mergeFormat(fmt);
- }
- void MainWindow::mergeFormat(QTextCharFormat format)
- {
- QTextCursor cursor = showWidget->text->textCursor();
- if(!cursor.hasSelection())
- {
- cursor.select(QTextCursor::WordUnderCursor);
- }
- cursor.mergeCharFormat(format);
- showWidget->text->mergeCurrentCharFormat(format);
- }
- void MainWindow::showSizeSpinBix(QString spinvalue) //设置字号
- {
- QTextCharFormat fmt;
- fmt.setFontPointSize(spinvalue.toFloat());
- showWidget->text->mergeCurrentCharFormat(fmt);
- }
- void MainWindow::showBoldBtn() //设置文字加粗
- {
- QTextCharFormat fmt;
- fmt.setFontWeight(boldBtn->isCheckable() ? QFont::Bold : QFont::Normal);
- showWidget->text->mergeCurrentCharFormat(fmt);
- }
- void MainWindow::showItalicBtn()
- {
- QTextCharFormat fmt;
- fmt.setFontItalic(italicBtn->isCheckable());
- showWidget->text->mergeCurrentCharFormat(fmt);
- }
- void MainWindow::showUnderlineBtn()
- {
- QTextCharFormat fmt;
- fmt.setFontUnderline(underlineBtn->isCheckable());
- showWidget->text->mergeCurrentCharFormat(fmt);
- }
- void MainWindow::showColorBtn()
- {
- QColor color = QColorDialog::getColor(Qt::red, this);
- if(color.isValid())
- {
- QTextCharFormat fmt;
- fmt.setForeground(color);
- showWidget->text->mergeCurrentCharFormat(fmt);
- }
- }
- void MainWindow::showCurrentFormatChanged(const QTextCharFormat &fmt)
- {
- fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));
- sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));
- boldBtn->setCheckable(fmt.font().bold());
- italicBtn->setCheckable(fmt.fontItalic());
- underlineBtn->setCheckable(fmt.fontUnderline());
- }
- void MainWindow::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 MainWindow::showCursorPositionChanged()
- {
- if(showWidget->text->alignment() == Qt::AlignLeft)
- {
- leftAction->setCheckable(true);
- }
- if(showWidget->text->alignment() == Qt::AlignRight)
- {
- rightAction->setCheckable(true);
- }
- if(showWidget->text->alignment() == Qt::AlignCenter)
- {
- centerAction->setCheckable(true);
- }
- if(showWidget->text->alignment() == Qt::AlignJustify)
- {
- justifyAction->setCheckable(true);
- }
- }
- void MainWindow::showList(int index)
- {
- 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;
- }
- 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);
- }
- }
- MainWindow::~MainWindow()
- {
- }
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 = 0);
- QImage img;
- QLabel *imageLabel;
- QTextEdit *text;
- signals:
- public slots:
- };
- #endif // SHOWWIDGET_H
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);
- }
程序中未解决的bug:
1、文本段落对齐功能有问题,不能实现其功能。(我仔细检查了代码,仍未发现问题所在,路过的大神各显神通吧,望不惜赤脚)。