Qt5主窗体程序: 文本编辑器的实现(Qt5开发及实例)

来源:互联网 发布:黑客盗号软件 编辑:程序博客网 时间:2024/06/06 06:39

下面的程序出自《Qt5开发及实例》陆文周。

效果图:

程序是一个文本编辑器的实例,主界面如下:



程序中的源文件和头文件:



源代码:

文件main.cpp的源代码:

[cpp] view plain copy
 print?
  1. #include "mainwindow.h"  
  2. #include <QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.   
  8.     QFont font("楷体",15);  
  9.     a.setFont(font);  
  10.   
  11.     MainWindow w;  
  12.     w.show();  
  13.   
  14.     return a.exec();  
  15. }  


mainwindow.h源代码:

[cpp] view plain copy
 print?
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5. #include <QImage>  
  6. #include <QLabel>  
  7. #include <QMenu>  
  8. #include <QMenuBar>  
  9. #include <QAction>  
  10. #include <QComboBox>  
  11. #include <QSpinBox>  
  12. #include <QToolBox>  
  13. #include <QFontComboBox>  
  14. #include <QToolButton>  
  15. #include <QTextCharFormat>  
  16. #include <QActionGroup>  
  17. #include <QAction>  
  18. #include "showwidget.h"  
  19.   
  20. class MainWindow : public QMainWindow  
  21. {  
  22.     Q_OBJECT  
  23.   
  24. public:  
  25.     MainWindow(QWidget *parent = 0);  
  26.     ~MainWindow();  
  27.   
  28.     void createActions();   //创建动作  
  29.     void createMenus(); //创建菜单  
  30.     void createToolBars();  //创建工具栏  
  31.   
  32.     void loadFile(QString filename);  
  33.     void mergeFormat(QTextCharFormat format);  
  34.   
  35. private:  
  36.     QMenu *fileMenu;    //各项菜单栏  
  37.     QMenu *zoomMenu;  
  38.     QMenu *rotateMenu;  
  39.     QMenu *mirrorMenu;  
  40.   
  41.     QImage img;  
  42.     QString fileName;  
  43.     ShowWidget *showWidget;  
  44.   
  45.     QAction *openFileAction;    //文件菜单项  
  46.     QAction *newFileAction;  
  47.     QAction *printTextAction;  
  48.     QAction *printImageAction;  
  49.     QAction *exitAction;  
  50.   
  51.     QAction *copyAction;    //编辑菜单项  
  52.     QAction *cutAction;  
  53.     QAction *pasteAction;  
  54.     QAction *aboutAction;  
  55.     QAction *zoomInAction;  
  56.     QAction *zoomOutAction;  
  57.   
  58.   
  59.     QAction *rotate90Action;    //旋转菜单项  
  60.     QAction *rotate180Action;  
  61.     QAction *rotate270Action;  
  62.   
  63.     QAction *mirrorVerticalAction;  //镜像菜单项  
  64.     QAction *mirrorHorizontalAction;  
  65.   
  66.     QAction *undoAction;    //撤销和返回  
  67.     QAction *redoAction;  
  68.   
  69.     QToolBar *fileTool; //工具栏  
  70.     QToolBar *zoomTool;  
  71.     QToolBar *rotateTool;  
  72.     QToolBar *mirrorTool;  
  73.     QToolBar *doToolBar;    //工具栏  
  74.   
  75.     QLabel *fontLabel1; //字体设置项  
  76.     QFontComboBox *fontComboBox;  
  77.     QLabel *fontLabel2;  
  78.     QComboBox *sizeComboBox;  
  79.     QToolButton *boldBtn;  
  80.     QToolButton *italicBtn;  
  81.     QToolButton *underlineBtn;  
  82.     QToolButton *colorBtn;  
  83.     QToolBar *fontToolBar;  //字体工具栏  
  84.   
  85.     QLabel *listLabel;  //文字排版功能  
  86.     QComboBox *listComboBox;  
  87.     QActionGroup *actGrp;  
  88.     QAction *leftAction;  
  89.     QAction *rightAction;  
  90.     QAction *centerAction;  
  91.     QAction *justifyAction;  
  92.     QToolBar *listToolBar;  
  93.   
  94.   
  95. protected slots:  
  96.     void showNewFile(); //新建文件槽函数  
  97.     void showOpenFile();    //打开文件槽函数  
  98.   
  99.     void showPrintText();   //打印文本槽函数  
  100.     void showPrintImage();  //打印图片槽函数  
  101.   
  102.     void showZoomIn();  //放大功能槽函数  
  103.     void showZoomOut(); //缩小功能槽函数  
  104.   
  105.     void showRotate90();    //旋转图片槽函数  
  106.     void showRotate180();  
  107.     void showRotate270();  
  108.   
  109.     void showMirrorVertical();  //镜像功能  
  110.     void showMirrorHorizontal();  
  111.   
  112.     void showFontComboBox(QString comboStr);    //字体设置相关的槽函数  
  113.     void showSizeSpinBix(QString spinValue);  
  114.     void showBoldBtn();  
  115.     void showItalicBtn();  
  116.     void showUnderlineBtn();  
  117.     void showColorBtn();  
  118.     void showCurrentFormatChanged(const QTextCharFormat &fmt);  
  119.   
  120.     void showList(int);  
  121.     void showAlignment(QAction *act);  
  122.     void showCursorPositionChanged();  
  123.   
  124. };  
  125.   
  126. #endif // MAINWINDOW_H  

mainwindow.cpp源代码:

[cpp] view plain copy
 print?
  1. #include "mainwindow.h"  
  2. #include "showwidget.h"  
  3. #include <QApplication>  
  4. #include <QToolBar>  
  5. #include <QFileDialog>  
  6. #include <QTextStream>  
  7. #include <QtPrintSupport/QPrinter>  
  8. #include <QtPrintSupport/QPrintDialog>  
  9. #include <QPainter>  
  10. #include <QRect>  
  11. #include <QSize>  
  12. #include <QGraphicsItem>  
  13. #include <QColorDialog>  
  14. #include <QColor>  
  15. #include <QAction>  
  16. #include <QTextList>  
  17. #include <QTextBlockFormat>  
  18.   
  19. MainWindow::MainWindow(QWidget *parent)  
  20.     : QMainWindow(parent)  
  21. {  
  22.     setWindowTitle(tr("Easy Word"));  
  23.   
  24.     showWidget = new ShowWidget(this);  
  25.     setCentralWidget(showWidget);  
  26.   
  27.     fontLabel1 = new QLabel(tr("字体: "));  
  28.     fontComboBox = new QFontComboBox;  
  29.     fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);  
  30.   
  31.     fontLabel2 = new QLabel(tr("字号: "));  
  32.     sizeComboBox = new QComboBox;  
  33.   
  34.     QFontDatabase db;  
  35.     foreach (int size ,db.standardSizes())  
  36.     {  
  37.         sizeComboBox->addItem(QString::number(size));  
  38.     }  
  39.   
  40.     boldBtn = new QToolButton;  
  41.     boldBtn->setIcon(QIcon("bold.png"));  
  42.     boldBtn->setCheckable(true);  
  43.     italicBtn = new QToolButton;  
  44.     italicBtn->setIcon(QIcon("italic.png"));  
  45.     italicBtn->setCheckable(true);  
  46.   
  47.     underlineBtn = new QToolButton;  
  48.     underlineBtn->setIcon(QIcon("underline.png"));  
  49.     underlineBtn->setCheckable(true);  
  50.   
  51.     colorBtn = new QToolButton;  
  52.     colorBtn->setIcon(QIcon("color.png"));  
  53.     colorBtn->setCheckable(true);  
  54.   
  55.   
  56.     //文字排版功能  
  57.     listLabel = new QLabel(tr("排序"));  
  58.     listComboBox = new QComboBox;  
  59.     listComboBox->addItem("Standard");  
  60.     listComboBox->addItem("QTextListFormat::ListDisc");  
  61.     listComboBox->addItem("QTextListFormat::ListCircle");  
  62.     listComboBox->addItem("QTextListFormat::ListSquare");  
  63.     listComboBox->addItem("QTextListFormat::ListDecimal");  
  64.     listComboBox->addItem("QTextListFormat::ListLowerAlpha");  
  65.     listComboBox->addItem("QTextListFormat::ListUpperAlpha");  
  66.     listComboBox->addItem("QTextListFormat::ListLowerRoman");  
  67.     listComboBox->addItem("QTextListFormat::ListUpperRoman");  
  68.   
  69.   
  70.   
  71.     createActions();  
  72.     createMenus();  
  73.     createToolBars();  
  74.   
  75.     if(img.load("image.png"))  
  76.     {  
  77.         showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  78.     }  
  79.   
  80.     //各字体设置相关功能的信号与槽  
  81.     connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(showFontComboBox(QString)));  
  82.     connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(showSizeSpinBix(QString)));  
  83.     connect(boldBtn,SIGNAL(clicked(bool)),this,SLOT(showBoldBtn()));  
  84.     connect(italicBtn,SIGNAL(clicked(bool)),this,SLOT(showItalicBtn()));  
  85.     connect(underlineBtn,SIGNAL(clicked(bool)),this,SLOT(showUnderlineBtn()));  
  86.     connect(colorBtn,SIGNAL(clicked(bool)),this,SLOT(showColorBtn()));  
  87.     connect(showWidget->text,SIGNAL(currentCharFormatChanged(QTextCharFormat &)),this,SLOT(showCurrentFormatChanged(QTextCharFormat &)));  
  88.   
  89. //    //文字排版功能的信号与槽  
  90. //    connect(listComboBox,SIGNAL(activated(int)),this,SLOT(showList(int)));  
  91. //    connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));  
  92. //    connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));  
  93. //    connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(showCursorPositionChanged()));  
  94.   
  95.     connect(listComboBox,SIGNAL(activated(int)),this,SLOT(showList(int)));  
  96.     connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));  
  97.     connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));  
  98.     connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(showCursorPositionChanged()));  
  99.   
  100. }  
  101.   
  102. void MainWindow::createActions()  
  103. {  
  104.         //“打开”动作  
  105.        openFileAction = new QAction(QIcon("open.png") , tr("打开") , this);  
  106.        openFileAction->setShortcut(tr("Ctrl + O"));  
  107.        openFileAction->setStatusTip(tr("打开一个文件"));  
  108.        connect(openFileAction,SIGNAL(triggered(bool)),this,SLOT(showOpenFile()));  
  109.   
  110.   
  111.   
  112.        //“新建”动作  
  113.        newFileAction = new QAction(QIcon("new.png") , tr("新建") ,this );  
  114.        newFileAction->setShortcut(tr("Ctrl + N"));  
  115.        newFileAction->setStatusTip(tr("新建一个文件"));  
  116.        connect(newFileAction,SIGNAL(triggered()),this,SLOT(showNewFile()));  
  117.   
  118.   
  119.        //"退出"动作  
  120.        exitAction = new QAction(tr("退出"), this);  
  121.        exitAction->setShortcut(tr("Ctrl + Q"));  
  122.        exitAction->setStatusTip(tr("退出程序"));  
  123.        connect(exitAction,SIGNAL(triggered(bool)),this,SLOT(close()));  
  124.   
  125.        //"复制"动作  
  126.        copyAction = new QAction(QIcon("copy.png") , tr("复制") , this);  
  127.        copyAction->setShortcut(tr("Ctrl + C"));  
  128.        copyAction->setStatusTip(tr("复制文件"));  
  129.        connect(copyAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(copy()));  
  130.   
  131.        //剪切操作  
  132.        cutAction = new QAction(QIcon("cut.png") , tr("剪切") ,this);  
  133.        cutAction->setShortcut(tr("Ctrl + X"));  
  134.        cutAction->setStatusTip(tr("剪切文件"));  
  135.        connect(cutAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(cut()));  
  136.   
  137.        //粘贴操作  
  138.        pasteAction = new QAction(QIcon("paste.png") , tr("粘贴") ,this);  
  139.        pasteAction->setShortcut(tr("Ctrl + V"));  
  140.        pasteAction->setStatusTip(tr("粘贴文件"));  
  141.        connect(pasteAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(paste()));  
  142.   
  143.        //“关于”操作  
  144.        aboutAction = new QAction(tr("关于") ,this);  
  145.        connect(aboutAction, SIGNAL(triggered(bool)),this, SLOT(Qapplication::aboutQt()));  
  146.   
  147.        //打印文本  
  148.        printTextAction = new QAction(QIcon("printText.png") , tr("打印文本") ,this);  
  149.        printTextAction->setStatusTip(tr("打印一个文本"));  
  150.        connect(printTextAction,SIGNAL(triggered(bool)),this,SLOT(showPrintText()));  
  151.   
  152.        //打印图片  
  153.        printImageAction = new QAction(QIcon("printImage.png") , tr("打印图像") , this);  
  154.        printImageAction->setStatusTip(tr("打印一幅图像"));  
  155.        connect(printImageAction,SIGNAL(triggered(bool)),this,SLOT(showPrintImage()));  
  156.   
  157.        //放大图片  
  158.        zoomInAction = new QAction(QIcon("zoomin.png") , tr("放大") ,this);  
  159.        zoomInAction->setStatusTip(tr("放大一张图片"));  
  160.        connect(zoomInAction,SIGNAL(triggered(bool)),this,SLOT(showZoomIn()));  
  161.   
  162.        //缩小图片  
  163.        zoomOutAction = new QAction(QIcon("zoomout.png") ,tr("缩小") ,this);  
  164.        zoomOutAction->setStatusTip(tr("缩小一张图片"));  
  165.        connect(zoomOutAction,SIGNAL(triggered(bool)),this,SLOT(showZoomOut()));  
  166.   
  167.        //旋转图片  
  168.        rotate90Action = new QAction(QIcon("rotate90.png") , tr("旋转90") ,this);  
  169.        rotate90Action->setStatusTip(tr("将一幅图旋转90度"));  
  170.        connect(rotate90Action,SIGNAL(triggered(bool)),this,SLOT(showRotate90()));  
  171.   
  172.        rotate180Action = new QAction(QIcon("rotate180.png") ,tr("旋转180") ,this);  
  173.        rotate180Action->setStatusTip(tr("将一幅图片旋转180度"));  
  174.        connect(rotate180Action,SIGNAL(triggered(bool)),this,SLOT(showRotate180()));  
  175.   
  176.        rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270") ,this);  
  177.        rotate270Action->setStatusTip(tr("将一幅图片旋转270度"));  
  178.        connect(rotate270Action,SIGNAL(triggered(bool)),this,SLOT(showRotate270()));  
  179.   
  180.        //垂直镜像  
  181.        mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png") , tr("纵向镜像") ,this);  
  182.        mirrorVerticalAction->setStatusTip(tr("对一张图片做纵向镜像"));  
  183.        connect(mirrorVerticalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorVertical()));  
  184.   
  185.        //水平镜像  
  186.        mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png") ,tr("横向镜像") ,this);  
  187.        mirrorHorizontalAction->setStatusTip(tr("对一张图片做横向镜像"));  
  188.        connect(mirrorHorizontalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorHorizontal()));  
  189.   
  190.        //撤销操作  
  191.        undoAction = new QAction(QIcon("undo.png"), tr("撤销") ,this);  
  192.        connect(undoAction, SIGNAL(triggered(bool)),showWidget->text,SLOT(undo()));  
  193.   
  194.        //重做操作  
  195.        redoAction = new QAction(QIcon("redo.png") ,tr("重做") ,this);  
  196.        connect(redoAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(redo()));  
  197.   
  198.        //文字排版功能  
  199.        actGrp = new QActionGroup(this);  
  200.   
  201.        leftAction = new QAction(QIcon("left.png"),"左对齐" ,actGrp);  
  202.        leftAction->setCheckable(true);  
  203.   
  204.        rightAction = new QAction(QIcon("right.png") , "右对齐" ,actGrp);  
  205.        rightAction->setCheckable(true);  
  206.   
  207.        centerAction = new QAction(QIcon("center.png") , "居中" ,actGrp);  
  208.        centerAction->setCheckable(true);  
  209.   
  210.        justifyAction = new QAction(QIcon("justify.png") , "两端对齐" , actGrp);  
  211.        justifyAction->setCheckable(true);  
  212.   
  213.        connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*)));  
  214.   
  215. }  
  216.   
  217. void MainWindow::createMenus()  //创建菜单栏  
  218. {  
  219.     fileMenu = menuBar()->addMenu(tr("文件"));  
  220.     fileMenu->addAction(openFileAction);  
  221.     fileMenu->addAction(newFileAction);  
  222.     fileMenu->addAction(printTextAction);  
  223.     fileMenu->addAction(printImageAction);  
  224.     fileMenu->addSeparator();  
  225.     fileMenu->addAction(exitAction);  
  226.   
  227.     zoomMenu = menuBar()->addMenu(tr("编辑"));  
  228.     zoomMenu->addAction(copyAction);  
  229.     zoomMenu->addAction(cutAction);  
  230.     zoomMenu->addAction(pasteAction);  
  231.     zoomMenu->addAction(aboutAction);  
  232.     zoomMenu->addSeparator();  
  233.     zoomMenu->addAction(zoomInAction);  
  234.     zoomMenu->addAction(zoomOutAction);  
  235.   
  236.     rotateMenu = menuBar()->addMenu(tr("旋转"));  
  237.     rotateMenu->addAction(rotate90Action);  
  238.     rotateMenu->addAction(rotate180Action);  
  239.     rotateMenu->addAction(rotate270Action);  
  240.   
  241.     mirrorMenu = menuBar()->addMenu(tr("镜像"));  
  242.     mirrorMenu->addAction(mirrorVerticalAction);  
  243.     mirrorMenu->addAction(mirrorHorizontalAction);  
  244. }  
  245.   
  246. void MainWindow::createToolBars()   //创建工具栏  
  247. {  
  248.     fileTool = addToolBar("File");  
  249.     fileTool->addAction(openFileAction);  
  250.     fileTool->addAction(newFileAction);  
  251.     fileTool->addAction(printTextAction);  
  252.     fileTool->addAction(printImageAction);  
  253.   
  254.     zoomTool = addToolBar("Edit");  //创建文字编辑工具栏  
  255.     zoomTool->addAction(copyAction);  
  256.     zoomTool->addAction(cutAction);  
  257.     zoomTool->addAction(pasteAction);  
  258.     zoomTool->addSeparator();  
  259.     zoomTool->addAction(zoomInAction);  
  260.     zoomTool->addAction(zoomOutAction);  
  261.   
  262.     rotateTool = addToolBar("Rotate");  //创建旋转工具栏  
  263.     rotateTool->addAction(rotate90Action);  
  264.     rotateTool->addAction(rotate180Action);  
  265.     rotateTool->addAction(rotate270Action);  
  266.   
  267.     doToolBar = addToolBar("DoEdit");   //创建撤销和重做工具栏  
  268.     doToolBar->addAction(undoAction);  
  269.     doToolBar->addAction(redoAction);  
  270.   
  271.     fontToolBar = addToolBar("Font");   //创建字体工具栏  
  272.     fontToolBar->addWidget(fontLabel1);  
  273.     fontToolBar->addWidget(fontComboBox);  
  274.     fontToolBar->addWidget(fontLabel2);  
  275.     fontToolBar->addWidget(sizeComboBox);  
  276.     fontToolBar->addSeparator();    //加入分割线  
  277.     fontToolBar->addWidget(boldBtn);  
  278.     fontToolBar->addWidget(italicBtn);  
  279.     fontToolBar->addWidget(underlineBtn);  
  280.     fontToolBar->addSeparator();  
  281.     fontToolBar->addWidget(colorBtn);  
  282.   
  283.     listToolBar = addToolBar("list");   //创建文字排版工具栏  
  284.     listToolBar->addWidget(listLabel);  
  285.     listToolBar->addWidget(listComboBox);  
  286.     listToolBar->addSeparator();  
  287.     listToolBar->addActions(actGrp->actions());  
  288. }  
  289.   
  290. void MainWindow::showNewFile()  //新建文件操作  
  291. {  
  292.      MainWindow *newMainWindow = new MainWindow;  
  293.      newMainWindow->show();  
  294. }  
  295.   
  296. void MainWindow::showOpenFile() //读文本是打开新窗口  
  297. {  
  298.     fileName = QFileDialog::getOpenFileName(this);  
  299.     if(!fileName.isEmpty())  
  300.     {  
  301.         if(showWidget->text->document()->isEmpty())  
  302.         {  
  303.             loadFile(fileName);  
  304.         }  
  305.         else  
  306.         {  
  307.             MainWindow *newMainWindow = new MainWindow;  
  308.             newMainWindow->show();  //当一个窗口已经打开了一个文件,还需要再打开文件时,新生成窗口  
  309.             newMainWindow->loadFile(fileName);  
  310.         }  
  311.     }  
  312. }  
  313.   
  314. void MainWindow::loadFile(QString fileName) //载入文件  
  315. {  
  316.     printf("File name:%s\n",fileName.data());  
  317.   
  318.     QFile file(fileName);  
  319.     if(file.open(QIODevice::ReadOnly | QIODevice::Text))  
  320.     {  
  321.         QTextStream textStream(&file);  
  322.         while(!textStream.atEnd())  
  323.         {  
  324.             showWidget->text->append(textStream.readLine());  
  325.             printf("Read line\n");  
  326.         }  
  327.         printf("End\\n");  
  328.     }  
  329. }  
  330.   
  331.   
  332. void MainWindow::showPrintText()    //打印文本  
  333. {  
  334.     QPrinter printer;  
  335.     QPrintDialog printDialog;  
  336.     if(printDialog.exec())  
  337.     {  
  338.         QTextDocument *doc = showWidget->text->document();  
  339.         doc->print(&printer);  
  340.     }  
  341. }  
  342.   
  343. void MainWindow::showPrintImage()   //打印图片  
  344. {  
  345.     QPrinter printer;  
  346.     QPrintDialog printDialog;  
  347.     if(printDialog.exec())  
  348.     {  
  349.         QPainter    painter(&printer);  
  350.         QRect rect = painter.viewport();  
  351.         QSize size = img.size();  
  352.         size.scale(rect.size() , Qt::KeepAspectRatio);  
  353.   
  354.         painter.setViewport(rect.x() , rect.y(), size.width(), size.height());  
  355.         painter.setWindow(img.rect());  
  356.         painter.drawImage(0,0,img);  
  357.   
  358.     }  
  359. }  
  360.   
  361. void MainWindow::showZoomIn()   //图片放大  
  362. {  
  363.   
  364.     if(img.isNull())  
  365.     {  
  366.         return ;  
  367.     }  
  368.     QMatrix martix;  
  369.     martix.scale(2,2);  
  370.     img = img.transformed(martix);  
  371.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  372.   
  373. }  
  374.   
  375. void MainWindow::showZoomOut()  //图片缩小  
  376. {  
  377.   
  378.     if(img.isNull())  
  379.     {  
  380.         return ;  
  381.     }  
  382.     QMatrix martix;  
  383.     martix.scale(0.5,0.5);  
  384.     img = img.transformed(martix);  
  385.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  386.   
  387. }  
  388.   
  389.   
  390. void MainWindow::showRotate90() //旋转图片功能  
  391. {  
  392.     if(img.isNull())  
  393.     {  
  394.         return;  
  395.     }  
  396.     QMatrix matrix;  
  397.     matrix.rotate(90);  
  398.     img = img.transformed(matrix);  
  399.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  400. }  
  401.   
  402. void MainWindow::showRotate180()  
  403. {  
  404.     if(img.isNull())  
  405.     {  
  406.         return;  
  407.     }  
  408.     QMatrix matrix;  
  409.     matrix.rotate(180);  
  410.     img = img.transformed(matrix);  
  411.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  412. }  
  413.   
  414. void MainWindow::showRotate270()  
  415. {  
  416.     if(img.isNull())  
  417.     {  
  418.         return;  
  419.     }  
  420.     QMatrix matrix;  
  421.     matrix.rotate(270);  
  422.     img = img.transformed(matrix);  
  423.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  424. }  
  425.   
  426. void MainWindow::showMirrorHorizontal() //水平镜像  
  427. {  
  428.     if(img.isNull())  
  429.     {  
  430.         return ;  
  431.     }  
  432.     img = img.mirrored(true,false);  
  433.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  434. }  
  435.   
  436. void MainWindow::showMirrorVertical()   //垂直镜像  
  437. {  
  438.     if(img.isNull())  
  439.     {  
  440.         return ;  
  441.     }  
  442.     img = img.mirrored(falsetrue);  
  443.     showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));  
  444. }  
  445.   
  446. void MainWindow::showFontComboBox(QString comboStr) //设置字体  
  447. {  
  448.     QTextCharFormat fmt;  
  449.     fmt.setFontFamily(comboStr);  
  450.     mergeFormat(fmt);  
  451. }  
  452.   
  453. void MainWindow::mergeFormat(QTextCharFormat format)  
  454. {  
  455.     QTextCursor cursor = showWidget->text->textCursor();  
  456.     if(!cursor.hasSelection())  
  457.     {  
  458.         cursor.select(QTextCursor::WordUnderCursor);  
  459.     }  
  460.     cursor.mergeCharFormat(format);  
  461.     showWidget->text->mergeCurrentCharFormat(format);  
  462. }  
  463.   
  464. void MainWindow::showSizeSpinBix(QString spinvalue) //设置字号  
  465. {  
  466.     QTextCharFormat fmt;  
  467.     fmt.setFontPointSize(spinvalue.toFloat());  
  468.     showWidget->text->mergeCurrentCharFormat(fmt);  
  469. }  
  470.   
  471. void MainWindow::showBoldBtn()  //设置文字加粗  
  472. {  
  473.     QTextCharFormat fmt;  
  474.     fmt.setFontWeight(boldBtn->isCheckable() ? QFont::Bold : QFont::Normal);  
  475.     showWidget->text->mergeCurrentCharFormat(fmt);  
  476. }  
  477.   
  478. void MainWindow::showItalicBtn()  
  479. {  
  480.     QTextCharFormat fmt;  
  481.     fmt.setFontItalic(italicBtn->isCheckable());  
  482.     showWidget->text->mergeCurrentCharFormat(fmt);  
  483. }  
  484.   
  485. void MainWindow::showUnderlineBtn()  
  486. {  
  487.     QTextCharFormat fmt;  
  488.     fmt.setFontUnderline(underlineBtn->isCheckable());  
  489.     showWidget->text->mergeCurrentCharFormat(fmt);  
  490. }  
  491.   
  492. void MainWindow::showColorBtn()  
  493. {  
  494.     QColor color = QColorDialog::getColor(Qt::red, this);  
  495.     if(color.isValid())  
  496.     {  
  497.         QTextCharFormat fmt;  
  498.         fmt.setForeground(color);  
  499.         showWidget->text->mergeCurrentCharFormat(fmt);  
  500.     }  
  501. }  
  502.   
  503. void MainWindow::showCurrentFormatChanged(const QTextCharFormat &fmt)  
  504. {  
  505.     fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));  
  506.     sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));  
  507.     boldBtn->setCheckable(fmt.font().bold());  
  508.     italicBtn->setCheckable(fmt.fontItalic());  
  509.     underlineBtn->setCheckable(fmt.fontUnderline());  
  510. }  
  511.   
  512.   
  513. void MainWindow::showAlignment(QAction *act)  
  514. {  
  515.     if(act == leftAction)  
  516.     {  
  517.         showWidget->text->setAlignment(Qt::AlignLeft);  
  518.     }  
  519.     if(act == rightAction)  
  520.     {  
  521.         showWidget->text->setAlignment(Qt::AlignRight);  
  522.     }  
  523.     if(act == centerAction)  
  524.     {  
  525.         showWidget->text->setAlignment(Qt::AlignCenter);  
  526.     }  
  527.     if(act == justifyAction)  
  528.     {  
  529.         showWidget->text->setAlignment(Qt::AlignJustify);  
  530.     }  
  531.   
  532. }  
  533.   
  534. void MainWindow::showCursorPositionChanged()  
  535. {  
  536.     if(showWidget->text->alignment() == Qt::AlignLeft)  
  537.     {  
  538.         leftAction->setCheckable(true);  
  539.     }  
  540.     if(showWidget->text->alignment() == Qt::AlignRight)  
  541.     {  
  542.         rightAction->setCheckable(true);  
  543.     }  
  544.     if(showWidget->text->alignment() == Qt::AlignCenter)  
  545.     {  
  546.         centerAction->setCheckable(true);  
  547.     }  
  548.     if(showWidget->text->alignment() == Qt::AlignJustify)  
  549.     {  
  550.         justifyAction->setCheckable(true);  
  551.     }  
  552.   
  553. }  
  554.   
  555. void MainWindow::showList(int index)  
  556. {  
  557.     QTextCursor cursor = showWidget->text->textCursor();  
  558.     if(index != 0)  
  559.     {  
  560.         QTextListFormat::Style style = QTextListFormat::ListDisc;  
  561.         switch(index)  
  562.         {  
  563.             default:  
  564.             case 1 :  
  565.                 style = QTextListFormat::ListDisc;  
  566.                 break;  
  567.             case 2:  
  568.                 style = QTextListFormat::ListCircle;  
  569.                 break;  
  570.             case 3:  
  571.                 style = QTextListFormat::ListSquare;  
  572.                 break;  
  573.             case 4:  
  574.                 style = QTextListFormat::ListDecimal;  
  575.                 break;  
  576.             case 5:  
  577.                 style = QTextListFormat::ListLowerAlpha;  
  578.                 break;  
  579.             case 6:  
  580.                 style = QTextListFormat::ListUpperAlpha;  
  581.                 break;  
  582.             case 7:  
  583.                 style = QTextListFormat::ListLowerRoman;  
  584.                 break;  
  585.             case 8:  
  586.                 style = QTextListFormat::ListUpperRoman;  
  587.         }  
  588.   
  589.         cursor.beginEditBlock();    //设置缩进值  
  590.   
  591.         QTextBlockFormat blockFmt = cursor.blockFormat();  
  592.         QTextListFormat listFmt;  
  593.         if(cursor.currentList())  
  594.         {  
  595.             listFmt = cursor.currentList()->format();  
  596.         }  
  597.         else  
  598.         {  
  599.             listFmt.setIndent(blockFmt.indent() + 1);  
  600.             blockFmt.setIndent(0);  
  601.             cursor.setBlockFormat(blockFmt);  
  602.         }  
  603.         listFmt.setStyle(style);  
  604.         cursor.createList(listFmt);  
  605.   
  606.         cursor.endEditBlock();  
  607.     }  
  608.     else  
  609.     {  
  610.         QTextBlockFormat bfmt;  
  611.         bfmt.setObjectIndex(-1);  
  612.         cursor.mergeBlockFormat(bfmt);  
  613.     }  
  614.   
  615. }  
  616.   
  617. MainWindow::~MainWindow()  
  618. {  
  619.   
  620. }  

showwidget.h源代码:

[cpp] view plain copy
 print?
  1. #ifndef SHOWWIDGET_H  
  2. #define SHOWWIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QLabel>  
  6. #include <QTextEdit>  
  7. #include <QImage>  
  8.   
  9. class ShowWidget : public QWidget  
  10. {  
  11.     Q_OBJECT  
  12. public:  
  13.     explicit ShowWidget(QWidget *parent = 0);  
  14.   
  15.     QImage img;  
  16.     QLabel *imageLabel;  
  17.     QTextEdit *text;  
  18.   
  19. signals:  
  20.   
  21. public slots:  
  22. };  
  23.   
  24. #endif // SHOWWIDGET_H  

showwidget.cpp源代码:

[cpp] view plain copy
 print?
  1. #include "showwidget.h"  
  2. #include <QHBoxLayout>  
  3.   
  4. ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)  
  5. {  
  6.     imageLabel = new QLabel;  
  7.     imageLabel->setScaledContents(true);    //设置允许控件自动调整大小,以使内容填充整个有效区域  
  8.   
  9.     text = new QTextEdit;  
  10.   
  11.     QHBoxLayout *mainLayout = new QHBoxLayout(this);    //水平盒子布局  
  12.     mainLayout->addWidget(imageLabel);  
  13.     mainLayout->addWidget(text);  
  14. }  


程序中未解决的bug:

1、文本段落对齐功能有问题,不能实现其功能。(我仔细检查了代码,仍未发现问题所在,路过的大神各显神通吧,望不惜赤脚)。


源代码下载地址:http://download.csdn.net/detail/rl529014/9546463

阅读全文
0 0