下面的程序出自《Qt5开发及实例》陆文周。

效果图:

程序是一个文本编辑器的实例,主界面如下:

程序中的源文件和头文件:

源代码:

文件main.cpp的源代码:

[cpp] view plaincopy print?
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. QFont font("楷体",15);
  7. a.setFont(font);
  8. MainWindow w;
  9. w.show();
  10. return a.exec();
  11. }

mainwindow.h源代码:

[cpp] view plaincopy print?
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QImage>
  5. #include <QLabel>
  6. #include <QMenu>
  7. #include <QMenuBar>
  8. #include <QAction>
  9. #include <QComboBox>
  10. #include <QSpinBox>
  11. #include <QToolBox>
  12. #include <QFontComboBox>
  13. #include <QToolButton>
  14. #include <QTextCharFormat>
  15. #include <QActionGroup>
  16. #include <QAction>
  17. #include "showwidget.h"
  18. class MainWindow : public QMainWindow
  19. {
  20. Q_OBJECT
  21. public:
  22. MainWindow(QWidget *parent = 0);
  23. ~MainWindow();
  24. void createActions();   //创建动作
  25. void createMenus(); //创建菜单
  26. void createToolBars();  //创建工具栏
  27. void loadFile(QString filename);
  28. void mergeFormat(QTextCharFormat format);
  29. private:
  30. QMenu *fileMenu;    //各项菜单栏
  31. QMenu *zoomMenu;
  32. QMenu *rotateMenu;
  33. QMenu *mirrorMenu;
  34. QImage img;
  35. QString fileName;
  36. ShowWidget *showWidget;
  37. QAction *openFileAction;    //文件菜单项
  38. QAction *newFileAction;
  39. QAction *printTextAction;
  40. QAction *printImageAction;
  41. QAction *exitAction;
  42. QAction *copyAction;    //编辑菜单项
  43. QAction *cutAction;
  44. QAction *pasteAction;
  45. QAction *aboutAction;
  46. QAction *zoomInAction;
  47. QAction *zoomOutAction;
  48. QAction *rotate90Action;    //旋转菜单项
  49. QAction *rotate180Action;
  50. QAction *rotate270Action;
  51. QAction *mirrorVerticalAction;  //镜像菜单项
  52. QAction *mirrorHorizontalAction;
  53. QAction *undoAction;    //撤销和返回
  54. QAction *redoAction;
  55. QToolBar *fileTool; //工具栏
  56. QToolBar *zoomTool;
  57. QToolBar *rotateTool;
  58. QToolBar *mirrorTool;
  59. QToolBar *doToolBar;    //工具栏
  60. QLabel *fontLabel1; //字体设置项
  61. QFontComboBox *fontComboBox;
  62. QLabel *fontLabel2;
  63. QComboBox *sizeComboBox;
  64. QToolButton *boldBtn;
  65. QToolButton *italicBtn;
  66. QToolButton *underlineBtn;
  67. QToolButton *colorBtn;
  68. QToolBar *fontToolBar;  //字体工具栏
  69. QLabel *listLabel;  //文字排版功能
  70. QComboBox *listComboBox;
  71. QActionGroup *actGrp;
  72. QAction *leftAction;
  73. QAction *rightAction;
  74. QAction *centerAction;
  75. QAction *justifyAction;
  76. QToolBar *listToolBar;
  77. protected slots:
  78. void showNewFile(); //新建文件槽函数
  79. void showOpenFile();    //打开文件槽函数
  80. void showPrintText();   //打印文本槽函数
  81. void showPrintImage();  //打印图片槽函数
  82. void showZoomIn();  //放大功能槽函数
  83. void showZoomOut(); //缩小功能槽函数
  84. void showRotate90();    //旋转图片槽函数
  85. void showRotate180();
  86. void showRotate270();
  87. void showMirrorVertical();  //镜像功能
  88. void showMirrorHorizontal();
  89. void showFontComboBox(QString comboStr);    //字体设置相关的槽函数
  90. void showSizeSpinBix(QString spinValue);
  91. void showBoldBtn();
  92. void showItalicBtn();
  93. void showUnderlineBtn();
  94. void showColorBtn();
  95. void showCurrentFormatChanged(const QTextCharFormat &fmt);
  96. void showList(int);
  97. void showAlignment(QAction *act);
  98. void showCursorPositionChanged();
  99. };
  100. #endif // MAINWINDOW_H

mainwindow.cpp源代码:

[cpp] view plaincopy print?
  1. #include "mainwindow.h"
  2. #include "showwidget.h"
  3. #include <QApplication>
  4. #include <QToolBar>
  5. #include <QFileDialog>
  6. #include <QTextStream>
  7. #include <QtPrintSupport/QPrinter>
  8. #include <QtPrintSupport/QPrintDialog>
  9. #include <QPainter>
  10. #include <QRect>
  11. #include <QSize>
  12. #include <QGraphicsItem>
  13. #include <QColorDialog>
  14. #include <QColor>
  15. #include <QAction>
  16. #include <QTextList>
  17. #include <QTextBlockFormat>
  18. MainWindow::MainWindow(QWidget *parent)
  19. : QMainWindow(parent)
  20. {
  21. setWindowTitle(tr("Easy Word"));
  22. showWidget = new ShowWidget(this);
  23. setCentralWidget(showWidget);
  24. fontLabel1 = new QLabel(tr("字体: "));
  25. fontComboBox = new QFontComboBox;
  26. fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
  27. fontLabel2 = new QLabel(tr("字号: "));
  28. sizeComboBox = new QComboBox;
  29. QFontDatabase db;
  30. foreach (int size ,db.standardSizes())
  31. {
  32. sizeComboBox->addItem(QString::number(size));
  33. }
  34. boldBtn = new QToolButton;
  35. boldBtn->setIcon(QIcon("bold.png"));
  36. boldBtn->setCheckable(true);
  37. italicBtn = new QToolButton;
  38. italicBtn->setIcon(QIcon("italic.png"));
  39. italicBtn->setCheckable(true);
  40. underlineBtn = new QToolButton;
  41. underlineBtn->setIcon(QIcon("underline.png"));
  42. underlineBtn->setCheckable(true);
  43. colorBtn = new QToolButton;
  44. colorBtn->setIcon(QIcon("color.png"));
  45. colorBtn->setCheckable(true);
  46. //文字排版功能
  47. listLabel = new QLabel(tr("排序"));
  48. listComboBox = new QComboBox;
  49. listComboBox->addItem("Standard");
  50. listComboBox->addItem("QTextListFormat::ListDisc");
  51. listComboBox->addItem("QTextListFormat::ListCircle");
  52. listComboBox->addItem("QTextListFormat::ListSquare");
  53. listComboBox->addItem("QTextListFormat::ListDecimal");
  54. listComboBox->addItem("QTextListFormat::ListLowerAlpha");
  55. listComboBox->addItem("QTextListFormat::ListUpperAlpha");
  56. listComboBox->addItem("QTextListFormat::ListLowerRoman");
  57. listComboBox->addItem("QTextListFormat::ListUpperRoman");
  58. createActions();
  59. createMenus();
  60. createToolBars();
  61. if(img.load("image.png"))
  62. {
  63. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  64. }
  65. //各字体设置相关功能的信号与槽
  66. connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(showFontComboBox(QString)));
  67. connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(showSizeSpinBix(QString)));
  68. connect(boldBtn,SIGNAL(clicked(bool)),this,SLOT(showBoldBtn()));
  69. connect(italicBtn,SIGNAL(clicked(bool)),this,SLOT(showItalicBtn()));
  70. connect(underlineBtn,SIGNAL(clicked(bool)),this,SLOT(showUnderlineBtn()));
  71. connect(colorBtn,SIGNAL(clicked(bool)),this,SLOT(showColorBtn()));
  72. connect(showWidget->text,SIGNAL(currentCharFormatChanged(QTextCharFormat &)),this,SLOT(showCurrentFormatChanged(QTextCharFormat &)));
  73. //    //文字排版功能的信号与槽
  74. //    connect(listComboBox,SIGNAL(activated(int)),this,SLOT(showList(int)));
  75. //    connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
  76. //    connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
  77. //    connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(showCursorPositionChanged()));
  78. connect(listComboBox,SIGNAL(activated(int)),this,SLOT(showList(int)));
  79. connect(showWidget->text->document(),SIGNAL(undoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
  80. connect(showWidget->text->document(),SIGNAL(redoAvailable(bool)),redoAction,SLOT(setEnabled(bool)));
  81. connect(showWidget->text,SIGNAL(cursorPositionChanged()),this,SLOT(showCursorPositionChanged()));
  82. }
  83. void MainWindow::createActions()
  84. {
  85. //“打开”动作
  86. openFileAction = new QAction(QIcon("open.png") , tr("打开") , this);
  87. openFileAction->setShortcut(tr("Ctrl + O"));
  88. openFileAction->setStatusTip(tr("打开一个文件"));
  89. connect(openFileAction,SIGNAL(triggered(bool)),this,SLOT(showOpenFile()));
  90. //“新建”动作
  91. newFileAction = new QAction(QIcon("new.png") , tr("新建") ,this );
  92. newFileAction->setShortcut(tr("Ctrl + N"));
  93. newFileAction->setStatusTip(tr("新建一个文件"));
  94. connect(newFileAction,SIGNAL(triggered()),this,SLOT(showNewFile()));
  95. //"退出"动作
  96. exitAction = new QAction(tr("退出"), this);
  97. exitAction->setShortcut(tr("Ctrl + Q"));
  98. exitAction->setStatusTip(tr("退出程序"));
  99. connect(exitAction,SIGNAL(triggered(bool)),this,SLOT(close()));
  100. //"复制"动作
  101. copyAction = new QAction(QIcon("copy.png") , tr("复制") , this);
  102. copyAction->setShortcut(tr("Ctrl + C"));
  103. copyAction->setStatusTip(tr("复制文件"));
  104. connect(copyAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(copy()));
  105. //剪切操作
  106. cutAction = new QAction(QIcon("cut.png") , tr("剪切") ,this);
  107. cutAction->setShortcut(tr("Ctrl + X"));
  108. cutAction->setStatusTip(tr("剪切文件"));
  109. connect(cutAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(cut()));
  110. //粘贴操作
  111. pasteAction = new QAction(QIcon("paste.png") , tr("粘贴") ,this);
  112. pasteAction->setShortcut(tr("Ctrl + V"));
  113. pasteAction->setStatusTip(tr("粘贴文件"));
  114. connect(pasteAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(paste()));
  115. //“关于”操作
  116. aboutAction = new QAction(tr("关于") ,this);
  117. connect(aboutAction, SIGNAL(triggered(bool)),this, SLOT(Qapplication::aboutQt()));
  118. //打印文本
  119. printTextAction = new QAction(QIcon("printText.png") , tr("打印文本") ,this);
  120. printTextAction->setStatusTip(tr("打印一个文本"));
  121. connect(printTextAction,SIGNAL(triggered(bool)),this,SLOT(showPrintText()));
  122. //打印图片
  123. printImageAction = new QAction(QIcon("printImage.png") , tr("打印图像") , this);
  124. printImageAction->setStatusTip(tr("打印一幅图像"));
  125. connect(printImageAction,SIGNAL(triggered(bool)),this,SLOT(showPrintImage()));
  126. //放大图片
  127. zoomInAction = new QAction(QIcon("zoomin.png") , tr("放大") ,this);
  128. zoomInAction->setStatusTip(tr("放大一张图片"));
  129. connect(zoomInAction,SIGNAL(triggered(bool)),this,SLOT(showZoomIn()));
  130. //缩小图片
  131. zoomOutAction = new QAction(QIcon("zoomout.png") ,tr("缩小") ,this);
  132. zoomOutAction->setStatusTip(tr("缩小一张图片"));
  133. connect(zoomOutAction,SIGNAL(triggered(bool)),this,SLOT(showZoomOut()));
  134. //旋转图片
  135. rotate90Action = new QAction(QIcon("rotate90.png") , tr("旋转90") ,this);
  136. rotate90Action->setStatusTip(tr("将一幅图旋转90度"));
  137. connect(rotate90Action,SIGNAL(triggered(bool)),this,SLOT(showRotate90()));
  138. rotate180Action = new QAction(QIcon("rotate180.png") ,tr("旋转180") ,this);
  139. rotate180Action->setStatusTip(tr("将一幅图片旋转180度"));
  140. connect(rotate180Action,SIGNAL(triggered(bool)),this,SLOT(showRotate180()));
  141. rotate270Action = new QAction(QIcon("rotate270.png"), tr("旋转270") ,this);
  142. rotate270Action->setStatusTip(tr("将一幅图片旋转270度"));
  143. connect(rotate270Action,SIGNAL(triggered(bool)),this,SLOT(showRotate270()));
  144. //垂直镜像
  145. mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png") , tr("纵向镜像") ,this);
  146. mirrorVerticalAction->setStatusTip(tr("对一张图片做纵向镜像"));
  147. connect(mirrorVerticalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorVertical()));
  148. //水平镜像
  149. mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png") ,tr("横向镜像") ,this);
  150. mirrorHorizontalAction->setStatusTip(tr("对一张图片做横向镜像"));
  151. connect(mirrorHorizontalAction,SIGNAL(triggered(bool)),this,SLOT(showMirrorHorizontal()));
  152. //撤销操作
  153. undoAction = new QAction(QIcon("undo.png"), tr("撤销") ,this);
  154. connect(undoAction, SIGNAL(triggered(bool)),showWidget->text,SLOT(undo()));
  155. //重做操作
  156. redoAction = new QAction(QIcon("redo.png") ,tr("重做") ,this);
  157. connect(redoAction,SIGNAL(triggered(bool)),showWidget->text,SLOT(redo()));
  158. //文字排版功能
  159. actGrp = new QActionGroup(this);
  160. leftAction = new QAction(QIcon("left.png"),"左对齐" ,actGrp);
  161. leftAction->setCheckable(true);
  162. rightAction = new QAction(QIcon("right.png") , "右对齐" ,actGrp);
  163. rightAction->setCheckable(true);
  164. centerAction = new QAction(QIcon("center.png") , "居中" ,actGrp);
  165. centerAction->setCheckable(true);
  166. justifyAction = new QAction(QIcon("justify.png") , "两端对齐" , actGrp);
  167. justifyAction->setCheckable(true);
  168. connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*)));
  169. }
  170. void MainWindow::createMenus()  //创建菜单栏
  171. {
  172. fileMenu = menuBar()->addMenu(tr("文件"));
  173. fileMenu->addAction(openFileAction);
  174. fileMenu->addAction(newFileAction);
  175. fileMenu->addAction(printTextAction);
  176. fileMenu->addAction(printImageAction);
  177. fileMenu->addSeparator();
  178. fileMenu->addAction(exitAction);
  179. zoomMenu = menuBar()->addMenu(tr("编辑"));
  180. zoomMenu->addAction(copyAction);
  181. zoomMenu->addAction(cutAction);
  182. zoomMenu->addAction(pasteAction);
  183. zoomMenu->addAction(aboutAction);
  184. zoomMenu->addSeparator();
  185. zoomMenu->addAction(zoomInAction);
  186. zoomMenu->addAction(zoomOutAction);
  187. rotateMenu = menuBar()->addMenu(tr("旋转"));
  188. rotateMenu->addAction(rotate90Action);
  189. rotateMenu->addAction(rotate180Action);
  190. rotateMenu->addAction(rotate270Action);
  191. mirrorMenu = menuBar()->addMenu(tr("镜像"));
  192. mirrorMenu->addAction(mirrorVerticalAction);
  193. mirrorMenu->addAction(mirrorHorizontalAction);
  194. }
  195. void MainWindow::createToolBars()   //创建工具栏
  196. {
  197. fileTool = addToolBar("File");
  198. fileTool->addAction(openFileAction);
  199. fileTool->addAction(newFileAction);
  200. fileTool->addAction(printTextAction);
  201. fileTool->addAction(printImageAction);
  202. zoomTool = addToolBar("Edit");  //创建文字编辑工具栏
  203. zoomTool->addAction(copyAction);
  204. zoomTool->addAction(cutAction);
  205. zoomTool->addAction(pasteAction);
  206. zoomTool->addSeparator();
  207. zoomTool->addAction(zoomInAction);
  208. zoomTool->addAction(zoomOutAction);
  209. rotateTool = addToolBar("Rotate");  //创建旋转工具栏
  210. rotateTool->addAction(rotate90Action);
  211. rotateTool->addAction(rotate180Action);
  212. rotateTool->addAction(rotate270Action);
  213. doToolBar = addToolBar("DoEdit");   //创建撤销和重做工具栏
  214. doToolBar->addAction(undoAction);
  215. doToolBar->addAction(redoAction);
  216. fontToolBar = addToolBar("Font");   //创建字体工具栏
  217. fontToolBar->addWidget(fontLabel1);
  218. fontToolBar->addWidget(fontComboBox);
  219. fontToolBar->addWidget(fontLabel2);
  220. fontToolBar->addWidget(sizeComboBox);
  221. fontToolBar->addSeparator();    //加入分割线
  222. fontToolBar->addWidget(boldBtn);
  223. fontToolBar->addWidget(italicBtn);
  224. fontToolBar->addWidget(underlineBtn);
  225. fontToolBar->addSeparator();
  226. fontToolBar->addWidget(colorBtn);
  227. listToolBar = addToolBar("list");   //创建文字排版工具栏
  228. listToolBar->addWidget(listLabel);
  229. listToolBar->addWidget(listComboBox);
  230. listToolBar->addSeparator();
  231. listToolBar->addActions(actGrp->actions());
  232. }
  233. void MainWindow::showNewFile()  //新建文件操作
  234. {
  235. MainWindow *newMainWindow = new MainWindow;
  236. newMainWindow->show();
  237. }
  238. void MainWindow::showOpenFile() //读文本是打开新窗口
  239. {
  240. fileName = QFileDialog::getOpenFileName(this);
  241. if(!fileName.isEmpty())
  242. {
  243. if(showWidget->text->document()->isEmpty())
  244. {
  245. loadFile(fileName);
  246. }
  247. else
  248. {
  249. MainWindow *newMainWindow = new MainWindow;
  250. newMainWindow->show();  //当一个窗口已经打开了一个文件,还需要再打开文件时,新生成窗口
  251. newMainWindow->loadFile(fileName);
  252. }
  253. }
  254. }
  255. void MainWindow::loadFile(QString fileName) //载入文件
  256. {
  257. printf("File name:%s\n",fileName.data());
  258. QFile file(fileName);
  259. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
  260. {
  261. QTextStream textStream(&file);
  262. while(!textStream.atEnd())
  263. {
  264. showWidget->text->append(textStream.readLine());
  265. printf("Read line\n");
  266. }
  267. printf("End\\n");
  268. }
  269. }
  270. void MainWindow::showPrintText()    //打印文本
  271. {
  272. QPrinter printer;
  273. QPrintDialog printDialog;
  274. if(printDialog.exec())
  275. {
  276. QTextDocument *doc = showWidget->text->document();
  277. doc->print(&printer);
  278. }
  279. }
  280. void MainWindow::showPrintImage()   //打印图片
  281. {
  282. QPrinter printer;
  283. QPrintDialog printDialog;
  284. if(printDialog.exec())
  285. {
  286. QPainter    painter(&printer);
  287. QRect rect = painter.viewport();
  288. QSize size = img.size();
  289. size.scale(rect.size() , Qt::KeepAspectRatio);
  290. painter.setViewport(rect.x() , rect.y(), size.width(), size.height());
  291. painter.setWindow(img.rect());
  292. painter.drawImage(0,0,img);
  293. }
  294. }
  295. void MainWindow::showZoomIn()   //图片放大
  296. {
  297. if(img.isNull())
  298. {
  299. return ;
  300. }
  301. QMatrix martix;
  302. martix.scale(2,2);
  303. img = img.transformed(martix);
  304. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  305. }
  306. void MainWindow::showZoomOut()  //图片缩小
  307. {
  308. if(img.isNull())
  309. {
  310. return ;
  311. }
  312. QMatrix martix;
  313. martix.scale(0.5,0.5);
  314. img = img.transformed(martix);
  315. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  316. }
  317. void MainWindow::showRotate90() //旋转图片功能
  318. {
  319. if(img.isNull())
  320. {
  321. return;
  322. }
  323. QMatrix matrix;
  324. matrix.rotate(90);
  325. img = img.transformed(matrix);
  326. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  327. }
  328. void MainWindow::showRotate180()
  329. {
  330. if(img.isNull())
  331. {
  332. return;
  333. }
  334. QMatrix matrix;
  335. matrix.rotate(180);
  336. img = img.transformed(matrix);
  337. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  338. }
  339. void MainWindow::showRotate270()
  340. {
  341. if(img.isNull())
  342. {
  343. return;
  344. }
  345. QMatrix matrix;
  346. matrix.rotate(270);
  347. img = img.transformed(matrix);
  348. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  349. }
  350. void MainWindow::showMirrorHorizontal() //水平镜像
  351. {
  352. if(img.isNull())
  353. {
  354. return ;
  355. }
  356. img = img.mirrored(true,false);
  357. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  358. }
  359. void MainWindow::showMirrorVertical()   //垂直镜像
  360. {
  361. if(img.isNull())
  362. {
  363. return ;
  364. }
  365. img = img.mirrored(false, true);
  366. showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
  367. }
  368. void MainWindow::showFontComboBox(QString comboStr) //设置字体
  369. {
  370. QTextCharFormat fmt;
  371. fmt.setFontFamily(comboStr);
  372. mergeFormat(fmt);
  373. }
  374. void MainWindow::mergeFormat(QTextCharFormat format)
  375. {
  376. QTextCursor cursor = showWidget->text->textCursor();
  377. if(!cursor.hasSelection())
  378. {
  379. cursor.select(QTextCursor::WordUnderCursor);
  380. }
  381. cursor.mergeCharFormat(format);
  382. showWidget->text->mergeCurrentCharFormat(format);
  383. }
  384. void MainWindow::showSizeSpinBix(QString spinvalue) //设置字号
  385. {
  386. QTextCharFormat fmt;
  387. fmt.setFontPointSize(spinvalue.toFloat());
  388. showWidget->text->mergeCurrentCharFormat(fmt);
  389. }
  390. void MainWindow::showBoldBtn()  //设置文字加粗
  391. {
  392. QTextCharFormat fmt;
  393. fmt.setFontWeight(boldBtn->isCheckable() ? QFont::Bold : QFont::Normal);
  394. showWidget->text->mergeCurrentCharFormat(fmt);
  395. }
  396. void MainWindow::showItalicBtn()
  397. {
  398. QTextCharFormat fmt;
  399. fmt.setFontItalic(italicBtn->isCheckable());
  400. showWidget->text->mergeCurrentCharFormat(fmt);
  401. }
  402. void MainWindow::showUnderlineBtn()
  403. {
  404. QTextCharFormat fmt;
  405. fmt.setFontUnderline(underlineBtn->isCheckable());
  406. showWidget->text->mergeCurrentCharFormat(fmt);
  407. }
  408. void MainWindow::showColorBtn()
  409. {
  410. QColor color = QColorDialog::getColor(Qt::red, this);
  411. if(color.isValid())
  412. {
  413. QTextCharFormat fmt;
  414. fmt.setForeground(color);
  415. showWidget->text->mergeCurrentCharFormat(fmt);
  416. }
  417. }
  418. void MainWindow::showCurrentFormatChanged(const QTextCharFormat &fmt)
  419. {
  420. fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily()));
  421. sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize())));
  422. boldBtn->setCheckable(fmt.font().bold());
  423. italicBtn->setCheckable(fmt.fontItalic());
  424. underlineBtn->setCheckable(fmt.fontUnderline());
  425. }
  426. void MainWindow::showAlignment(QAction *act)
  427. {
  428. if(act == leftAction)
  429. {
  430. showWidget->text->setAlignment(Qt::AlignLeft);
  431. }
  432. if(act == rightAction)
  433. {
  434. showWidget->text->setAlignment(Qt::AlignRight);
  435. }
  436. if(act == centerAction)
  437. {
  438. showWidget->text->setAlignment(Qt::AlignCenter);
  439. }
  440. if(act == justifyAction)
  441. {
  442. showWidget->text->setAlignment(Qt::AlignJustify);
  443. }
  444. }
  445. void MainWindow::showCursorPositionChanged()
  446. {
  447. if(showWidget->text->alignment() == Qt::AlignLeft)
  448. {
  449. leftAction->setCheckable(true);
  450. }
  451. if(showWidget->text->alignment() == Qt::AlignRight)
  452. {
  453. rightAction->setCheckable(true);
  454. }
  455. if(showWidget->text->alignment() == Qt::AlignCenter)
  456. {
  457. centerAction->setCheckable(true);
  458. }
  459. if(showWidget->text->alignment() == Qt::AlignJustify)
  460. {
  461. justifyAction->setCheckable(true);
  462. }
  463. }
  464. void MainWindow::showList(int index)
  465. {
  466. QTextCursor cursor = showWidget->text->textCursor();
  467. if(index != 0)
  468. {
  469. QTextListFormat::Style style = QTextListFormat::ListDisc;
  470. switch(index)
  471. {
  472. default:
  473. case 1 :
  474. style = QTextListFormat::ListDisc;
  475. break;
  476. case 2:
  477. style = QTextListFormat::ListCircle;
  478. break;
  479. case 3:
  480. style = QTextListFormat::ListSquare;
  481. break;
  482. case 4:
  483. style = QTextListFormat::ListDecimal;
  484. break;
  485. case 5:
  486. style = QTextListFormat::ListLowerAlpha;
  487. break;
  488. case 6:
  489. style = QTextListFormat::ListUpperAlpha;
  490. break;
  491. case 7:
  492. style = QTextListFormat::ListLowerRoman;
  493. break;
  494. case 8:
  495. style = QTextListFormat::ListUpperRoman;
  496. }
  497. cursor.beginEditBlock();    //设置缩进值
  498. QTextBlockFormat blockFmt = cursor.blockFormat();
  499. QTextListFormat listFmt;
  500. if(cursor.currentList())
  501. {
  502. listFmt = cursor.currentList()->format();
  503. }
  504. else
  505. {
  506. listFmt.setIndent(blockFmt.indent() + 1);
  507. blockFmt.setIndent(0);
  508. cursor.setBlockFormat(blockFmt);
  509. }
  510. listFmt.setStyle(style);
  511. cursor.createList(listFmt);
  512. cursor.endEditBlock();
  513. }
  514. else
  515. {
  516. QTextBlockFormat bfmt;
  517. bfmt.setObjectIndex(-1);
  518. cursor.mergeBlockFormat(bfmt);
  519. }
  520. }
  521. MainWindow::~MainWindow()
  522. {
  523. }

showwidget.h源代码:

[cpp] view plaincopy print?
  1. #ifndef SHOWWIDGET_H
  2. #define SHOWWIDGET_H
  3. #include <QWidget>
  4. #include <QLabel>
  5. #include <QTextEdit>
  6. #include <QImage>
  7. class ShowWidget : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit ShowWidget(QWidget *parent = 0);
  12. QImage img;
  13. QLabel *imageLabel;
  14. QTextEdit *text;
  15. signals:
  16. public slots:
  17. };
  18. #endif // SHOWWIDGET_H

showwidget.cpp源代码:

[cpp] view plaincopy print?
  1. #include "showwidget.h"
  2. #include <QHBoxLayout>
  3. ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)
  4. {
  5. imageLabel = new QLabel;
  6. imageLabel->setScaledContents(true);    //设置允许控件自动调整大小,以使内容填充整个有效区域
  7. text = new QTextEdit;
  8. QHBoxLayout *mainLayout = new QHBoxLayout(this);    //水平盒子布局
  9. mainLayout->addWidget(imageLabel);
  10. mainLayout->addWidget(text);
  11. }

程序中未解决的bug:

1、文本段落对齐功能有问题,不能实现其功能。(我仔细检查了代码,仍未发现问题所在,路过的大神各显神通吧,望不惜赤脚)。

源代码下载地址:http://download.csdn.net/detail/rl529014/9546463

Qt5主窗体程序: 文本编辑器的实现(Qt5开发及实例)相关推荐

  1. html5在哪编辑器,HTML5文本编辑器推荐-属于Web开发人员的HTML5编辑器

    HTML5文本编辑器推荐-属于Web开发人员的HTML5编辑器 Web开发人员和设计人员经常需要用到HTML5文本编辑器,同时它也是Web开发人员最为重要的工具之一.由于HTML5技术的不断发展,为此 ...

  2. Java 实训 使用弹出对话框设置主窗体中的文本字体

     实训要求: 编写一个主窗体,该窗体有"设置字体"按钮和一个文本区. 编写一个JDialog的子类; FontDialog,其中使用下拉列表显示全部 字体的名称,当选择下拉列表中某 ...

  3. 如何用C#编写文本编辑器

    如何用C#编写文本编辑器[2005-8-24版] 南京千里独行2005版权所有,不限转载,请保留版权声明 作者Blog:http://blog.csdn.net/yyf9989/ 摘要 本文探讨了使用 ...

  4. linux编译c gedit,[2018年最新整理]LINUX-Gedit文本编辑器.ppt

    [2018年最新整理]LINUX-Gedit文本编辑器 Gedit文本编辑器 系别:电信系 班级:08 自动化 姓名:张小亚 学号: 30 号 辅导老师:兰建平 Gedit的启动与打开文本 Gedit ...

  5. linux编辑文本工具栏,LINUX-Gdit文本编辑器.ppt

    LINUX-Gdit文本编辑器 Gedit文本编辑器 系别:电信系 班级:08 自动化 姓名:张小亚 学号: 30 号 辅导老师:兰建平 Gedit的启动与打开文本 Gedit可以用命令或主菜单的方式 ...

  6. 列模式 文本编辑器_UltraEdit 24.2 文本编辑器免费版

    UltraEdit是由IDM官方最新出品的文本编辑器版本,它是一款性价比全球一流的文本.十六进制,HTML.PHP.Java 和 JavaScript程序文本编辑器,被认为是世界上最好的文本编辑器.U ...

  7. 文本编辑器_markdown编辑器与富文本编辑器优缺点比较

    其实对于用户来说,Markdown编辑器和富文本编辑器的作用是一样的,功能上也没有什么区别,在Markdown诞生之前大家都是用富文本编辑器的,也没见什么功能不能实现的,而两者主要区别在于他们的使用方 ...

  8. [译] 为数字优先新闻编辑室开发文本编辑器

    原文地址:Building a Text Editor for a Digital-First Newsroom 原文作者:Sophia Ciocca 译文出自:掘金翻译计划 本文永久链接:githu ...

  9. ANSI环境下支持多语言输入的单行文本编辑器 V0.01

    File:      SMLInput Name:      ANSI环境下支持多语言输入的单行文本编辑器 Author:    zyl910 Blog:      http://blog.csdn. ...

最新文章

  1. 吴恩达深度学习笔记(105)-人脸识别之面部验证与二分类
  2. 半导体基础知识(3):双极结和场效应晶体管(BJT和FET)
  3. 数据结构 python的书推荐-为什么程序员一定要学数据结构?数据结构书单推荐~...
  4. 人工智能学习实战之路
  5. POJ 3750 小孩报数问题
  6. 函数、指针、数组的组合 及结构体和共用体
  7. delphi使用存储过程
  8. IOS基础之打砖块项目演练
  9. 201421123042 《Java程序设计》第8周学习总结
  10. 真正聪明的人,为什么从不去社交?
  11. Effective C++学习第五天
  12. 1859. 将句子排序
  13. dart开发Android服务,关于android:在Flutter应用中使用由swagger生成的Dart代码生成的Web服务...
  14. 可行性研究报告模板 Shane版
  15. Struts2 面试题
  16. 玩王者荣耀用不好英雄?两阶段算法帮你精准推荐精彩视频
  17. 晴天计算机按键,【图】超实用的ML系列操控快速入门,新手必存(按钮示意图)...
  18. java 破解版 aspose-word 切图
  19. Matlab中的参数解析
  20. 600度近视眼恢复方法_近视孩子的家长看看:600度以上近视可致盲,不花钱恢复视力法...

热门文章

  1. 三轴桁架机械手控制系统 用于数控车床自动上下料
  2. SEO不可或缺的优化技巧——友链
  3. 基于Python的数独游戏设计与实现 课程文档+任务书+项目源码
  4. MRT115 热电堆温度传感器
  5. 阿里云服务器内网ip有什么用
  6. 社保管理客户端显示服务器异常,必看!社保管理客户端的5个热点问题解答
  7. 如何把一个excel工作薄中N个工作表复制到另一个工作薄中
  8. 【流放之路-装备制作篇】
  9. java培训学习之Java桌面软件开发
  10. 国产新秀---XS5018A,芯昇,图像信号处理芯片