标签:
源码下载:http://pan.baidu.com/s/1hs2lx68
程序效果截图如下:
代码如下:
1 //imgprocessor.h 2 #ifndef IMGPROCESSOR_H 3 #define IMGPROCESSOR_H 4 5 #include <QMainWindow> 6 #include <QApplication> 7 #include <QImage> 8 #include <QLabel> 9 #include <QMenu> 10 #include <QMenuBar> 11 #include <QSaveFile> 12 #include <QAction> 13 #include <QActionGroup> 14 #include <QComboBox> 15 #include <QSpinBox> 16 #include <QToolBar> 17 #include <QToolButton> 18 #include <QFontComboBox> 19 #include "showwidget.h" 20 21 class ImgProcessor : public QMainWindow 22 { 23 Q_OBJECT 24 25 public: 26 ImgProcessor(QWidget *parent = 0); 27 ~ImgProcessor(); 28 void createActions(); //创建动作 29 void createMenus(); //创建菜单 30 void createToolBars(); //创建工具栏 31 void loadFile(QString filename); //加载文件 32 void mergeFormat(QTextCharFormat); //合并格式 33 private: 34 //各项菜单栏 35 QMenu *fileMenu; //文件菜单 36 QMenu *zoomMenu; //缩放菜单 37 QMenu *rotateMenu; //旋转 38 QMenu *mirrorMenu; //镜像 39 //放置图片 40 QImage img; 41 42 QString fileName; //打开文件名 43 QString curFileName; //保存文件名 44 //显示中心部件 45 ShowWidget *showWidget; 46 47 //文件菜单项 48 QAction *openFileAction; //打开文件动作 49 QAction *NewFileAction; //新建文件 50 QAction *SaveAction; //保存文件 51 QAction *SaveAsAction; //另存为文件 52 QAction *PrintTextAction; //打印文本动作 53 QAction *PrintImageAction; //打印图片 54 QAction *exitAction; //退出动作 55 56 //编辑菜单项 57 QAction *copyAction; //复制动作 58 QAction *cutAction; //剪贴 59 QAction *pasteAction; //粘贴 60 QAction *aboutAction; //关于 61 QAction *zoomInAction; //放大 62 QAction *zoomOutAction; //缩小 63 64 //旋转菜单栏--图像处理 65 QAction *rotate90Action; //90° 66 QAction *rotate180Action; //180° 67 QAction *rotate270Action; //270° 68 69 //镜像菜单栏 70 QAction *mirrorVerticalAction; //垂直镜像 71 QAction *mirrorHorizonAction; //水平镜像 72 QAction *undoAction; //撤销 73 QAction *redoAction; //取消撤销 74 75 //工具栏 76 QToolBar *fileTool; //文件-打开、新建、保存,另存为,打印文本(图像) 77 QToolBar *zoomTool; //缩放 78 QToolBar *rotateTool; //旋转 79 QToolBar *mirrorTool; //镜像 80 QToolBar *doToolBar; //撤销、回撤--操作 81 82 //文本编辑 83 QLabel *fontLabel; //字体设置项 84 QFontComboBox *fontComboBox; //字体选框 85 QLabel *fontLabel2; //大小 86 QComboBox *sizeComboBox; //大小选框 87 QToolButton *boldBtn; //加粗按钮 88 QToolButton *italicBtn; //斜体 89 QToolButton *underlineBtn; //下划线 90 QToolButton *colorBtn; //字体颜色 91 QToolBar *fontToolBar; //字体工具栏 92 93 //排序设置项 94 QLabel *listLabel; 95 QComboBox *listComboBox; 96 QActionGroup *actGrp; 97 QAction *leftAction; //居左 98 QAction *rightAction; //局右 99 QAction *centerAction; //居中 100 QAction *justifyAction; //两端对齐 101 QToolBar *listToolBar; //排序工具栏 102 103 protected slots: 104 void ShowNewFile(); //新建文件 105 void ShowOpenFile(); //打开文件 106 void ShowSaveFile(); //保存文件 107 void ShowSaveAsFile(); //另存为 108 void ShowPrintText(); //打印文本 109 void ShowPrintImage(); //打印图像 110 void ShowZoomIn(); //放大功能 111 void ShowZoomOut(); //缩小功能 112 void ShowRotate90(); //旋转90° 113 void ShowRotate180(); 114 void ShowRotate270(); 115 void ShowMirrorVertical(); //镜像--纵向镜像 116 void ShowMirrorHorizontal(); //横向镜像 117 void ShowFontComboBox(QString comboStr); //显示字体框 118 void ShowSizeSpinBox(QString spinValue); //大小自旋盒 119 void ShowBoldBtn(); //加粗功能 120 void ShowItalicBtn(); //显示斜体 121 void ShowUnderlineBtn(); //下划线 122 void ShowColorBtn(); 123 //字符格式化 124 void ShowCurrentFormatChanged(const QTextCharFormat &fmt); 125 126 void ShowList(int); //排序 127 void ShowAlignment(QAction *act); //对齐方式 128 void ShowCursorPositionChanged(); //显示光标位置 129 130 }; 131 132 #endif // IMGPROCESSOR_H
1 //showWidget.h 2 #ifndef SHOWWIDGET_H 3 #define SHOWWIDGET_H 4 5 #include <QWidget> 6 #include <QLabel> 7 #include <QTextEdit> //文本编辑框 8 #include <QImage> //插入图片 9 10 class ShowWidget : public QWidget 11 { 12 Q_OBJECT 13 public: 14 explicit ShowWidget(QWidget *parent = 0); 15 QImage *img; 16 QLabel *imageLabel; 17 QTextEdit *text; 18 19 signals: 20 21 public slots: 22 }; 23 24 #endif // SHOWWIDGET_H
1 //imgprocessor.cpp 2 #include "imgprocessor.h" 3 #include <QFileDialog> 4 #include <QFile> 5 #include <QTextStream> 6 #include <QPrintDialog> 7 #include <QPrinter> 8 #include <QPainter> 9 #include <QColor> 10 #include <QColorDialog> 11 #include <QTextList> 12 #include <QMessageBox> 13 #include <QDebug> 14 15 ImgProcessor::ImgProcessor(QWidget *parent) 16 : QMainWindow(parent) 17 { 18 setWindowTitle (tr("Easy Word")); //设置窗体标题 19 //创建放置图像QLabel和文本编辑器QTextEdit的QWidget对象showWidget,并将该QWidget对象设置 20 //为中心部件 21 showWidget = new ShowWidget(this); 22 setCentralWidget (showWidget); 23 curFileName = tr(""); //初始化文件名 24 25 //排序 26 listLabel = new QLabel(tr("排序")); 27 listComboBox = new QComboBox; 28 listComboBox->addItem ("Standard"); 29 listComboBox->addItem ("QTextListFormat::ListDisc"); //圆盘 30 listComboBox->addItem ("QTextListFormat::ListCircle"); //圆 31 listComboBox->addItem ("QTextListFormat::ListSquare"); //方形 32 listComboBox->addItem ("QTextListFormat::ListDecimal"); //十进制 33 listComboBox->addItem ("QTextListformat::ListLowerAlpha"); //小写字母 34 listComboBox->addItem ("QTextListFormat::ListUpperAlpha"); //大写字母 35 listComboBox->addItem ("QTextListformat::ListLowerRoman"); //小写罗马字符 36 listComboBox->addItem ("QTextListFormat::ListUpperRoman"); //大写罗马字符 37 38 //在工具栏上嵌入控件:字体,大小,粗体,斜体,字体颜色 39 //字体--这里的字体,字号仅仅是下拉列表框改变 40 fontLabel = new QLabel(tr("字体:")); 41 fontComboBox = new QFontComboBox; 42 //setFontFilter--接口过滤(只在下拉列表框中显示某一类字体,默认情况下为QFontComboBox::AllFonts 43 //列出所有字体 44 fontComboBox->setFontFilters (QFontComboBox::ScalableFonts); 45 46 //字号 47 fontLabel2 = new QLabel(tr("字号:")); 48 sizeComboBox = new QComboBox; 49 /**QFontDatabase-实现在字号下拉列表框中填充各种不同的字号条目 50 * 其用于表示当前系统中所有可用的格式信息,主要是字体和字号大小 51 * provide information about the fonts avaliable in the underlying(潜在) window system*/ 52 QFontDatabase db; 53 54 //standardSize(): return a list of standard font size(返回可用标准字号的列表). 55 foreach (int size, db.standardSizes ()) 56 sizeComboBox->addItem (QString::number (size)); //将它们插入到字号下拉框中 57 58 //加粗 59 boldBtn = new QToolButton; 60 boldBtn->setIcon (QIcon("bold.png")); 61 //设置成是否是开关(toggle)按钮(true) 62 boldBtn->setCheckable (true); 63 64 //斜体 65 italicBtn = new QToolButton; 66 italicBtn->setIcon (QIcon("italic.png")); 67 italicBtn->setCheckable (true); 68 69 //下划线 70 underlineBtn = new QToolButton; 71 underlineBtn->setIcon (QIcon("underline.png")); 72 underlineBtn->setCheckable (true); 73 74 //颜色 75 colorBtn = new QToolButton; 76 colorBtn->setIcon (QIcon("color.png")); 77 colorBtn->setCheckable (true); 78 79 /* 创建动作、菜单、工具栏函数 */ 80 createActions (); 81 createMenus (); 82 createToolBars (); 83 if (img.load ("image.png")) 84 { 85 //在imageLabel对象放置图像 86 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 87 } 88 89 //连接槽函数 90 connect (fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString))); 91 connect (sizeComboBox, SIGNAL(activated(QString)), this, SLOT(ShowSizeSpinBox(QString))); 92 connect (boldBtn, SIGNAL(clicked(bool)), this, SLOT(ShowBoldBtn())); 93 connect (italicBtn, SIGNAL(clicked(bool)), this, SLOT(ShowItalicBtn())); 94 connect (underlineBtn, SIGNAL(clicked(bool)), this, SLOT(ShowUnderlineBtn())); 95 connect (colorBtn, SIGNAL(clicked(bool)), this , SLOT(ShowColorBtn())); 96 97 //设置字符格式--之前的字符的字体,字号,仅仅是下拉列表框的改变, 还需要调用此函数改变文本字体 98 //当光标所在字符格式发生变化时调用此槽函数,函数根据 新的字符格式 将 工具栏 各个格式的显示更新 99 connect (showWidget->text, SIGNAL(currentCharFormatChanged(QTextCharFormat)), 100 this, SLOT(ShowCurrentFormatChanged(QTextCharFormat))); 101 102 connect (listComboBox, SIGNAL(activated(int)), this, SLOT(ShowList(int))); 103 //连接撤销信号和 redo对象的槽函数相连 104 connect (showWidget->text->document (), SIGNAL(undoAvailable(bool)), 105 redoAction, SLOT(setEnabled(bool))); 106 connect (showWidget->text->document (), SIGNAL(redoAvailable(bool)), 107 redoAction, SLOT(setEnabled(bool))); 108 connect (showWidget->text, SIGNAL(cursorPositionChanged()), 109 this, SLOT(ShowCursorPositionChanged())); 110 } 111 112 //动作(Action)的实现 113 void ImgProcessor::createActions () 114 { 115 //基于文件操作的动作(Action)的代码 116 //“打开”动作 117 //在创建“打开文件”动作的同时,指定了此动作使用的图标、名称及父窗口 118 openFileAction = new QAction(QIcon("open.png"), tr("打开"), this); 119 //设定此动作的组合键为【Ctrl+O】。 120 openFileAction->setShortcut (tr("Ctrl+O")); 121 //设定了状态条显示,当鼠标移至此动作对应的菜单条目或工具栏按钮上时,在状态条上显示"打开xxx"提示 122 openFileAction->setStatusTip (tr("打开一个文件")); 123 connect (openFileAction, SIGNAL(triggered(bool)), this, SLOT(ShowOpenFile())); 124 125 //“新建”动作 126 NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this); 127 NewFileAction->setShortcut (tr("Ctrl+N")); 128 NewFileAction->setStatusTip (tr("新建一个文件")); 129 connect (NewFileAction, SIGNAL(triggered(bool)), this, SLOT(ShowNewFile())); 130 131 //“保存”动作 132 SaveAction = new QAction(QIcon("save.png"), tr("保存"), this); 133 SaveAction->setShortcut (tr("Ctrl+S")); 134 SaveAction->setStatusTip (tr("保存一个文件")); 135 connect (SaveAction, SIGNAL(triggered(bool)), this, SLOT(ShowSaveFile())); 136 137 //“另存为”动作 138 SaveAsAction = new QAction(tr("另存为"), this); 139 SaveAsAction->setShortcut (tr("Ctrl+alt+S")); 140 SaveAsAction->setStatusTip (tr("另存为")); 141 connect (SaveAsAction, SIGNAL(triggered(bool)), this, SLOT(ShowSaveAsFile())); 142 143 //“退出”动作 144 exitAction = new QAction(tr("退出"), this); 145 exitAction->setShortcut (tr("Ctrl+Q")); 146 exitAction->setStatusTip (tr("退出程序")); 147 connect (exitAction, SIGNAL(triggered(bool)), this, SLOT(close())); 148 149 //"复制"动作 150 copyAction = new QAction(QIcon("copy.png"), tr("复制"), this); 151 copyAction->setShortcut (tr("Ctrl+C")); 152 copyAction->setStatusTip (tr("复制")); 153 //连接引发信号 和 中心部件的文本编辑框 复制信号 154 connect (copyAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(copy())); 155 156 //“剪切”信号 157 cutAction = new QAction(QIcon("cut.png"), tr("剪贴"), this); 158 cutAction->setShortcut (tr("Ctrl+X")); 159 cutAction->setStatusTip (tr("剪切文件")); 160 connect (cutAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(cut())); 161 162 //"粘贴"动作 163 pasteAction = new QAction(QIcon("paste.png"), tr("粘贴"), this); 164 pasteAction->setShortcut (tr("Ctrl+V")); 165 pasteAction->setStatusTip (tr("粘贴文件")); 166 connect (pasteAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(paste())); 167 168 //“关于"动作 169 aboutAction = new QAction(tr("关于"), this); 170 connect (aboutAction, SIGNAL(triggered(bool)), this, SLOT(QApplication::aboutQt ())); 171 172 //实现打印文本和图像、图像缩放和镜像的动作(Action)的代码 173 //“打印文本”动作 174 PrintTextAction = new QAction(QIcon("printText.png"), tr("打印文本"), this); 175 PrintTextAction->setStatusTip (tr("打印一个文本")); 176 connect (PrintTextAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintText())); 177 178 //“打印图像”动作 179 PrintImageAction = new QAction(QIcon("printImage.png"), tr("打印图像"), this); 180 PrintImageAction->setStatusTip (tr("打印一幅图像")); 181 connect (PrintImageAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintImage())); 182 183 //“放大”动作 184 zoomInAction = new QAction(QIcon("zoomin.png"), tr("放大"), this); 185 zoomInAction->setStatusTip (tr("放大一张图片")); 186 connect (zoomInAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomIn())); 187 188 //"缩小"动作 189 zoomOutAction = new QAction(QIcon("zoomout.png"), tr("缩小"), this); 190 zoomOutAction->setStatusTip (tr("缩小一张图片")); 191 connect (zoomOutAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomOut())); 192 193 //实现图像旋转动作(Action) 194 //旋转90° 195 rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋转 90°"), this); 196 rotate90Action->setStatusTip (tr("将一幅图旋转 90°")); 197 connect (rotate90Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate90())); 198 199 //旋转180° 200 rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋转 180°"), this); 201 rotate180Action->setStatusTip (tr("将一幅图旋转 180°")); 202 connect (rotate180Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate180())); 203 204 //旋转270° 205 rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转 270°"), this); 206 rotate270Action->setStatusTip (tr("将一幅图旋转 270°")); 207 connect (rotate270Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate270())); 208 209 //实现图像镜像的动作(Action) 210 //纵向镜像 211 mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png"), tr("纵向镜像"), this); 212 mirrorVerticalAction->setStatusTip (tr("对一幅图做纵镜像")); 213 connect (mirrorVerticalAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorVertical())); 214 215 //横向镜像 216 mirrorHorizonAction = new QAction(QIcon("mirrorHorizontal.png"), tr("横向镜像"), this); 217 mirrorHorizonAction->setStatusTip (tr("对一幅图做横镜像")); 218 connect (mirrorHorizonAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorHorizontal())); 219 220 //实现撤销和恢复的动作(Action) 221 undoAction = new QAction(QIcon("undo.png"), "撤销", this); 222 connect (undoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(undo())); 223 redoAction = new QAction(QIcon("redo.png"), "重做", this); 224 connect (redoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(redo())); 225 226 //排序: 左对齐、右对齐、居中、两端对齐 227 //setCheckable()只能是可选择的,或不能选择的.选中了,就不能取消选中状态(不可复选) 228 //setChecked()可以是选中的,或者是非选中的(可复选) 229 actGrp = new QActionGroup(this); 230 leftAction = new QAction(QIcon("left.png"), "左对齐", actGrp); 231 leftAction->setCheckable (true); 232 rightAction = new QAction(QIcon("right.png"), "右对齐", actGrp); 233 rightAction->setCheckable (true); 234 centerAction = new QAction(QIcon("center.png"), "居中", actGrp); 235 centerAction->setCheckable (true); 236 justifyAction = new QAction(QIcon("justify.png"), "两端对齐", actGrp); 237 justifyAction->setCheckable (true); 238 //按下某个对齐按钮的响应,根据比较判断触发的是哪个对齐按钮 239 connect (actGrp, SIGNAL(triggered(QAction*)), this, SLOT(ShowAlignment(QAction*))); 240 241 } 242 243 //实现了各个动作之后,需要将它们通过菜单,工具栏或快捷键的方式体现出来. 244 //菜单的实现 245 void ImgProcessor::createMenus () 246 { 247 //文件菜单 248 //直接调用QMainWindow的menuBar()函数即可得到主窗口的菜单条指针,再调用菜单条 249 //QMenuBar的addMenu()函数,即可完成在菜单条中插入一个新菜单fileMenu,fileMenu()函数为一个 250 //QMenu类对象 251 fileMenu = menuBar ()->addMenu (tr("文件")); 252 fileMenu->addAction(openFileAction); 253 fileMenu->addAction(NewFileAction); 254 fileMenu->addAction(SaveAction); 255 fileMenu->addAction(SaveAsAction); 256 fileMenu->addAction(PrintTextAction); 257 fileMenu->addAction(PrintImageAction); 258 fileMenu->addSeparator (); //添加分割 259 fileMenu->addAction(exitAction); 260 //编辑菜单(缩放...) 261 zoomMenu = menuBar ()->addMenu (tr("编辑")); 262 zoomMenu->addAction(copyAction); 263 zoomMenu->addAction(cutAction); 264 zoomMenu->addAction(pasteAction); 265 zoomMenu->addAction(aboutAction); 266 zoomMenu->addSeparator (); 267 zoomMenu->addAction(zoomInAction); 268 zoomMenu->addAction(zoomOutAction); 269 //旋转菜单 270 rotateMenu = menuBar ()->addMenu (tr("旋转")); 271 rotateMenu->addAction(rotate90Action); 272 rotateMenu->addAction(rotate180Action); 273 rotateMenu->addAction(rotate270Action); 274 //镜像菜单 275 mirrorMenu = menuBar ()->addMenu (tr("镜像")); 276 mirrorMenu->addAction(mirrorVerticalAction); 277 mirrorMenu->addAction(mirrorHorizonAction); 278 } 279 280 //工具栏的实现(ToolBars)的实现 281 void ImgProcessor::createToolBars () 282 { 283 //文件工具条 284 //直接调用QMainWindow的addToolBar()函数即可获得主窗口的工具条对象,每新增一个工具条 285 //调用一次addToolBar()函数,赋予不同的名称,即可在主窗口中新增一个工具条 286 fileTool = addToolBar ("File"); 287 //调用QToolBar的addAction()函数在工具条中插入属于本工具条的动作. 288 //这些工具条可以在工具栏单击右键,将弹出工具条显示的选择菜单 289 fileTool->addAction(openFileAction); 290 fileTool->addAction(NewFileAction); 291 fileTool->addAction(SaveAction); 292 fileTool->addAction(PrintTextAction); 293 fileTool->addAction(PrintImageAction); 294 //编辑工具条 295 zoomTool = addToolBar ("Edit"); 296 zoomTool->addAction(copyAction); 297 zoomTool->addAction(cutAction); 298 zoomTool->addAction(pasteAction); 299 zoomTool->addSeparator (); 300 zoomTool->addAction(zoomInAction); 301 zoomTool->addAction(zoomOutAction); 302 //旋转工具条 303 rotateTool = addToolBar ("rotate"); 304 rotateTool->addAction(rotate90Action); 305 rotateTool->addAction(rotate180Action); 306 rotateTool->addAction(rotate270Action); 307 //撤销和重做工具条 308 doToolBar = addToolBar ("doEdit"); 309 doToolBar->addAction(undoAction); 310 doToolBar->addAction(redoAction); 311 /*** 312 * 工具条是可移动窗口,停靠的区域由QToolBar的allowAreas决定 313 * Qt::LeftToolBarArea、Qt::RightToolBarArea、Qt::TopToolBarArea、 314 * Qt::BottomToolBarArea, Qt::AllToolBarAreas(默认)。 315 * 可以通过调用setAllowAreas()函数来指定工具条可停靠的区域 316 * 如:fileTool->setAllowAreas(Qt::TopToolBarArea|Qt::LeftToolBarArea); 317 * 来限定fileTool只能出现在主窗口的顶部或者左侧.工具条也可通过setMovable()设定可移动性: 318 * 如fileTool->setMovable (false)来指定文件工具条不可移动 */ 319 320 //字体工具条 321 fontToolBar = addToolBar ("Font"); 322 fontToolBar->addWidget (fontLabel); 323 fontToolBar->addWidget (fontComboBox); 324 fontToolBar->addWidget (fontLabel2); 325 fontToolBar->addWidget (sizeComboBox); 326 fontToolBar->addSeparator (); 327 fontToolBar->addWidget (boldBtn); 328 fontToolBar->addWidget (italicBtn); 329 fontToolBar->addWidget (underlineBtn); 330 fontToolBar->addSeparator (); 331 fontToolBar->addWidget (colorBtn); 332 333 //排序工具条 334 listToolBar = addToolBar ("list"); 335 listToolBar->addWidget (listLabel); 336 listToolBar->addWidget (listComboBox); 337 listToolBar->addSeparator (); 338 listToolBar->addActions(actGrp->actions ()); 339 } 340 341 //文件操作功能 342 //新建文件 343 void ImgProcessor::ShowNewFile () 344 { 345 ImgProcessor *newImgProcessor = new ImgProcessor; 346 newImgProcessor->show (); 347 } 348 349 //打开文件 350 void ImgProcessor::ShowOpenFile () 351 { 352 fileName = QFileDialog::getOpenFileName(this, "打开"); 353 curFileName = fileName; 354 if (!fileName.isEmpty ()) //文件名不为空 355 { 356 //原窗口内容为空,则直接加载该文件 357 if (showWidget->text->document ()->isEmpty ()) 358 { 359 loadFile (fileName); //loadFile()函数实现在下方. 360 } 361 //否则,打开新窗口加载文件 362 else 363 { 364 ImgProcessor *newImgProcessor = new ImgProcessor; 365 newImgProcessor->show (); 366 newImgProcessor->loadFile (fileName); 367 } 368 } 369 } 370 371 //保存文件 372 void ImgProcessor::ShowSaveFile () 373 { 374 if (curFileName.isEmpty ()) //原路径文件名 375 { 376 QString filename = QFileDialog::getSaveFileName (this, tr("保存文件"), 377 QString(), tr("文本文件(*.txt);;c++文件(*h *cpp *hpp")); 378 if (!filename.isEmpty ()) { 379 QFile file(filename); 380 if (!file.open (QIODevice::WriteOnly)) { 381 QMessageBox::critical (this, tr("错误"), tr("不能打开文件")); 382 return; 383 } 384 curFileName = filename; //下次保存,默认同一条路径 385 QTextStream outs(&file); 386 outs << showWidget->text->toPlainText (); 387 outs.flush (); 388 file.close (); 389 } 390 } 391 else { 392 QFile file(curFileName); 393 if (!file.open (QIODevice::WriteOnly)) { 394 QMessageBox::critical (this, tr("错误"), tr("不能打开文件")); 395 return; 396 } 397 QTextStream outs(&file); 398 outs << showWidget->text->toPlainText (); 399 outs.flush (); 400 file.close (); 401 } 402 403 } 404 405 //另存为 406 void ImgProcessor::ShowSaveAsFile () 407 { 408 QString filename = QFileDialog::getSaveFileName (this, tr("保存文件"), 409 QString(), tr("文本文件(*.txt);;c++文件(*.h *.cpp *.hpp)")); 410 if (!filename.isEmpty ()) { 411 QFile file(filename); 412 if (!file.open (QIODevice::WriteOnly)) { 413 QMessageBox::critical (this, tr("错误"), tr("不能打开文件")); 414 return; 415 } 416 QTextStream outs(&file); 417 //将text->toPlainText()返回文本编辑的纯文本,outs<<将纯文本写入流 418 outs << showWidget->text->toPlainText (); 419 outs.flush (); 420 file.close (); 421 } 422 } 423 424 //loadFile()函数实现,利用QFile和QTextStream完成具体读取文件内容工作 425 void ImgProcessor::loadFile (QString filename) 426 { 427 printf ("file name:%s\n", (char*)filename.data ()); 428 QFile file(filename); //获取文件 429 if (file.open (QIODevice::ReadOnly | QIODevice::Text)) 430 { 431 //提供基于文本流的功能(对于ASCII文本) 432 QTextStream textStream(&file); 433 while (!textStream.atEnd ()) //判断是否到文件流或文件终点 434 { 435 //读取流中的一行,并返回包含文本的返回的字符串 436 //(不包含任何换行符合回车符-不同于QIODevice::readline()--不会省略最后换行符). 437 showWidget->text->append (textStream.readLine ()); 438 printf("read line\n"); 439 } 440 printf("end\n"); 441 } 442 } 443 444 //打印文本 445 void ImgProcessor::ShowPrintText () 446 { 447 QPrinter printer; 448 //创建一个QPrintDialog对象,参数为QPrinter对象 449 QPrintDialog printDialog(&printer, this); 450 //判断打印对话框显示后用户是否单击“打印”,打印--则相关打印属性将可以通过创建QPrintDialog 451 //对象时,使用的QPrinter对象获得;单击取消,则不执行后续的打印操作 452 if (printDialog.exec ()) 453 { 454 //获得QTextEdit对象的文档 455 QTextDocument *doc = showWidget->text->document (); 456 //打印 457 doc->print (&printer); 458 } 459 460 } 461 462 //打印图像 463 void ImgProcessor::ShowPrintImage () 464 { 465 QPrinter printer; 466 QPrintDialog printDialog(&printer, this); 467 if (printDialog.exec ()) 468 { 469 //创建一个QPainter对象,并指定绘图设备为一个QPrinter对象 470 QPainter painter(&printer); 471 //获得QPainter对象的视图矩形区域 472 QRect rect = painter.viewport (); 473 474 QSize size = img.size (); //获得图像大小 475 /* 按照图形的比例大小重新设置视图矩形区域 */ 476 //Qt::KeepAspectRatio: 按照图形比例大小,保持宽高比例,尽可能大的在矩形内 477 size.scale (rect.size (), Qt::KeepAspectRatio); //图像大小改变 478 //重新得到painter对象的视图矩形区域 479 //设置QPainter的视图矩形区域at(x,y) with the given width and height 480 painter.setViewport (rect.x (), rect.y (), size.width (), size.height ()); 481 painter.setWindow (img.rect ()); //设置QPainter窗口大小为图像的大小 482 painter.drawImage (0, 0, img); //打印图像at(0,0) 483 } 484 } 485 486 void ImgProcessor::ShowZoomIn () 487 { 488 if (img.isNull ()) //有效性判断 489 return; 490 QMatrix matrix; //声明一个QMatrix类的实例 491 //按照2倍比例对水平和垂直方向进行放大,并将当前显示的图形按照该坐标矩阵进行转换 492 matrix.scale (2, 2); 493 img = img.transformed (matrix); 494 //重新设置显示图形 495 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 496 } 497 498 void ImgProcessor::ShowZoomOut () 499 { 500 if (img.isNull ()) 501 return; 502 QMatrix matrix; 503 matrix.scale (0.5, 0.5); 504 img = img.transformed (matrix); 505 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 506 507 } 508 509 //旋转90° 510 void ImgProcessor::ShowRotate90 () 511 { 512 if (img.isNull ()) 513 return; 514 QMatrix matrix; 515 matrix.rotate (90); 516 img = img.transformed (matrix); 517 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 518 } 519 520 void ImgProcessor::ShowRotate180 () 521 { 522 if (img.isNull ()) 523 return; 524 QMatrix matrix; 525 matrix.rotate (180); 526 img = img.transformed (matrix); 527 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 528 } 529 530 void ImgProcessor::ShowRotate270 () 531 { 532 if (img.isNull ()) 533 return; 534 QMatrix matrix; 535 matrix.rotate (270); 536 img = img.transformed (matrix); 537 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 538 } 539 540 //镜像--垂直 541 void ImgProcessor::ShowMirrorVertical () 542 { 543 if (img.isNull ()) 544 return; 545 //Qimage Qimage::mirrored(bool horizontal = false, bool vertical = true); 546 //垂直镜像 547 img = img.mirrored (false, true); 548 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 549 } 550 551 //水平镜像 552 void ImgProcessor::ShowMirrorHorizontal () 553 { 554 if (img.isNull ()) 555 return; 556 //Qimage Qimage::mirrored(bool horizontal = true, bool vertical = false); 557 //水平镜像 558 img = img.mirrored (true, false); 559 showWidget->imageLabel->setPixmap (QPixmap::fromImage (img)); 560 } 561 562 //设置字体格式 563 //字体 564 void ImgProcessor::ShowFontComboBox (QString comboStr) 565 { 566 QTextCharFormat fmt; //创建一个QTextCharFormat对象 567 fmt.setFontFamily (comboStr); //选择的字体名称设置给QTextCharFormat对象 568 mergeFormat (fmt); //将新格式应用到光标选区内的字符 569 } 570 571 //作用于全局 572 //对于所有的QTextDocumet进行的修改都通过QTextCursor类来完成 573 void ImgProcessor::mergeFormat (QTextCharFormat format) 574 { 575 QTextCursor cursor = showWidget->text->textCursor (); //获得编辑框中的光标 576 //若光标没有高亮区,则光标所在处的词为选区(由前后有空格,“,”,“、”,“."等标点分隔 577 if (!cursor.hasSelection ()) 578 cursor.select (QTextCursor::WordUnderCursor); 579 //将format所表示的格式应用到光标所在处的字符上 580 cursor.mergeCharFormat (format); 581 //调用QTextEdit的mergeCurrentCharFormat()将格式应用到选区的所有字符上 582 showWidget->text->mergeCurrentCharFormat (format); 583 } 584 585 //设置字号 586 void ImgProcessor::ShowSizeSpinBox (QString spinValue) 587 { 588 QTextCharFormat fmt; 589 //设置字号 590 fmt.setFontPointSize (spinValue.toFloat ()); 591 //直接调用QTextEdit的 592 showWidget->text->mergeCurrentCharFormat (fmt); 593 } 594 595 //设置文字显示加粗 596 void ImgProcessor::ShowBoldBtn () 597 { 598 QTextCharFormat fmt; 599 //设置是否加粗 600 fmt.setFontWeight (boldBtn->isChecked () ? QFont::Bold : QFont::Normal); 601 showWidget->text->mergeCurrentCharFormat (fmt); 602 } 603 604 //设置文字倾斜 605 void ImgProcessor::ShowItalicBtn () 606 { 607 QTextCharFormat fmt; 608 //设置是否倾斜 (true or false) 609 fmt.setFontItalic (italicBtn->isChecked() ); 610 showWidget->text->mergeCurrentCharFormat (fmt); 611 } 612 613 //是否下划线 614 void ImgProcessor::ShowUnderlineBtn () 615 { 616 QTextCharFormat fmt; 617 //设置是否有下划线 618 fmt.setFontUnderline (underlineBtn->isChecked ()); 619 showWidget->text->mergeCurrentCharFormat (fmt); 620 } 621 622 //是否字体颜色 623 void ImgProcessor::ShowColorBtn () 624 { 625 QColor color = QColorDialog::getColor (Qt::red, this); 626 if (color.isValid ()) //查看颜色是否有效 627 { 628 QTextCharFormat fmt; 629 fmt.setForeground (color); //设置前景颜色 630 showWidget->text->mergeCurrentCharFormat (fmt); 631 } 632 } 633 634 //设置字符格式 635 void ImgProcessor::ShowCurrentFormatChanged (const QTextCharFormat &fmt) 636 { 637 //findText()返回(fmt.fontFamily()位置的索引)根据该索引得到ComboBox里面的元素 638 fontComboBox->setCurrentIndex (fontComboBox->findText (fmt.fontFamily ())); 639 sizeComboBox->setCurrentIndex (sizeComboBox->findText (QString::number (fmt.fontPointSize ()))); 640 boldBtn->setChecked(fmt.font ().bold ()); 641 italicBtn->setChecked (fmt.fontItalic ()); 642 underlineBtn->setChecked (fmt.fontUnderline ()); 643 } 644 645 //实现段落对齐 646 void ImgProcessor::ShowAlignment (QAction *act) 647 { 648 if (act == leftAction) 649 showWidget->text->setAlignment (Qt::AlignLeft); 650 if (act == rightAction) 651 showWidget->text->setAlignment (Qt::AlignRight); 652 if (act == centerAction) 653 showWidget->text->setAlignment (Qt::AlignCenter); 654 if (act == justifyAction) 655 showWidget->text->setAlignment (Qt::AlignJustify); 656 } 657 658 //响应文本中发生改变的信号的函数 659 //完成四个按钮的状态更新,通过调用QTextEdit类的alignment()函数获得当前光标所在段落 660 //的对齐方式,设置相应的对齐按钮为按下状态 661 void ImgProcessor::ShowCursorPositionChanged () 662 { 663 if (showWidget->text->alignment () == Qt::AlignLeft) 664 leftAction->setChecked (true); 665 if (showWidget->text->alignment () == Qt::AlignRight) 666 rightAction->setChecked (true); 667 if (showWidget->text->alignment () == Qt::AlignCenter) 668 centerAction->setChecked (true); 669 if (showWidget->text->alignment () == Qt::AlignJustify) 670 justifyAction->setChecked (true); 671 672 } 673 674 //通过获取当前文本段QTextBlockFormat的缩进值来进行相应的计算的方法,以获得排序文本的缩进值 675 void ImgProcessor::ShowList (int index) 676 { 677 //获得编辑框的QTextCursor对象指针 678 QTextCursor cursor = showWidget->text->textCursor (); 679 if (index != 0) 680 { 681 QTextListFormat::Style style = QTextListFormat::ListDisc; //初始化style属性值 682 switch(index) 683 { 684 default: 685 //根据索引值来确定style属性值 686 case 1: 687 style = QTextListFormat::ListDisc; break; 688 case 2: 689 style = QTextListFormat::ListCircle; break; 690 case 3: 691 style = QTextListFormat::ListSquare; break; 692 case 4: 693 style = QTextListFormat::ListDecimal; break; 694 case 5: 695 style = QTextListFormat::ListLowerAlpha; break; 696 case 6: 697 style = QTextListFormat::ListUpperAlpha; break; 698 case 7: 699 style = QTextListFormat::ListLowerRoman; break; 700 case 8: 701 style = QTextListFormat::ListUpperRoman; break; 702 } 703 /* 设置缩进值 */ 704 //设定beginEditBlock()和endEditBlock()间的所有操作相当于一个动作 705 cursor.beginEditBlock (); 706 //通过QTextcursor获得QTextBlockFormat对象,由其获得段落的缩进值. 707 QTextBlockFormat blockFmt = cursor.blockFormat (); 708 QTextListFormat listFmt; 709 710 //下面的if{...}else {...}代码用来改变段落缩进的长度 711 if (cursor.currentList ()) 712 { 713 listFmt = cursor.currentList()->format (); 714 } 715 else 716 { 717 //再通过QTextListFormat定义缩进值 718 listFmt.setIndent (blockFmt.indent() + 1); 719 blockFmt.setIndent (0); 720 cursor.setBlockFormat (blockFmt); 721 } 722 //将设定的格式用到光标所在文本 723 // qDebug() << "progressing..."; 724 listFmt.setStyle (style); 725 cursor.createList (listFmt); 726 cursor.endEditBlock (); 727 } 728 else 729 { 730 QTextBlockFormat bfmt; 731 bfmt.setObjectIndex (-1); 732 cursor.mergeBlockFormat (bfmt); 733 } 734 } 735 736 ImgProcessor::~ImgProcessor() 737 { 738 739 }
1 //showWidget.cpp 2 #include "showwidget.h" 3 #include <QHBoxLayout> 4 5 ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent) 6 { 7 imageLabel = new QLabel; 8 //使图片放置合适位置 9 imageLabel->setScaledContents (true); 10 11 text = new QTextEdit; 12 13 QHBoxLayout *mainLayout = new QHBoxLayout(this); 14 15 mainLayout->addWidget (imageLabel); 16 mainLayout->addWidget (text); 17 }
1 //main.cpp 2 #include "imgprocessor.h" 3 #include <QApplication> 4 #include <QTextCodec> //使能够输出中文 5 6 int main(int argc, char *argv[]) 7 { 8 //此语句一定要加上去,qt5中取消了QTextCodec::setCodecForTr(QTextCodec::codecForName("gbk") 9 //来设置中文字体编码 10 //在读取文件时,才能不会读取中文为乱码 11 QTextCodec::setCodecForLocale (QTextCodec::codecForName ("UTF8")); 12 13 QApplication a(argc, argv); 14 QFont f("ZYSong18030", 12); 15 a.setFont (f); 16 17 ImgProcessor w; 18 w.show(); 19 20 return a.exec(); 21 }
标签:
原文地址:http://www.cnblogs.com/douzujun/p/5702214.html