QT_文本編輯器_源碼下載


源碼下載: 鏈接: http://pan.baidu.com/s/1c21EVRy 密碼: qub8

實現主要的功能有:新建,打開,保存,另存為,查找(查找的時候需要先將光標放到最下面位置才能查全,不知道為什么...找不出來),改變字體,編輯器背景色等....

程序效果截圖如下:

代碼如下:

 
          
//imgprocessor.h
  1 #ifndef IMGPROCESSOR_H
  2 #define IMGPROCESSOR_H
  3 
  4 #include "showwidget.h"
  5 
  6 #include <QMainWindow>
  7 #include <QApplication>
  8 #include <QPushButton>
  9 #include <QRadioButton>
 10 #include <QCheckBox>
 11 #include <QImage>
 12 #include <QLabel>
 13 #include <QMenu>
 14 #include <QMenuBar>
 15 #include <QSaveFile>
 16 #include <QAction>
 17 #include <QActionGroup>
 18 #include <QComboBox>
 19 #include <QSpinBox>
 20 #include <QToolBar>
 21 #include <QToolButton>
 22 #include <QFontComboBox>
 23 #include <QGridLayout>
 24 #include <QHBoxLayout>
 25 #include <QVBoxLayout>
 26 #include <QLineEdit>
 27 #include <QTextCursor>
 28 
 29 
 30 class ImgProcessor : public QMainWindow
 31 {
 32     Q_OBJECT
 33 
 34 public:
 35     ImgProcessor(QWidget *parent = 0);
 36     ~ImgProcessor();
 37     void createActions();                      //創建動作
 38     void createMenus();                        //創建菜單
 39     void createToolBars();                     //創建工具欄
 40     void loadFile(QString  filename);          //加載文件
 41     void mergeFormat(QTextCharFormat);         //合並格式
 42 private:
 43     //各項菜單欄
 44     QMenu *fileMenu;                           //文件菜單
 45     QMenu *zoomMenu;                           //縮放菜單
 46     QMenu *rotateMenu;                         //旋轉
 47     QMenu *mirrorMenu;                         //鏡像
 48     //放置圖片
 49     QImage img;
 50 
 51     QString fileName;                          //打開文件名
 52     QString curFileName;                       //保存文件名
 53     //顯示中心部件
 54     ShowWidget *showWidget;
 55 
 56     //查找對話框
 57 //    findDialog *find_dialog;
 58     QString str;
 59     QLabel *lineLabel;
 60     QLineEdit *lineEdit;
 61     QPushButton *findNextBtn;
 62     QPushButton *cancelBtn;
 63 
 64     QLabel *directLabel;
 65 
 66     QRadioButton *upBtn;
 67     QRadioButton *downBtn;
 68 
 69     QCheckBox *lowOrUpperBox;
 70 
 71     //替換對話框
 72     QString str2;
 73     QString str3;
 74     QLabel *lineLabel21;           //查找
 75     QLineEdit *lineEdit21;          //查找內容
 76     QLabel *lineLabel22;           //替換
 77     QLineEdit *lineEdit22;          //替換內容
 78     QPushButton *findNextBtn2;     //查找下一個
 79     QPushButton *subBtn2;          //替換按鈕
 80     QPushButton *cancelBtn2;
 81 
 82     QLabel *directLabel2;
 83     QCheckBox *lowOrUpperBox2;
 84     bool findjudge;
 85 
 86 
 87     //文件菜單項
 88     QAction *openFileAction;                    //打開文件動作
 89     QAction *NewFileAction;                     //新建文件
 90     QAction *SaveAction;                        //保存文件
 91     QAction *SaveAsAction;                      //另存為文件
 92     QAction *PrintTextAction;                   //打印文本動作
 93     QAction *PrintImageAction;                  //打印圖片
 94     QAction *exitAction;                        //退出動作
 95 
 96     //編輯菜單項
 97     QAction *copyAction;                        //復制動作
 98     QAction *cutAction;                         //剪貼
 99     QAction *pasteAction;                       //粘貼
100     QAction *findAction;                        //查找
101     QAction *subAction;                         //替換
102     QAction *aboutAction;                       //關於
103     QAction *zoomInAction;                      //放大
104     QAction *zoomOutAction;                     //縮小
105 
106     //旋轉菜單欄--圖像處理
107     QAction *rotate90Action;                    //90°
108     QAction *rotate180Action;                   //180°
109     QAction *rotate270Action;                   //270°
110 
111     //鏡像菜單欄
112     QAction *mirrorVerticalAction;              //垂直鏡像
113     QAction *mirrorHorizonAction;               //水平鏡像
114     QAction *undoAction;                        //撤銷
115     QAction *redoAction;                        //取消撤銷
116 
117     //工具欄
118     QToolBar *fileTool;                         //文件-打開、新建、保存,另存為,打印文本(圖像)
119     QToolBar *zoomTool;                         //縮放
120     QToolBar *rotateTool;                       //旋轉
121     QToolBar *mirrorTool;                       //鏡像
122     QToolBar *doToolBar;                        //撤銷、回撤--操作
123 
124     //文本編輯
125     QLabel *fontLabel;                          //字體設置項
126     QFontComboBox *fontComboBox;                //字體選框
127     QLabel *fontLabel2;                         //大小
128     QComboBox *sizeComboBox;                    //大小選框
129     QToolButton *boldBtn;                       //加粗按鈕
130     QToolButton *italicBtn;                     //斜體
131     QToolButton *underlineBtn;                  //下划線
132     QToolButton *colorBtn;                      //字體顏色
133     QToolButton *colorBackBtn;                  //背景顏色按鈕
134     QToolBar *fontToolBar;                      //字體工具欄
135 
136     //排序設置項
137     QLabel *listLabel;
138     QComboBox *listComboBox;
139     QActionGroup *actGrp;
140     QAction *leftAction;                        //居左
141     QAction *rightAction;                       //局右
142     QAction *centerAction;                      //居中
143     QAction *justifyAction;                     //兩端對齊
144     QToolBar *listToolBar;                      //排序工具欄
145 
146 protected slots:
147     void ShowNewFile();                         //新建文件
148     void ShowOpenFile();                        //打開文件
149     void ShowSaveFile();                        //保存文件
150     void ShowSaveAsFile();                      //另存為
151     void ShowFindDialog();                      //查找對話框
152     void ShowFindText();                        //查找
153     void ShowFindStr();
154 
155     void ShowSubDialog();                       //替換對話框
156     void ShowSubstitute();                      //替換
157     void ShowSubText();
158     void ShowSubText2();
159     void ShowFindStr2 ();
160 
161     void ShowPrintText();                       //打印文本
162     void ShowPrintImage();                      //打印圖像
163     void ShowZoomIn();                          //放大功能
164     void ShowZoomOut();                         //縮小功能
165     void ShowRotate90();                        //旋轉90°
166     void ShowRotate180();
167     void ShowRotate270();
168     void ShowMirrorVertical();                  //鏡像--縱向鏡像
169     void ShowMirrorHorizontal();                //橫向鏡像
170     void ShowFontComboBox(QString comboStr);    //顯示字體框
171     void ShowSizeSpinBox(QString spinValue);    //大小自旋盒
172     void ShowBoldBtn();                         //加粗功能
173     void ShowItalicBtn();                       //顯示斜體
174     void ShowUnderlineBtn();                    //下划線
175     void ShowColorBtn();
176     void ShowBackColorBtn();                    //背景顏色
177     //字符格式化
178     void ShowCurrentFormatChanged(const QTextCharFormat &fmt);
179 
180     void ShowList(int);                          //排序
181     void ShowAlignment(QAction *act);            //對齊方式
182     void ShowCursorPositionChanged();            //顯示光標位置
183 
184 };
185 
186 #endif // IMGPROCESSOR_H
 
           
 
          
 
         

 //imgprocessor.cpp

  1 #include "imgprocessor.h"
  2 #include <QFileDialog>
  3 #include <QFile>
  4 #include <QTextStream>
  5 #include <QPrintDialog>
  6 #include <QPrinter>
  7 #include <QPainter>
  8 #include <QColor>
  9 #include <QColorDialog>
 10 #include <QTextList>
 11 #include <QMessageBox>
 12 #include <QDebug>
 13 
 14 ImgProcessor::ImgProcessor(QWidget *parent)
 15     : QMainWindow(parent)
 16 {
 17     setWindowTitle (tr("Easy Word"));         //設置窗體標題
 18     //創建放置圖像QLabel和文本編輯器QTextEdit的QWidget對象showWidget,並將該QWidget對象設置
 19     //為中心部件
 20     showWidget = new ShowWidget(this);
 21     setCentralWidget (showWidget);
 22 
 23 //    find_dialog = new findDialog(this);
 24 
 25     curFileName = tr("");                     //初始化文件名
 26 
 27     //排序
 28     listLabel = new QLabel(tr("排序"));
 29     listComboBox = new QComboBox;
 30     listComboBox->addItem ("Standard");
 31     listComboBox->addItem ("QTextListFormat::ListDisc");    //圓盤
 32     listComboBox->addItem ("QTextListFormat::ListCircle");  //
 33     listComboBox->addItem ("QTextListFormat::ListSquare");  //方形
 34     listComboBox->addItem ("QTextListFormat::ListDecimal"); //十進制
 35     listComboBox->addItem ("QTextListformat::ListLowerAlpha"); //小寫字母
 36     listComboBox->addItem ("QTextListFormat::ListUpperAlpha"); //大寫字母
 37     listComboBox->addItem ("QTextListformat::ListLowerRoman"); //小寫羅馬字符
 38     listComboBox->addItem ("QTextListFormat::ListUpperRoman"); //大寫羅馬字符
 39 
 40     //在工具欄上嵌入控件:字體,大小,粗體,斜體,字體顏色
 41     //字體--這里的字體,字號僅僅是下拉列表框改變
 42     fontLabel = new QLabel(tr("字體:"));
 43     fontComboBox = new QFontComboBox;
 44     //setFontFilter--接口過濾(只在下拉列表框中顯示某一類字體,默認情況下為QFontComboBox::AllFonts
 45     //列出所有字體
 46     fontComboBox->setFontFilters (QFontComboBox::ScalableFonts);
 47 
 48     //字號
 49     fontLabel2 = new QLabel(tr("字號:"));
 50     sizeComboBox = new QComboBox;
 51 
 52     /**QFontDatabase-實現在字號下拉列表框中填充各種不同的字號條目
 53      * 其用於表示當前系統中所有可用的格式信息,主要是字體和字號大小
 54      * provide information about the fonts avaliable in the underlying(潛在) window system*/
 55 
 56     QFontDatabase db;
 57 
 58     //standardSize(): return a list of standard font size(返回可用標准字號的列表).
 59     foreach (int size, db.standardSizes ())
 60         sizeComboBox->addItem (QString::number (size)); //將它們插入到字號下拉框中
 61 
 62     //加粗
 63     boldBtn = new QToolButton;
 64     boldBtn->setIcon (QIcon("bold.png"));
 65     //設置成是否是開關(toggle)按鈕(true)---即可復選的
 66     boldBtn->setCheckable (true);
 67 
 68     //斜體
 69     italicBtn = new QToolButton;
 70     italicBtn->setIcon (QIcon("italic.png"));
 71     italicBtn->setCheckable (true);
 72 
 73     //下划線
 74     underlineBtn = new QToolButton;
 75     underlineBtn->setIcon (QIcon("underline.png"));
 76     underlineBtn->setCheckable (true);
 77 
 78     //顏色
 79     colorBtn = new QToolButton;
 80     colorBtn->setIcon (QIcon("color.png"));
 81     colorBtn->setCheckable (true);
 82 
 83     //背景顏色
 84     colorBackBtn = new QToolButton;
 85     colorBackBtn->setIcon (QIcon("color.png"));
 86     colorBtn->setCheckable (true);
 87 
 88     /* 創建動作、菜單、工具欄函數 */
 89     createActions ();
 90     createMenus ();
 91     createToolBars ();
 92     if (img.load ("image.png"))
 93     {
 94         //在imageLabel對象放置圖像
 95         showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
 96     }
 97 
 98     //連接槽函數
 99     connect (fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString)));
