運行效果

- 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 }
