QT_文本编辑器_源码下载
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 }