100     connect (sizeComboBox, SIGNAL(activated(QString)), this, SLOT(ShowSizeSpinBox(QString)));
101     connect (boldBtn, SIGNAL(clicked(bool)), this, SLOT(ShowBoldBtn()));
102     connect (italicBtn, SIGNAL(clicked(bool)), this, SLOT(ShowItalicBtn()));
103     connect (underlineBtn, SIGNAL(clicked(bool)), this, SLOT(ShowUnderlineBtn()));
104     connect (colorBtn, SIGNAL(clicked(bool)), this , SLOT(ShowColorBtn()));
105     connect (colorBackBtn, SIGNAL(clicked(bool)),this, SLOT(ShowBackColorBtn()));
106 
107     //設置字符格式--之前的字符的字體,字號,僅僅是下拉列表框的改變, 還需要調用此函數改變文本字體
108     //當光標所在字符格式發生變化時調用此槽函數,函數根據 新的字符格式 將 工具欄 各個格式的顯示更新??
109     connect (showWidget->text, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
110              this, SLOT(ShowCurrentFormatChanged(QTextCharFormat)));
111 
112     connect (listComboBox, SIGNAL(activated(int)), this, SLOT(ShowList(int)));
113     //連接撤銷信號和 rede按鈕顯示狀況相連接
114     connect (showWidget->text->document (), SIGNAL(undoAvailable(bool)),
115              redoAction, SLOT(setEnabled(bool)));
116     connect (showWidget->text->document (), SIGNAL(redoAvailable(bool)),
117              redoAction, SLOT(setEnabled(bool)));
118     //用於排版
119     connect (showWidget->text, SIGNAL(cursorPositionChanged()),
120              this, SLOT(ShowCursorPositionChanged()));
121 }
122 
123 //動作(Action)的實現
124 void ImgProcessor::createActions ()
125 {
126 
127     //基於文件操作的動作(Action)的代碼
128     //“打開”動作
129     //在創建“打開文件”動作的同時,指定了此動作使用的圖標、名稱及父窗口
130     openFileAction = new QAction(QIcon("open.png"), tr("打開"), this);
131     //設定此動作的組合鍵為【Ctrl+O】。
132     openFileAction->setShortcut (tr("Ctrl+O"));
133     //設定了狀態條顯示,當鼠標移至此動作對應的菜單條目或工具欄按鈕上時,在狀態條上顯示"打開xxx"提示
134     openFileAction->setStatusTip (tr("打開一個文件"));
135     connect (openFileAction, SIGNAL(triggered(bool)), this, SLOT(ShowOpenFile()));
136 
137     //“新建”動作
138     NewFileAction = new QAction(QIcon("new.png"), tr("新建"), this);
139     NewFileAction->setShortcut (tr("Ctrl+N"));
140     NewFileAction->setStatusTip (tr("新建一個文件"));
141     connect (NewFileAction, SIGNAL(triggered(bool)), this, SLOT(ShowNewFile()));
142 
143     //“保存”動作
144     SaveAction = new QAction(QIcon("save.png"), tr("保存"), this);
145     SaveAction->setShortcut (tr("Ctrl+S"));
146     SaveAction->setStatusTip (tr("保存一個文件"));
147     connect (SaveAction, SIGNAL(triggered(bool)), this, SLOT(ShowSaveFile()));
148 
149     //“另存為”動作
150     SaveAsAction = new QAction(tr("另存為"), this);
151     SaveAsAction->setShortcut (tr("Ctrl+alt+S"));
152     SaveAsAction->setStatusTip (tr("另存為"));
153     connect (SaveAsAction, SIGNAL(triggered(bool)), this, SLOT(ShowSaveAsFile()));
154 
155     //“退出”動作
156     exitAction = new QAction(tr("退出"), this);
157     exitAction->setShortcut (tr("Ctrl+Q"));
158     exitAction->setStatusTip (tr("退出程序"));
159     connect (exitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
160 
161     //"復制"動作
162     copyAction = new QAction(QIcon("copy.png"), tr("復制"), this);
163     copyAction->setShortcut (tr("Ctrl+C"));
164     copyAction->setStatusTip (tr("復制"));
165     //連接引發信號 和 中心部件的文本編輯框 復制信號
166     connect (copyAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(copy()));
167 
168     //“剪切”信號
169     cutAction = new QAction(QIcon("cut.png"), tr("剪貼"), this);
170     cutAction->setShortcut (tr("Ctrl+X"));
171     cutAction->setStatusTip (tr("剪切文件"));
172     connect (cutAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(cut()));
173 
174     //"粘貼"動作
175     pasteAction = new QAction(QIcon("paste.png"), tr("粘貼"), this);
176     pasteAction->setShortcut (tr("Ctrl+V"));
177     pasteAction->setStatusTip (tr("粘貼文件"));
178     connect (pasteAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(paste()));
189 
190     findAction = new QAction(QIcon("find.png"), tr("查找"), this);
191     findAction->setShortcut (tr("Ctrl+f"));
192     findAction->setStatusTip (tr("查找文件"));
193     connect (findAction, SIGNAL(triggered(bool)), this, SLOT(ShowFindDialog()));
194 
195     subAction = new QAction(QIcon("sub.png"), tr("替換"), this);
196     subAction->setShortcut (tr("Ctrl+H"));
197     subAction->setStatusTip (tr("替換"));
198     connect (subAction, SIGNAL(triggered(bool)), this, SLOT(ShowSubDialog()));
199 
200     //“關於"動作
201     aboutAction = new QAction(tr("關於"), this);
202     connect (aboutAction, SIGNAL(triggered(bool)), this, SLOT(QApplication::aboutQt ()));
203 
204     //實現打印文本和圖像、圖像縮放和鏡像的動作(Action)的代碼
205     //“打印文本”動作
206     PrintTextAction = new QAction(QIcon("printText.png"), tr("打印文本"), this);
207     PrintTextAction->setStatusTip (tr("打印一個文本"));
208     connect (PrintTextAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintText()));
209 
210     //“打印圖像”動作
211     PrintImageAction = new QAction(QIcon("printImage.png"), tr("打印圖像"), this);
212     PrintImageAction->setStatusTip (tr("打印一幅圖像"));
213     connect (PrintImageAction, SIGNAL(triggered(bool)), this, SLOT(ShowPrintImage()));
214 
215     //“放大”動作
216     zoomInAction = new QAction(QIcon("zoomin.png"), tr("放大"), this);
217     zoomInAction->setStatusTip (tr("放大一張圖片"));
218     connect (zoomInAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomIn()));
219 
220     //"縮小"動作
221     zoomOutAction = new QAction(QIcon("zoomout.png"), tr("縮小"), this);
222     zoomOutAction->setStatusTip (tr("縮小一張圖片"));
223     connect (zoomOutAction, SIGNAL(triggered(bool)), this, SLOT(ShowZoomOut()));
224 
225     //實現圖像旋轉動作(Action)
226     //旋轉90°
227     rotate90Action = new QAction(QIcon("rotate90.png"), tr("旋轉 90°"), this);
228     rotate90Action->setStatusTip (tr("將一幅圖旋轉 90°"));
229     connect (rotate90Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate90()));
230 
231     //旋轉180°
232     rotate180Action = new QAction(QIcon("rotate180.png"), tr("旋轉 180°"), this);
233     rotate180Action->setStatusTip (tr("將一幅圖旋轉 180°"));
234     connect (rotate180Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate180()));
235 
236     //旋轉270°
237     rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋轉 270°"), this);
238     rotate270Action->setStatusTip (tr("將一幅圖旋轉 270°"));
239     connect (rotate270Action, SIGNAL(triggered(bool)), this, SLOT(ShowRotate270()));
240 
241     //實現圖像鏡像的動作(Action)
242     //縱向鏡像
243     mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png"), tr("縱向鏡像"), this);
244     mirrorVerticalAction->setStatusTip (tr("對一幅圖做縱鏡像"));
245     connect (mirrorVerticalAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorVertical()));
246 
247     //橫向鏡像
248     mirrorHorizonAction = new QAction(QIcon("mirrorHorizontal.png"), tr("橫向鏡像"), this);
249     mirrorHorizonAction->setStatusTip (tr("對一幅圖做橫鏡像"));
250     connect (mirrorHorizonAction, SIGNAL(triggered(bool)), this, SLOT(ShowMirrorHorizontal()));
251 
252     //實現撤銷和恢復的動作(Action)
253     undoAction = new QAction(QIcon("undo.png"), "撤銷", this);
254     connect (undoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(undo()));
255     redoAction = new QAction(QIcon("redo.png"), "重做", this);
256     connect (redoAction, SIGNAL(triggered(bool)), showWidget->text, SLOT(redo()));
257 
258     //排序: 左對齊、右對齊、居中、兩端對齊
259     //setCheckable()只能是可選擇的,或不能選擇的.選中了,就不能取消選中狀態(不可復選)
260     //setChecked()可以是選中的,或者是非選中的(可復選)
261     actGrp = new QActionGroup(this);
262     leftAction = new QAction(QIcon("left.png"), "左對齊", actGrp);
263     leftAction->setCheckable (true);
264     rightAction = new QAction(QIcon("right.png"), "右對齊", actGrp);
265     rightAction->setCheckable (true);
266     centerAction = new QAction(QIcon("center.png"), "居中", actGrp);
267     centerAction->setCheckable (true);
268     justifyAction = new QAction(QIcon("justify.png"), "兩端對齊", actGrp);
269     justifyAction->setCheckable (true);
270     //按下某個對齊按鈕的響應,根據比較判斷觸發的是哪個對齊按鈕
271     connect (actGrp, SIGNAL(triggered(QAction*)), this, SLOT(ShowAlignment(QAction*)));
272 
273 }
274 
275 //實現了各個動作之后,需要將它們通過菜單,工具欄或快捷鍵的方式體現出來.
276 //菜單的實現
277 void ImgProcessor::createMenus ()
278 {
279     //文件菜單
280     //直接調用QMainWindow的menuBar()函數即可得到主窗口的菜單條指針,再調用菜單條
281     //QMenuBar的addMenu()函數,即可完成在菜單條中插入一個新菜單fileMenu,fileMenu()函數為一個
282     //QMenu類對象
283     fileMenu = menuBar ()->addMenu (tr("文件"));
284     fileMenu->addAction(openFileAction);
285     fileMenu->addAction(NewFileAction);
286     fileMenu->addAction(SaveAction);
287     fileMenu->addAction(SaveAsAction);
288     fileMenu->addAction(PrintTextAction);
289     fileMenu->addAction(PrintImageAction);
290     fileMenu->addSeparator ();             //添加分割
291     fileMenu->addAction(exitAction);
292     //編輯菜單(縮放...)
293     zoomMenu = menuBar ()->addMenu (tr("編輯"));
294     zoomMenu->addAction(copyAction);
295     zoomMenu->addAction(cutAction);
296     zoomMenu->addAction(pasteAction);
297     zoomMenu->addAction(findAction);
298     zoomMenu->addAction(subAction);
299     zoomMenu->addAction(aboutAction);
300     zoomMenu->addSeparator ();
301     zoomMenu->addAction(zoomInAction);
302     zoomMenu->addAction(zoomOutAction);
303     //旋轉菜單
304     rotateMenu = menuBar ()->addMenu (tr("旋轉"));
305     rotateMenu->addAction(rotate90Action);
306     rotateMenu->addAction(rotate180Action);
307     rotateMenu->addAction(rotate270Action);
308     //鏡像菜單
309     mirrorMenu = menuBar ()->addMenu (tr("鏡像"));
310     mirrorMenu->addAction(mirrorVerticalAction);
311     mirrorMenu->addAction(mirrorHorizonAction);
312 }
313 
314 //工具欄的實現(ToolBars)的實現
315 void ImgProcessor::createToolBars ()
316 {
317     //文件工具條
318     //直接調用QMainWindow的addToolBar()函數即可獲得主窗口的工具條對象,每新增一個工具條
319     //調用一次addToolBar()函數,賦予不同的名稱,即可在主窗口中新增一個工具條
320     fileTool = addToolBar ("File");
321     //調用QToolBar的addAction()函數在工具條中插入屬於本工具條的動作.
322     //這些工具條可以在工具欄單擊右鍵,將彈出工具條顯示的選擇菜單
323     fileTool->addAction(openFileAction);
324     fileTool->addAction(NewFileAction);
325     fileTool->addAction(SaveAction);
326     fileTool->addAction(PrintTextAction);
327     fileTool->addAction(PrintImageAction);
328     //編輯工具條
329     zoomTool = addToolBar ("Edit");
330     zoomTool->addAction(copyAction);
331     zoomTool->addAction(cutAction);
332     zoomTool->addAction(pasteAction);
333     zoomTool->addSeparator ();
334     zoomTool->addAction(zoomInAction);
335     zoomTool->addAction(zoomOutAction);
336     //旋轉工具條
337     rotateTool = addToolBar ("rotate");
338     rotateTool->addAction(rotate90Action);
339     rotateTool->addAction(rotate180Action);
340     rotateTool->addAction(rotate270Action);
341     //撤銷和重做工具條
342     doToolBar = addToolBar ("doEdit");
343     doToolBar->addAction(undoAction);
344     doToolBar->addAction(redoAction);
345     /***
346      * 工具條是可移動窗口,停靠的區域由QToolBar的allowAreas決定
347      * Qt::LeftToolBarArea、Qt::RightToolBarArea、Qt::TopToolBarArea、
348      * Qt::BottomToolBarArea, Qt::AllToolBarAreas(默認)。
349      * 可以通過調用setAllowAreas()函數來指定工具條可停靠的區域
350      * 如:fileTool->setAllowAreas(Qt::TopToolBarArea|Qt::LeftToolBarArea);
351      * 來限定fileTool只能出現在主窗口的頂部或者左側.工具條也可通過setMovable()設定可移動性:
352      * 如fileTool->setMovable (false)來指定文件工具條不可移動 */
353 
354     //字體工具條
355     fontToolBar = addToolBar ("Font");
356     fontToolBar->addWidget (fontLabel);
357     fontToolBar->addWidget (fontComboBox);
358     fontToolBar->addWidget (fontLabel2);
359     fontToolBar->addWidget (sizeComboBox);
360     fontToolBar->addSeparator ();
361     fontToolBar->addWidget (boldBtn);
362     fontToolBar->addWidget (italicBtn);
363     fontToolBar->addWidget (underlineBtn);
364     fontToolBar->addSeparator ();
365     fontToolBar->addWidget (colorBtn);
366     fontToolBar->addSeparator ();
367     fontToolBar->addWidget (colorBackBtn);
368 
369     //排序工具條
370     listToolBar = addToolBar ("list");
371     listToolBar->addWidget (listLabel);
372     listToolBar->addWidget (listComboBox);
373     listToolBar->addSeparator ();
374     listToolBar->addActions(actGrp->actions ());
375 }
376 
377 //文件操作功能
378 //新建文件
379 void ImgProcessor::ShowNewFile ()
380 {
381     ImgProcessor *newImgProcessor = new ImgProcessor;
382     newImgProcessor->show ();
383 }
384 
385 //打開文件
386 void ImgProcessor::ShowOpenFile ()
387 {
388     fileName = QFileDialog::getOpenFileName(this, "打開");
389     curFileName = fileName;
390     if (!fileName.isEmpty ())       //文件名不為空
391     {
392         //原窗口內容為空,則直接加載該文件
393         if (showWidget->text->document ()->isEmpty ())
394         {
395             loadFile (fileName);    //loadFile()函數實現在下方.
396         }
397         //否則,打開新窗口加載文件
398         else
399         {
400             ImgProcessor *newImgProcessor = new ImgProcessor;
401             newImgProcessor->show ();
402             newImgProcessor->loadFile (fileName);
403         }
404     }
405 }
406 
407 //loadFile()函數實現,利用QFile和QTextStream完成具體讀取文件內容工作
408 void ImgProcessor::loadFile (QString filename)
409 {
410     printf ("file name:%s\n", (char*)filename.data ());
411     QFile file(filename);     //獲取文件
412     if (file.open (QIODevice::ReadOnly | QIODevice::Text))
413     {
414         //提供基於文本流的功能(對於ASCII文本)
415         QTextStream textStream(&file);
416         while (!textStream.atEnd ())      //判斷是否到文件流或文件終點
417         {
418             //讀取流中的一行,並返回包含文本的返回的字符串
419             //(不包含任何換行符合回車符-不同於QIODevice::readline()--不會省略最后換行符).
420             showWidget->text->append (textStream.readLine ());
421             printf("read line\n");
422         }
423         printf("end\n");
424     }
425 }
426 
427 //保存文件
428 void ImgProcessor::ShowSaveFile ()
429 {
430     if (curFileName.isEmpty ())    //原路徑文件名
431     {
432         QString filename = QFileDialog::getSaveFileName (this, tr("保存文件"),
433                                                          QString(), tr("文本文件(*.txt);;c++文件(*h *cpp *hpp"));
434         if (!filename.isEmpty ()) {
435             QFile file(filename);
436             if (!file.open (QIODevice::WriteOnly)) {
437                 QMessageBox::critical (this, tr("錯誤"), tr("不能打開文件"));
438                 return;
439             }
440             curFileName = filename;      //下次保存,默認同一條路徑
441             QTextStream outs(&file);
442             outs << showWidget->text->toPlainText ();
443             outs.flush ();
444             file.close ();
445         }
446     }
447     else {
448         QFile file(curFileName);
449         if (!file.open (QIODevice::WriteOnly)) {
450             QMessageBox::critical (this, tr("錯誤"), tr("不能打開文件"));
451             return;
452         }
453         QTextStream outs(&file);
454         outs << showWidget->text->toPlainText ();
455         outs.flush ();
456         file.close ();
457     }
458 
459 }
460 
461 //另存為
462 void ImgProcessor::ShowSaveAsFile ()
463 {
464     QString filename = QFileDialog::getSaveFileName (this, tr("保存文件"),
465                                                      QString(), tr("文本文件(*.txt);;c++文件(*.h *.cpp *.hpp)"));
466     if (!filename.isEmpty ()) {
467         QFile file(filename);
468         if (!file.open (QIODevice::WriteOnly)) {
469             QMessageBox::critical (this, tr("錯誤"), tr("不能打開文件"));
470             return;
471         }
472         QTextStream outs(&file);
473         //將text->toPlainText()返回文本編輯的純文本,outs<<將純文本寫入流
474         outs << showWidget->text->toPlainText ();
475         outs.flush ();
476         file.close ();
477     }
478 }
479 
480 //顯示查找對話框
481 void ImgProcessor::ShowFindDialog ()
482 {
483     QDialog *FindDialog = new QDialog(this);
484     QGridLayout *mainLayout = new QGridLayout(FindDialog);
485 
486     lineLabel = new QLabel(tr("查找內容(N): "));
487     lineEdit = new QLineEdit;
488 
489     QHBoxLayout *inputLayout = new QHBoxLayout;
490     inputLayout->addWidget (lineLabel);
491     inputLayout->addWidget (lineEdit);
492 
493     findNextBtn = new QPushButton(tr("查找下一個"));
494     cancelBtn = new QPushButton(tr("取消"));
495     connect (cancelBtn, SIGNAL(clicked(bool)), FindDialog, SLOT(reject()));
496 
497     QVBoxLayout *buttonLayout = new QVBoxLayout;
498     buttonLayout->addWidget (findNextBtn);
499     buttonLayout->addWidget (cancelBtn);
500 
501     directLabel = new QLabel(tr("方向"));
502     upBtn = new QRadioButton(tr("向上"));
503 
504 
505     upBtn->setChecked (true);
506 //    findNextBtn->setEnabled (false);
507 
508     downBtn = new QRadioButton(tr("向下"));
509 
510     lowOrUpperBox = new QCheckBox(tr("區分大小寫(C)"));
511 
512     mainLayout->addLayout (inputLayout, 0, 0, 1, 3);
513     mainLayout->addLayout (buttonLayout, 0, 4, 2, 1);
514     mainLayout->addWidget (lowOrUpperBox, 2, 0);
515     mainLayout->addWidget (directLabel, 1, 1);
516     mainLayout->addWidget (upBtn, 2, 1);
517     mainLayout->addWidget (downBtn, 2, 2);
518 
519     FindDialog->show ();
520     str = "";
521     connect (lineEdit, SIGNAL(textChanged(QString)), this, SLOT(ShowFindText()));
522     connect (findNextBtn, SIGNAL(clicked(bool)), this, SLOT(ShowFindStr()));
523 }
524 
525 void ImgProcessor::ShowFindText ()
526 {
527     str = lineEdit->text ();
528 }
529 
530 void ImgProcessor::ShowFindStr ()
531 {
532 //    qDebug() << str;
533     bool judge;
534     if (!str.isEmpty ()) {
535         if (upBtn->isChecked () == Qt::Checked) {
536             if (lowOrUpperBox->isChecked () == Qt::Checked)
537                 judge = showWidget->text->find (str,QTextDocument::FindCaseSensitively);
538             else {
539                 judge = showWidget->text->find (str);
540             }
541         }
542         else {
543             if (lowOrUpperBox->isChecked () == Qt::Checked) {
544                 judge = showWidget->text->find (str, QTextDocument::FindCaseSensitively);
545             }
546             else {
547                 judge = showWidget->text->find (str, QTextDocument::FindBackward);
548             }
549         }
550     }
551     else {
552         return;
553     }
554     if (!judge) {
555         QMessageBox::information (this, tr("結果"), tr("沒找到查找內容"),
556                                   QMessageBox::Yes);
557     }
558 }
559 
560 //替換對話框
561 void ImgProcessor::ShowSubDialog ()
562 {
563     QDialog *SubDialog = new QDialog(this);
564     QGridLayout *mainLayout = new QGridLayout(SubDialog);
565 
566     lineLabel21 = new QLabel(tr("查找內容(N): "));
567     lineEdit21 = new QLineEdit;
568 
569     lineLabel22 = new QLabel(tr("替換(P): "));
570     lineEdit22 = new QLineEdit;
571 
572     QHBoxLayout *inputLayout = new QHBoxLayout;
573     inputLayout->addWidget (lineLabel21);
574     inputLayout->addWidget (lineEdit21);
575 
576     QHBoxLayout *inputLayout2 = new QHBoxLayout;
577     inputLayout2->addWidget (lineLabel22);
578     inputLayout2->addWidget (lineEdit22);
579 
580     findNextBtn2 = new QPushButton(tr("查找下一個"));
581     subBtn2 = new QPushButton(tr("替換"));
582     cancelBtn2 = new QPushButton(tr("取消"));
583     connect (cancelBtn2, SIGNAL(clicked(bool)), SubDialog, SLOT(reject()));
584 
585     QVBoxLayout *buttonLayout = new QVBoxLayout;
586     buttonLayout->addWidget (findNextBtn2);
587     buttonLayout->addWidget (subBtn2);
588     buttonLayout->addWidget (cancelBtn2);
589 
590     lowOrUpperBox = new QCheckBox(tr("區分大小寫(C)"));
591 
592     mainLayout->addLayout (inputLayout, 0, 0, 1, 3);
593     mainLayout->addLayout (inputLayout2, 1, 0, 1, 3);
594     mainLayout->addLayout (buttonLayout, 0, 4, 3, 1);
595     mainLayout->addWidget (lowOrUpperBox, 2, 0);
596 
597     SubDialog->show ();
598     str2 = "";
599     str3 = "";
600     findjudge = false;
601     connect (lineEdit21, SIGNAL(textChanged(QString)), this, SLOT(ShowSubText()));
602     connect (lineEdit22, SIGNAL(textChanged(QString)), this, SLOT(ShowSubText2()));
603     connect (findNextBtn2, SIGNAL(clicked(bool)), this, SLOT(ShowFindStr2()));
604     connect (subBtn2, SIGNAL(clicked(bool)), this, SLOT(ShowSubstitute()));
605 }
606 
607 void ImgProcessor::ShowSubText ()
608 {
609     str2 = lineEdit21->text ();
610 }
611 
612 void ImgProcessor::ShowSubText2 ()
613 {
614     str3 = lineEdit22->text ();
615 }
616 
617 void ImgProcessor::ShowFindStr2 ()
618 {
619     bool judge2;
620     if (!str2.isEmpty ()) {
621         if (lowOrUpperBox->isChecked () == Qt::Checked)
622             judge2 = showWidget->text->find (str2,QTextDocument::FindCaseSensitively);
623         else {
624             judge2 = showWidget->text->find (str2, QTextDocument::FindBackward);
625         }
626     }
627     else {
628         return;
629     }
630     if (!judge2) {
631         QMessageBox::information (this, tr("結果"), tr("沒找到查找內容"),
632                                   QMessageBox::Yes);
633     }
634 }
635 
636 
637 void ImgProcessor::ShowSubstitute ()
638 {
639     QTextCursor textCur;
640     int judge2;
641     if (!str2.isEmpty ()) {
642         if (lowOrUpperBox->isChecked () == Qt::Checked) {
643             judge2 = showWidget->text->find (str2,QTextDocument::FindCaseSensitively);
644             textCur = showWidget->text->textCursor ();
645         }
646         else {
647             judge2 = showWidget->text->find (str2, QTextDocument::FindBackward);
648             textCur = showWidget->text->textCursor ();
649         }
650     }
651     else {
652         return;
653     }
654     if (!judge2) {
655         QMessageBox::information (this, tr("結果"), tr("沒找到查找內容"),
656                                   QMessageBox::Yes);
657     }
658     else {
659          textCur.insertText (str3);
660     }
661 
662 }
663 
664 //打印文本
665 void ImgProcessor::ShowPrintText ()
666 {
667     QPrinter printer;
668     //創建一個QPrintDialog對象,參數為QPrinter對象
669     QPrintDialog printDialog(&printer, this);
670     //判斷打印對話框顯示后用戶是否單擊“打印”,打印--則相關打印屬性將可以通過創建QPrintDialog
671     //對象時,使用的QPrinter對象獲得;單擊取消,則不執行后續的打印操作
672     if (printDialog.exec ())
673     {
674         //獲得QTextEdit對象的文檔
675         QTextDocument *doc = showWidget->text->document ();
676         //打印
677         doc->print (&printer);
678     }
679 
680 }
681 
682 //打印圖像
683 void ImgProcessor::ShowPrintImage ()
684 {
685     QPrinter printer;
686     QPrintDialog printDialog(&printer, this);
687     if (printDialog.exec ())
688     {
689         //創建一個QPainter對象,並指定繪圖設備為一個QPrinter對象
690         QPainter painter(&printer);
691         //獲得QPainter對象的視圖矩形區域
692         QRect rect = painter.viewport ();
693 
694         QSize size = img.size ();                        //獲得圖像大小
695         /* 按照圖形的比例大小重新設置視圖矩形區域 */
696         //Qt::KeepAspectRatio:  按照圖形比例大小,保持寬高比例,盡可能大的在矩形內
697         size.scale (rect.size (), Qt::KeepAspectRatio);  //圖像大小改變
698         //重新得到painter對象的視圖矩形區域
699         //設置QPainter的視圖矩形區域at(x,y) with the given width and height
700         painter.setViewport (rect.x (), rect.y (), size.width (), size.height ());
701         painter.setWindow (img.rect ());      //設置QPainter窗口大小為圖像的大小
702         painter.drawImage (0, 0, img);        //打印圖像at(0,0)
703     }
704 }
705 
706 //放大
707 void ImgProcessor::ShowZoomIn ()
708 {
709     if (img.isNull ())     //有效性判斷
710         return;
711     QMatrix matrix;        //聲明一個QMatrix類的實例
712     //按照2倍比例對水平和垂直方向進行放大,並將當前顯示的圖形按照該坐標矩陣進行轉換
713     matrix.scale (2, 2);
714     img = img.transformed (matrix);
715     //重新設置顯示圖形
716     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
717 }
718 
719 //縮小
720 void ImgProcessor::ShowZoomOut ()
721 {
722     if (img.isNull ())
723         return;
724     QMatrix matrix;
725     matrix.scale (0.5, 0.5);
726     img = img.transformed (matrix);
727     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
728 
729 }
730 
731 //旋轉90°
732 void ImgProcessor::ShowRotate90 ()
733 {
734     if (img.isNull ())
735         return;
736     QMatrix matrix;
737     matrix.rotate (90);
738     img = img.transformed (matrix);
739     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
740 }
741 
742 void ImgProcessor::ShowRotate180 ()
743 {
744     if (img.isNull ())
745         return;
746     QMatrix matrix;
747     matrix.rotate (180);
748     img = img.transformed (matrix);
749     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
750 }
751 
752 void ImgProcessor::ShowRotate270 ()
753 {
754     if (img.isNull ())
755         return;
756     QMatrix matrix;
757     matrix.rotate (270);
758     img = img.transformed (matrix);
759     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
760 }
761 
762 //鏡像--垂直
763 void ImgProcessor::ShowMirrorVertical ()
764 {
765     if (img.isNull ())
766         return;
767     //Qimage Qimage::mirrored(bool horizontal = false, bool vertical = true);
768     //垂直鏡像
769     img = img.mirrored (false, true);
770     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
771 }
772 
773 //水平鏡像
774 void ImgProcessor::ShowMirrorHorizontal ()
775 {
776     if (img.isNull ())
777         return;
778     //Qimage Qimage::mirrored(bool horizontal = true, bool vertical = false);
779     //水平鏡像
780     img = img.mirrored (true, false);
781     showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
782 }
783 
784 //設置字體格式
785 //字體
786 void ImgProcessor::ShowFontComboBox (QString comboStr)
787 {
788     QTextCharFormat fmt;            //創建一個QTextCharFormat對象
789     fmt.setFontFamily (comboStr);   //選擇的字體名稱設置給QTextCharFormat對象
790     mergeFormat (fmt);              //將新格式應用到光標選區內的字符
791 }
792 
793 //對於所有的QTextDocumet進行的修改都通過QTextCursor類來完成
794 void ImgProcessor::mergeFormat (QTextCharFormat format)
795 {
796     QTextCursor cursor = showWidget->text->textCursor ();   //獲得編輯框中的光標
797     //若光標沒有高亮區,則光標所在處的詞為選區(由前后有空格,“,”,“、”,“."等標點分隔
798     if (!cursor.hasSelection ())
799         cursor.select (QTextCursor::WordUnderCursor);
800     //將format所表示的格式應用到光標所在處的字符上
801     cursor.mergeCharFormat (format);
802     //調用QTextEdit的mergeCurrentCharFormat()將格式應用到選區的所有字符上
803     showWidget->text->mergeCurrentCharFormat (format);
804 }
805 
806 //設置字號
807 void ImgProcessor::ShowSizeSpinBox (QString spinValue)
808 {
809     QTextCharFormat fmt;
810     //設置字號
811     fmt.setFontPointSize (spinValue.toFloat ());
812     //直接調用QTextEdit的
813     showWidget->text->mergeCurrentCharFormat (fmt);
814 }
815 
816 //設置文字顯示加粗
817 void ImgProcessor::ShowBoldBtn ()
818 {
819     QTextCharFormat fmt;
820     //設置是否加粗
821     fmt.setFontWeight (boldBtn->isChecked () ? QFont::Bold : QFont::Normal);
822     showWidget->text->mergeCurrentCharFormat (fmt);
823 }
824 
825 //設置文字傾斜
826 void ImgProcessor::ShowItalicBtn ()
827 {
828     QTextCharFormat fmt;
829     //設置是否傾斜 (true or false)
830     fmt.setFontItalic (italicBtn->isChecked() );
831     showWidget->text->mergeCurrentCharFormat (fmt);
832 }
833 
834 //是否下划線
835 void ImgProcessor::ShowUnderlineBtn ()
836 {
837     QTextCharFormat fmt;
838     //設置是否有下划線
839     fmt.setFontUnderline (underlineBtn->isChecked ());
840     showWidget->text->mergeCurrentCharFormat (fmt);
841 }
842 
843 //是否字體顏色
844 void ImgProcessor::ShowColorBtn ()
845 {
846     QColor color = QColorDialog::getColor (Qt::red, this);
847     if (color.isValid ())              //查看顏色是否有效
848     {
849         QTextCharFormat fmt;
850         fmt.setForeground (color);     //設置前景顏色
851         showWidget->text->mergeCurrentCharFormat (fmt);
852     }
853 }
854 
855 //是否背景顏色
856 void ImgProcessor::ShowBackColorBtn()
857 {
858     QColor color = QColorDialog::getColor (Qt::white, this);
859     if (color.isValid ())
860     {
861         QPalette p = showWidget->palette ();
862         p.setColor (QPalette::Base, color);
863         showWidget->setPalette (p);
864     }
865 }
866 
867 //設置字符格式
868 void ImgProcessor::ShowCurrentFormatChanged (const QTextCharFormat &fmt)
869 {
870     //findText()返回(fmt.fontFamily()位置的索引)根據該索引得到ComboBox里面的元素
871     fontComboBox->setCurrentIndex (fontComboBox->findText (fmt.fontFamily ()));
872     sizeComboBox->setCurrentIndex (sizeComboBox->findText (QString::number (fmt.fontPointSize ())));
873     boldBtn->setChecked(fmt.font ().bold ());
874     italicBtn->setChecked (fmt.fontItalic ());
875     underlineBtn->setChecked (fmt.fontUnderline ());
876 }
877 
878 //實現段落對齊
879 void ImgProcessor::ShowAlignment (QAction *act)
880 {
881     if (act == leftAction)
882         showWidget->text->setAlignment (Qt::AlignLeft);
883     if (act == rightAction)
884         showWidget->text->setAlignment (Qt::AlignRight);
885     if (act == centerAction)
886         showWidget->text->setAlignment (Qt::AlignCenter);
887     if (act == justifyAction)
888         showWidget->text->setAlignment (Qt::AlignJustify);
889 }
890 
891 //響應文本中發生改變的信號的函數
892 //完成四個按鈕的狀態更新,通過調用QTextEdit類的alignment()函數獲得當前光標所在段落
893 //的對齊方式,設置相應的對齊按鈕為按下狀態
894 void ImgProcessor::ShowCursorPositionChanged ()
895 {
896     if (showWidget->text->alignment () == Qt::AlignLeft)
897         leftAction->setChecked (true);
898     if (showWidget->text->alignment () == Qt::AlignRight)
899         rightAction->setChecked (true);
900     if (showWidget->text->alignment () == Qt::AlignCenter)
901         centerAction->setChecked (true);
902     if (showWidget->text->alignment () == Qt::AlignJustify)
903         justifyAction->setChecked (true);
904 
905 }
906 
907 //通過獲取當前文本段QTextBlockFormat的縮進值來進行相應的計算的方法,以獲得排序文本的縮進值
908 void ImgProcessor::ShowList (int index)
909 {
910     //獲得編輯框的QTextCursor對象指針
911     QTextCursor cursor = showWidget->text->textCursor ();
912     if (index != 0)
913     {
914         QTextListFormat::Style style = QTextListFormat::ListDisc;  //初始化style屬性值
915         switch(index)
916         {
917         default:
918         //根據索引值來確定style屬性值
919         case 1:
920             style = QTextListFormat::ListDisc;       break;
921         case 2:
922             style = QTextListFormat::ListCircle;     break;
923         case 3:
924             style = QTextListFormat::ListSquare;     break;
925         case 4:
926             style = QTextListFormat::ListDecimal;    break;
927         case 5:
928             style = QTextListFormat::ListLowerAlpha; break;
929         case 6:
930             style = QTextListFormat::ListUpperAlpha; break;
931         case 7:
932             style = QTextListFormat::ListLowerRoman; break;
933         case 8:
934             style = QTextListFormat::ListUpperRoman; break;
935         }
936         /* 設置縮進值 */
937         //設定beginEditBlock()和endEditBlock()間的所有操作相當於一個動作
938         cursor.beginEditBlock ();
939         //通過QTextcursor獲得QTextBlockFormat對象,由其獲得段落的縮進值.
940         QTextBlockFormat blockFmt = cursor.blockFormat ();
941         QTextListFormat  listFmt;
942 
943         //下面的if{...}else {...}代碼用來改變段落縮進的長度
944         if (cursor.currentList ())
945         {
946             listFmt = cursor.currentList()->format ();
947         }
948         else
949         {
950             //再通過QTextListFormat定義縮進值
951             listFmt.setIndent (blockFmt.indent() + 1);
952             blockFmt.setIndent (0);
953             cursor.setBlockFormat (blockFmt);
954         }
955         //將設定的格式用到光標所在文本
956 //        qDebug() << "progressing...";
957         listFmt.setStyle (style);
958         cursor.createList (listFmt);
959         cursor.endEditBlock ();
960     }
961     else
962     {
963         QTextBlockFormat bfmt;
964         bfmt.setObjectIndex (-1);
965         cursor.mergeBlockFormat (bfmt);
966     }
967 }
968 
969 ImgProcessor::~ImgProcessor()
970 {
971 
972 }

 

 

 1 //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     QImage *img;
15     QLabel *imageLabel;
16     QTextEdit *text;
17 
18 signals:
19 
20 public slots:
21 };
22 
23 #endif // SHOWWIDGET_H
 1 //showWidget.cpp
 1 #include "showwidget.h"
 2 #include <QHBoxLayout>
 3 
 4 ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)
 5 {
 6     imageLabel = new QLabel;
 7     //使圖片放置合適位置
 8     imageLabel->setScaledContents (true);
 9 
10     text = new QTextEdit;
11 
12     QHBoxLayout *mainLayout = new QHBoxLayout(this);
13 
14     mainLayout->addWidget (imageLabel);
15     mainLayout->addWidget (text);
16 }

 

 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 }
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM