主体代码实现

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include<QMenu>
  4. #include<QMenuBar>
  5. #include<QToolBar>
  6. #include<QDockWidget>
  7. #include<QTextEdit>
  8. #include<QDialog>
  9. #include<QAction>
  10. #include<QMessageBox>
  11. #include<QWindow>
  12. #include <QFile>
  13. #include <QFileDialog>
  14. #include <QTextStream>
  15. #include <QIcon>
  16. #include<QString>
  17. #include<QFont>
  18. #include<QDebug>
  19. #include<QFontDialog>
  20. #include<QColorDialog>
  21. #include<QPalette>
  22. #include<QColor>
  23. #include<QMessageBox>
  24. #include<QImage>
  25. #include<QTextDocumentFragment>
  26. #include<QPicture>
  27. #include<QFile>
  28. MainWindow::MainWindow(QWidget *parent)
  29. : QMainWindow(parent)
  30. , ui(new Ui::MainWindow)
  31. {
  32. ui->setupUi(this);
  33. //背景
  34. edit->setParent(this);
  35. setCentralWidget(edit);
  36. QPalette pl = edit->palette();
  37. setWindowTitle("记事本鬼灭之刃版");
  38. this->resize(1000,800);
  39. setStyleSheet("border-image:url(:/a3.jpg)");
  40. //菜单
  41. QMenuBar *bar=menuBar();
  42. setMenuBar(bar);
  43. bar->addSeparator();
  44. QMenu *m8=bar->addMenu("文件");
  45. QMenu *m1=bar->addMenu("工具");
  46. QMenu *m2=bar->addMenu("字号");
  47. QMenu *m3=bar->addMenu("颜色");
  48. QMenu *m9=bar->addMenu("字体");
  49. QMenu *m4=bar->addMenu("撤回");
  50. QMenu *m5=bar->addMenu("恢复");
  51. QMenu *m7=bar->addMenu("帮助");
  52. //文件
  53. //新建
  54. QAction *file=new QAction(tr("新建"));
  55. file->setShortcut(tr("Ctrl+N"));
  56. m8->addAction(file);
  57. m8->addSeparator();
  58. connect(file,&QAction::triggered,this,&MainWindow::newfile);
  59. //打开
  60. QAction *open=new QAction(tr("打开"));
  61. open->setShortcut(tr("Ctrl+X"));
  62. m8->addAction(open);
  63. m8->addSeparator();
  64. connect(open,&QAction::triggered,this,&MainWindow::open);
  65. //关闭
  66. QAction *close=new QAction(tr("关闭"));
  67. close->setShortcut(tr("Ctrl+L"));
  68. m8->addAction(close);
  69. m8->addSeparator();
  70. connect(close,&QAction::triggered,this,&MainWindow::closefun);
  71. //工具
  72. //复制
  73. QAction *copy=new QAction(tr("复制"));
  74. copy->setShortcut(tr("Ctrl+C"));
  75. m1->addAction(copy);
  76. m1->addSeparator();
  77. connect(copy,&QAction::triggered,this,&MainWindow::copyfun);
  78. //粘贴
  79. QAction *copywrite=new QAction(tr("粘贴"));
  80. copywrite->setShortcut(tr("Ctrl+V"));
  81. m1->addAction(copywrite);
  82. m1->addSeparator();
  83. connect(copywrite,&QAction::triggered,this,&MainWindow::copywritefun);
  84. //另存为
  85. QAction *down=new QAction(tr("另存为"));
  86. down->setShortcut(tr("Ctrl+A"));
  87. m1->addAction(down);
  88. m1->addSeparator();
  89. connect(down,&QAction::triggered,this,&MainWindow::downfun);
  90. //字号
  91. //手写
  92. QAction *a1=new QAction("一号");
  93. m2->addAction(a1);
  94. m2->addSeparator();
  95. connect(a1,&QAction::triggered,[=](){
  96. edit->setStyleSheet("font-size : 12px");
  97. });
  98. QAction *a2=new QAction("二号",m2);
  99. m2->addAction(a2);
  100. m2->addSeparator();
  101. connect(a2,&QAction::triggered,[=](){
  102. edit->setStyleSheet("font-size : 14px");
  103. });
  104. QAction *a3=new QAction("三号",m2);
  105. m2->addAction(a3);
  106. m2->addSeparator();
  107. connect(a3,&QAction::triggered,[=](){
  108. edit->setStyleSheet("font-size : 16px");
  109. });
  110. QAction *a4=new QAction("四号",m2);
  111. m2->addAction(a4);
  112. m2->addSeparator();
  113. connect(a4,&QAction::triggered,[=](){
  114. edit->setStyleSheet("font-size : 18px");
  115. });
  116. QAction *a5=new QAction("五号",m2);
  117. m2->addAction(a5);
  118. m2->addSeparator();
  119. connect(a5,&QAction::triggered,[=](){
  120. edit->setStyleSheet("font-size : 20px");
  121. });
  122. QAction *a6=new QAction("六号",m2);
  123. m2->addAction(a6);
  124. m2->addSeparator();
  125. connect(a6,&QAction::triggered,[=](){
  126. edit->setStyleSheet("font-size : 22px");
  127. });
  128. QAction *a7=new QAction("七号",m2);
  129. m2->addAction(a7);
  130. m2->addSeparator();
  131. connect(a7,&QAction::triggered,[=](){
  132. edit->setStyleSheet("font-size : 24px");
  133. });
  134. QAction *a8=new QAction("八号",m2);
  135. m2->addAction(a8);
  136. m2->addSeparator();
  137. connect(a8,&QAction::triggered,[=](){
  138. edit->setStyleSheet("font-size : 26px");
  139. });
  140. //对话框
  141. QAction *a9=new QAction("更多选择");
  142. m2->addAction(a9);
  143. m2->addSeparator();
  144. connect(a9,&QAction::triggered,[=](){
  145. bool ok;
  146. QFont font = QFontDialog::getFont(&ok,this);
  147. if(ok)
  148. edit->setFont(font);
  149. else
  150. qDebug()<<tr("没有选择字体");
  151. } );
  152. //颜色
  153. //手写— 红
  154. QAction *red=new QAction("红色");
  155. m3->addAction(red);
  156. m3->addSeparator();
  157. connect(red,&QAction::triggered,[=](){
  158. edit->setTextColor(QColor(255,0,0));
  159. });
  160. //手写- 绿
  161. QAction *green=new QAction("绿色");
  162. m3->addAction(green);
  163. m3->addSeparator();
  164. connect(green,&QAction::triggered,[=](){
  165. edit->setTextColor(QColor(0,255,0));
  166. });
  167. //手写- 蓝
  168. QAction *blue=new QAction("蓝色",m3);
  169. m3->addAction(blue);
  170. m3->addSeparator();
  171. connect(blue,&QAction::triggered,[=](){
  172. edit->setTextColor(QColor(0,0,255));
  173. });
  174. //对话框
  175. QAction *cmore=new QAction("更多颜色");
  176. m3->addAction(cmore);
  177. connect(cmore,&QAction::triggered,[=](){
  178. QColor color = QColorDialog::getColor();
  179. if(color.isValid()){
  180. edit->setTextColor(color);
  181. }
  182. });
  183. //字体
  184. //手写加粗斜体
  185. QAction *w1=new QAction("加粗");
  186. QAction *w2=new QAction("斜体");
  187. m9->addAction(w1);
  188. m9->addSeparator();
  189. m9->addAction(w2);
  190. m9->addSeparator();
  191. connect(w1,&QAction::triggered,[=]{
  192. edit->setStyleSheet("font-weight: bold;");
  193. });
  194. connect(w2,&QAction::triggered,[=]{
  195. edit->setFontItalic(true);
  196. });
  197. //更换字体
  198. //撤回
  199. QAction *x1=new QAction("撤回");
  200. m4->addAction(x1);
  201. m4->addSeparator();
  202. x1->setShortcut(tr("Ctrl+Z"));
  203. connect(x1,&QAction::triggered,[=](){
  204. edit->undo();
  205. });
  206. //恢复
  207. QAction *x2=new QAction("恢复");
  208. m5->addAction(x2);
  209. m5->addSeparator();
  210. x2->setShortcut(tr("Ctrl+M"));
  211. connect(x2,&QAction::triggered,[=](){
  212. edit->redo();
  213. });
  214. //帮助
  215. QAction *hlep=new QAction("帮助");
  216. m7->addAction(hlep);
  217. hlep->setShortcut(tr("Ctrl+H"));
  218. connect(hlep,&QAction::triggered,[=](){
  219. QDialog dialog;
  220. dialog.setWindowTitle(tr("懒得写帮助,自己用用看吧"));
  221. dialog.exec();
  222. });
  223. }
  224. MainWindow::~MainWindow()
  225. {
  226. delete ui;
  227. }
  228. void MainWindow::newfile(){
  229. edit->clear();
  230. edit->setText(QString());
  231. }
  232. void MainWindow::closefun(){
  233. QMessageBox box;
  234. box.setText("确定要关闭吗?");//用模态对话框来实现。
  235. box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
  236. if(box.exec()==QMessageBox::No){
  237. return;
  238. }
  239. else{
  240. this->close();
  241. }
  242. }
  243. void MainWindow::copyfun(){
  244. edit->copy();
  245. }
  246. void MainWindow::copywritefun(){
  247. edit->paste();
  248. }
  249. void MainWindow::downfun(){
  250. QString fn = QFileDialog::getSaveFileName(this,"另存为");
  251. QFile file(fn);
  252. file.open(QIODevice::Text|QIODevice::WriteOnly);//打开方式只写,并且是Text
  253. QTextStream in(&file);
  254. QString filecontent = edit->toPlainText();//toPlainText读取
  255. in << filecontent;
  256. file.close();
  257. }
  258. void MainWindow::open(){
  259. QString FileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
  260. QFile *file=new QFile;
  261. file->setFileName(FileName);
  262. bool ok=file->open(QIODevice::ReadOnly); //判断是否只读模式打开
  263. if(ok){
  264. QTextStream in(file); //类比c++的io流,使之与file绑定
  265. edit->setText(in.readAll()); //这个用法学c++的时候我还没看过
  266. file->close();//关闭文件和c++一样
  267. delete file;
  268. }
  269. else return;//打不开
  270. }

头文件代码

  1. Mainwindow.h
  2. #ifndef MAINWINDOW_H
  3. #define MAINWINDOW_H
  4. #include <QMainWindow>
  5. #include<QTextEdit>
  6. #include <QMouseEvent>
  7. #include<QDockWidget>
  8. #include <QPaintEvent>
  9. QT_BEGIN_NAMESPACE
  10. namespace Ui { class MainWindow; }
  11. QT_END_NAMESPACE
  12. class MainWindow : public QMainWindow
  13. {
  14. Q_OBJECT
  15. public:
  16. MainWindow(QWidget *parent = nullptr);
  17. ~MainWindow();
  18. private:
  19. Ui::MainWindow *ui;
  20. QString now;
  21. QTextEdit *edit=new QTextEdit;
  22. public slots:
  23. void open();
  24. void copyfun();
  25. void copywritefun();
  26. void downfun();
  27. void newfile();
  28. void closefun();
  29. };
  30. #endif // MAINWINDOW_H

Qt记事本,美化版的更多相关文章

  1. 联想A798T刷机包 基于百度云V6 集成RE3.1.7美化版 精简冗余文件

    ROM介绍 1.apk进行odex合并及zipaliang优化-省电及降低内存暂用. 2.測试相机.通话.数据.wifi.蓝牙.等传感器均正常,. 3.提供时间居中防iphone状态栏补丁 4.增加I ...

  2. HTML5小游戏UI美化版

    HTML5小游戏[是男人就下一百层]UI美化版 之前写的小游戏,要么就比较简单,要么就是比较难看,或者人物本身是不会动的. 结合了其它人的经验,研究了一下精灵运动,就写一个简单的小游戏来试一下. 介绍 ...

  3. 23.QT记事本

    描述 主要功能有: 新建,打开,保存,另存为,打印, 编辑,撤销,,拖放,xml配置文件读写,字体更改,查找替换 菜单栏,工具栏,状态栏的实现 如下图所示: 效果如下所示:   源码下载地址: htt ...

  4. 通过flask实现web页面简单的增删改查bootstrap美化版

    通过flask实现web页面简单的增删改查bootstrap美化版 项目目录结构 [root@node1 python]# tree -L 2 . ├── animate.css ├── fileut ...

  5. 10个HTML5美化版复选框和单选框

    单选框Radiobox和复选框checkbox在网页中也十分常见,虽然它没有按钮的交互性强,但是如果能把它们像按钮那样美化一下,那也是非常不错的.本文收集了10个相对比较漂亮的美化版单选框和复选框,希 ...

  6. Qt: 记事本源代码

    界面编程之实例学习,系统记事本是个极好的参考,初学Delphi及后之c#,皆以记事本为参考,今以Qt学习,亦是如此. 期间搭建开发环境,复习c++知识,寻找模块对应功能,不一而足:现刻录其模块代码,以 ...

  7. 太完美 TWM000极度精简版XP20130123终结美化版

    TWM000极度精简版XP20130123终结美化版:蛋蛋20130123终结版为蓝本,虫子提供的美化包进行了美化.此版经测试完美在Z77主板开启AHCI安装,此为最终版之美化版!LiteXPMH.i ...

  8. Qt界面美化 QSS

    目前发现在Qt-Design中右击控件,可以选择Change StyleSheet ------------------------以下总结不太对 刚接触Qt,发现Qt Design无法对每个控件进行 ...

  9. Qt做发布版,解决声音和图片、中文字体乱码问题(需要在main里写上QApplication::addLibraryPath("./plugins")才能加载图片,有图片,很清楚)

    前些天做Qt发布版,发现居然不显示图片,后来才发现原来还有图片的库没加!找找吧,去qt的安装包,我装在了F盘,在F盘F:/QT/qt/plugins,找到了plugins,这里面有个 imagefor ...

随机推荐

  1. C预备知识_001

    程序由什么构成? 1.对数据的描述:在程序中要指定用到哪些数据以及这些数据的类型和数据的组织形式,其实这就是数据结构(data structure). 2.对操作的描述:即要求计算机就行操作的步骤,也 ...

  2. iOS,开发准备之申请证书 ---by吴帮雷

    一.申请真机调试证书 打开iOS Dev Center,选择Sign in,登陆(至少99美元账号),登陆选择Certificates,Identifiers & Profiles --> ...

  3. python官网导航翻译

  4. Oracle用户创建、删除和授权等方法总结

    一.查看用户及权限 1.查询所有用户: 1.1.查看所有用户基本信息 select * from all_users; 1.2.查看所有用户相信信息 select * from dba_users; ...

  5. 矩阵QR分解

    1 orthonormal 向量与 Orthogonal 矩阵 orthonormal 向量定义为 ,任意向量  相互垂直,且模长为1: 如果将  orthonormal 向量按列组织成矩阵,矩阵为  ...

  6. Solution -「ARC 082D」Sandglass

    \(\mathcal{Description}\)   Link.   一个沙漏内共 \(Xg\) 沙,令初始时上半部分为 A,下半部分为 B.沙漏在 \(r_1,r_2,\cdots,r_n\) 时 ...

  7. Redis 学习-上

    一.Redis 概述 1.1.Redis 定义 Redis (Remote Dictionary Server):远程字典服务 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化 ...

  8. [LeetCode]1431. 拥有最多糖果的孩子

    给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目. 对每一个孩子,检查是否存在一种方案,将额外的 extraCandi ...

  9. rinetd基于内网TCP端口转发

    在Linux系统中大多数情况选择用iptables来实现端口转发,iptables虽然强大,但配置不便,而且新手容易出错.在此分享另一个TCP/UDP端口转发工具rinetd,rinetd体积小巧,配 ...

  10. 分析HTTP请求以降低HTTP走私攻击HTTP数据接收不同步攻击的风险

    写在前面的话 HTTP/1.1自1991年至2014年,已经走过了一段很长的发展历程: HTTP/0.9– 1991 HTTP/1.0– 1996 HTTP/1.1 RFC 2068– 1997 RF ...