29.QT主窗口加widget

运行效果

  • widget布局showwidget.h
     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     //标签
    17     QLabel *imageLabel;
    18     //编辑框
    19     QTextEdit *text;
    20 signals:
    21 
    22 public slots:
    23 
    24 };
    25 
    26 #endif // SHOWWIDGET_H

     

  • showwidget.cpp
     1 #include "showwidget.h"
     2 #include <QHBoxLayout>
     3 ShowWidget::ShowWidget(QWidget *parent) :
     4     QWidget(parent)
     5 {
     6     imageLabel =new QLabel;
     7     imageLabel->setScaledContents(true);
     8 
     9     text =new QTextEdit;
    10 
    11     //创建水平布局
    12     QHBoxLayout *mainLayout =new QHBoxLayout(this);
    13     mainLayout->addWidget(imageLabel);
    14     mainLayout->addWidget(text);
    15 }

     

  • 主窗口创建(工具栏,菜单项) imgprocessor.h
      1 #ifndef IMGPROCESSOR_H
      2 #define IMGPROCESSOR_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 <QToolBar>
     13 #include <QFontComboBox>
     14 #include <QToolButton>
     15 #include <QTextCharFormat>
     16 #include "showwidget.h"
     17 
     18 class ImgProcessor : public QMainWindow
     19 {
     20     Q_OBJECT
     21 
     22 public:
     23     ImgProcessor(QWidget *parent = 0);
     24     ~ImgProcessor();
     25 
     26     void createActions();                            //创建动作
     27     void createMenus();                               //创建菜单
     28     void createToolBars();                          //创建工具栏
     29 
     30     void loadFile(QString filename);
     31     void mergeFormat(QTextCharFormat);
     32 
     33 private:
     34     //创建工具栏动作
     35     QMenu *fileMenu;                                   //文件菜单栏
     36     QMenu *zoomMenu;                                //编辑菜单栏
     37     QMenu *rotateMenu;                              //选择菜单栏
     38     QMenu *mirrorMenu;                              //镜像菜单栏
     39 
     40     QImage img;                                     //图片资源
     41     QString fileName;                               //文件名
     42     ShowWidget *showWidget;                         //显示窗口
     43 
     44     QAction *openFileAction;                         //打开文件
     45     QAction *NewFileAction;                         //新建文件
     46     QAction *PrintTextAction;                       //打印文本
     47     QAction *PrintImageAction;                      //打印图片
     48     QAction *exitAction;                            //退出
     49 
     50     QAction *copyAction;                              //复制
     51     QAction *cutAction;                             //剪切
     52     QAction *pasteAction;                           //粘贴
     53     QAction *aboutAction;                           //关于
     54     QAction *zoomInAction;                          //放大
     55     QAction *zoomOutAction;                         //缩小
     56 
     57     QAction *rotate90Action;                         //旋转90度
     58     QAction *rotate180Action;                       //旋转180度
     59     QAction *rotate270Action;                       //旋转270度
     60 
     61     QAction *mirrorVerticalAction;                  //纵向镜像
     62     QAction *mirrorHorizontalAction;                //横向镜像
     63 
     64     QAction *undoAction;
     65     QAction *redoAction;
     66 
     67     QToolBar *fileTool;                              //文件工具栏
     68     QToolBar *zoomTool;                             //复制粘贴工具栏
     69     QToolBar *rotateTool;                           //放大缩小工具栏
     70     QToolBar *mirrorTool;                           //旋转工具栏
     71 
     72     QToolBar *doToolBar;                            //撤回工具栏
     73 
     74     QLabel *fontLabel1;                             //字体设置项
     75     QFontComboBox *fontComboBox;
     76     QLabel *fontLabel2;                             //字号设置项
     77     QComboBox *sizeComboBox;
     78     QToolButton *boldBtn;                           //加粗
     79     QToolButton *italicBtn;                         //倾斜
     80     QToolButton *underlineBtn;                      //下划线
     81     QToolButton *colorBtn;                          //颜色
     82 
     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 
     93     QToolBar *listToolBar;                          //排序工具栏
     94 
     95 protected slots:
     96     //显示文件
     97     void ShowNewFile();
     98     //打开文件
     99     void ShowOpenFile();
    100     //显示文本
    101     void ShowPrintText();
    102     //显示图片
    103     void ShowPrintImage();
    104     void ShowZoomIn();
    105     void ShowZoomOut();
    106     //旋转九十度
    107     void ShowRotate90();
    108     //旋转180度
    109     void ShowRotate180();
    110     //旋转270度
    111     void ShowRotate270();
    112     //纵向镜像
    113     void ShowMirrorVertical();
    114     //横向镜像
    115     void ShowMirrorHorizontal();
    116     //设置字体框
    117     void ShowFontComboBox(QString comboStr);
    118     //设置大小框
    119     void ShowSizeSpinBox(QString spinValue);
    120     //设置字体加粗
    121     void ShowBoldBtn();
    122     //设置倾斜
    123     void ShowItalicBtn();
    124     //设置下划线
    125     void ShowUnderlineBtn();
    126     //设置颜色框
    127     void ShowColorBtn();
    128     void ShowCurrentFormatChanged(const QTextCharFormat &fmt);
    129 
    130     void ShowList(int);
    131     void ShowAlignment(QAction *act);
    132     void ShowCursorPositionChanged();
    133 };
    134 
    135 #endif // IMGPROCESSOR_H

     

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

     

  • main.cpp
     1 #include "showwidget.h"
     2 #include "imgprocessor.h"
     3 #include <QApplication>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8     //ShowWidget w;
     9     ImgProcessor w;
    10     w.show();
    11 
    12     return a.exec();
    13 }

     

posted @ 2018-04-10 13:14  喵小喵~  阅读(4784)  评论(0编辑  收藏  举报