当前位置:   article > 正文

用Qt完成一个记事本_qt 记事本

qt 记事本

实验环境

ASUS VivoBook + Windows10 + QtCreator

简介

windows操作系统中,记事本 是一个小的应用程序,采用一个简单的文本编辑器进行文字信息的记录和存储。

记事本可以保存无格式文件,可以把记事本编辑的文件保存为:“ .html ”, “ .java ”,“ .asp ”等任意格式。这使得记事本又可以作为程序语言的编辑器。

截图示例

记事本功能

文件

  • 新建(创建新的记事本对象)

  • 新窗口(同上)

  • 打开(选择txt文件打开到记事本中)

  • 保存(将当前编辑的txt文件进行保存)

  • 另存为(将当前编辑的txt文件存储到指定位置)

编辑

  • 撤销(回滚刚才的操作)

  • 复制(将选中的文本进行拷贝)

  • 粘贴(将选中的文本复现到指定位置)

  • 剪切(将选中的文本删除后复现到指定位置)

  • 全选(选中全部内容)(选中全部内容)

格式

  • 自动换行(换行设置)

  • 字体(字体调整)

帮助

关于记事本

代码

main.cpp

  1. #include "notewindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. noteWindow w;
  7. w.show();
  8. return a.exec();
  9. }

main.cpp创建出notewindow类型的对象。

notewindow.cpp

  1. #include "notewindow.h"
  2. #include "ui_notewindow.h"
  3. #include <QMenuBar>
  4. #include <QMenu>
  5. #include <QAction>
  6. #include <QToolBar>
  7. #include <QStatusBar>
  8. #include <QTextEdit>
  9. #include <QDockWidget>
  10. #include <QLabel>
  11. #include <QMessageBox>
  12. #include <QFile>
  13. #include <QFileDialog>
  14. #include <QTextStream>
  15. #include <QCloseEvent>
  16. #include <QTextDocument>
  17. #include <QDateTime>
  18. #include <QDebug>
  19. #include <QFontDialog>
  20. noteWindow::noteWindow(QWidget *parent)
  21. : QMainWindow(parent)
  22. , ui(new Ui::noteWindow)
  23. {
  24. ui->setupUi(this);
  25. this->setWindowTitle("*新建文本文档.txt - 记事本");
  26. //菜单栏
  27. QMenuBar *mBar=this->menuBar();
  28. //菜单项
  29. QMenu *menuFile=mBar->addMenu("文件(F)");
  30. QMenu *menuEdit=mBar->addMenu("编辑(E)");
  31. QMenu *menuOperate=mBar->addMenu("格式(O)");
  32. QMenu *menuVisit=mBar->addMenu("查看(V)");
  33. QMenu *menuHelp=mBar->addMenu("帮助(H)");
  34. //菜单动作
  35. //新建
  36. QAction *actionNew=menuFile->addAction("新建(N)");
  37. //设置快捷键
  38. actionNew->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
  39. //信号与槽连接
  40. connect(actionNew,&QAction::triggered,this,&noteWindow::slotNew);
  41. //新窗口
  42. QAction *actionNewWin=menuFile->addAction("新窗口(W)");
  43. //设置快捷键
  44. actionNewWin->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT+ Qt::Key_N));
  45. //信号与槽连接
  46. connect(actionNewWin,&QAction::triggered,this,&noteWindow::slotNewWin);
  47. //打开
  48. QAction *actionOpen=menuFile->addAction("打开(O)...");
  49. //设置快捷键
  50. actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
  51. //信号与槽连接
  52. connect(actionOpen,&QAction::triggered,this,&noteWindow::slotOpen);
  53. //保存
  54. QAction *actionSave=menuFile->addAction("保存(S)");
  55. //设置快捷键
  56. actionSave->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
  57. //信号与槽连接
  58. connect(actionSave,&QAction::triggered,this,&noteWindow::slotSave);
  59. //另存为
  60. QAction *actionSaveNew=menuFile->addAction("另存为(A)...");
  61. //设置快捷键
  62. actionSaveNew->setShortcut(QKeySequence(Qt::CTRL +Qt::SHIFT +Qt::Key_S));
  63. //信号与槽连接
  64. connect(actionSaveNew,&QAction::triggered,this,&noteWindow::slotSaveAs);
  65. //分割线
  66. menuFile->addSeparator();
  67. //页面设置
  68. menuFile->addAction("页面设置(U)...");
  69. //打印
  70. QAction *actionPrint = menuFile->addAction("打印(P)...");
  71. //设置快捷键
  72. actionPrint->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_P));
  73. //分割线
  74. menuFile->addSeparator();
  75. //退出
  76. QAction *actionQuit=menuFile->addAction("退出(X)");
  77. //设置快捷键
  78. actionQuit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_X));
  79. //信号与槽连接
  80. connect(actionQuit,&QAction::triggered,this,&noteWindow::quitApp);
  81. //编辑
  82. //撤销
  83. QAction *actionZre=menuEdit->addAction("撤销(Z)");
  84. //设置快捷键
  85. actionZre->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Z));
  86. //信号与槽连接
  87. connect(actionZre,&QAction::triggered,this,&noteWindow::slotUndo);
  88. //分割线
  89. menuEdit->addSeparator();
  90. //剪切
  91. QAction *actionX=menuEdit->addAction("剪切(X)");
  92. //设置快捷键
  93. actionX->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_X));
  94. //信号与槽连接
  95. connect(actionX,&QAction::triggered,this,&noteWindow::slotCut);
  96. //复制
  97. QAction *actionC=menuEdit->addAction("复制(C)");
  98. //设置快捷键
  99. actionC->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
  100. //信号与槽连接
  101. connect(actionC,&QAction::triggered,this,&noteWindow::slotCopy);
  102. //粘贴
  103. QAction *actionV=menuEdit->addAction("粘贴(V)");
  104. //设置快捷键
  105. actionV->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_V));
  106. //信号与槽连接
  107. connect(actionV,&QAction::triggered,this,&noteWindow::slotPaste);
  108. //删除
  109. QAction *actionL=menuEdit->addAction("删除(L)");
  110. //设置快捷键
  111. actionL->setShortcut(QKeySequence(Qt::Key_Delete));
  112. //信号与槽连接
  113. connect(actionL,&QAction::triggered,this,&noteWindow::slotDelete);
  114. //分割线
  115. menuEdit->addSeparator();
  116. //使用Bing搜索
  117. QAction *actionBing=menuEdit->addAction("使用Bing搜索...");
  118. //设置快捷键
  119. actionBing->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
  120. //查找
  121. QAction *actionF=menuEdit->addAction("查找(F)...");
  122. //设置快捷键
  123. actionF->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));
  124. //查找下一个
  125. QAction *actionLatter=menuEdit->addAction("查找下一个(N)");
  126. //设置快捷键
  127. actionLatter->setShortcut(QKeySequence(Qt::Key_F3));
  128. //查找上一个
  129. QAction *actionFormer=menuEdit->addAction("查找上一个(V)");
  130. //设置快捷键
  131. actionFormer->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_F3));
  132. //替换
  133. QAction *actionH=menuEdit->addAction("替换(H)...");
  134. //设置快捷键
  135. actionH->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_H));
  136. //转到
  137. QAction *actionG=menuEdit->addAction("转到(G)...");
  138. //设置快捷键
  139. actionG->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_G));
  140. //分割线
  141. menuEdit->addSeparator();
  142. //全选
  143. QAction *actionAll=menuEdit->addAction("全选(A)");
  144. //设置快捷键
  145. actionAll->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_A));
  146. //时间/日期
  147. QAction *actionTime=menuEdit->addAction("时间/日期(D)");
  148. //设置快捷键
  149. actionTime->setShortcut(QKeySequence(Qt::Key_F5));
  150. //信号与槽连接
  151. connect(actionTime,&QAction::triggered,this,&noteWindow::slotTime);
  152. //格式
  153. //自动换行
  154. menuOperate->addAction("自动换行(W)");
  155. //字体
  156. QAction *actionFont=menuOperate->addAction("字体(F)...");
  157. //信号与槽连接
  158. connect(actionFont,&QAction::triggered,this,&noteWindow::slotFont);
  159. //查看
  160. //缩放
  161. menuVisit->addAction("缩放(Z)");
  162. //状态栏
  163. actionStatus = menuVisit->addAction("• 状态栏");
  164. //信号与槽连接
  165. connect(actionStatus,&QAction::triggered,this,&noteWindow::slotShowStatusOrNot);
  166. //帮助
  167. //查看帮助
  168. menuHelp->addAction("查看帮助(H)");
  169. //发送反馈
  170. menuHelp->addAction("发送反馈(F)");
  171. //分割线
  172. menuHelp->addSeparator();
  173. //关于记事本
  174. QAction *actionAbout=menuHelp->addAction("关于记事本(A)");
  175. //信号与槽连接
  176. connect(actionAbout,&QAction::triggered,this,&noteWindow::showAbout);
  177. //文本编辑框
  178. //设置默认字体为宋体10号字
  179. textEdit->setFont(QFont("微软雅黑", 12));
  180. //设置文本区域为整个窗口的中央部件
  181. setCentralWidget(textEdit);
  182. //状态栏
  183. sBar=this->statusBar();
  184. //编辑标签labelText
  185. labelText = new QLabel();
  186. labelText->setText("第1行,第1列");
  187. //添加标签labelText2
  188. labelText2 = new QLabel();
  189. labelText2->setText("100% ");
  190. //添加标签labelText3
  191. labelText3 = new QLabel();
  192. labelText3->setText("Windows(CRLF) ");
  193. //添加标签labelText4
  194. labelText4 = new QLabel();
  195. labelText4->setText("UTF-8 ");
  196. //显示标签
  197. if (true == showOrNot){
  198. sBar->addWidget(labelText);
  199. sBar->addWidget(labelText2);
  200. sBar->addWidget(labelText3);
  201. sBar->addWidget(labelText4);
  202. }
  203. //信号与槽连接
  204. connect(textEdit,&QTextEdit::cursorPositionChanged,this,&noteWindow::showlabelText);
  205. }
  206. noteWindow::~noteWindow()
  207. {
  208. delete ui;
  209. }
  210. void noteWindow::showlabelText(){
  211. //获得鼠标光标
  212. QTextCursor textCursor = textEdit->textCursor();
  213. //行数
  214. int rowNum = textCursor.blockNumber();
  215. //列数
  216. int colNum = textCursor.columnNumber();
  217. //显示在Label上
  218. labelText->setText(tr("第%1行,第%2列").arg(rowNum+1).arg(colNum+1));
  219. }
  220. void noteWindow::showAbout()
  221. {
  222. //利用对话框
  223. QMessageBox::about(this,"About NotePad","This notepad is improved by Shi Yabin.\n");
  224. }
  225. void noteWindow::slotCut(){
  226. textEdit->cut();
  227. }
  228. void noteWindow::slotCopy(){
  229. textEdit->copy();
  230. }
  231. void noteWindow::slotPaste(){
  232. textEdit->paste();
  233. }
  234. void noteWindow::slotUndo(){
  235. textEdit->undo();
  236. }
  237. bool noteWindow::canBeSaveOrNot(){
  238. //判断文件是否被修改
  239. if(textEdit->document()->isModified()){
  240. QMessageBox::StandardButtons result = QMessageBox::warning(this,"Waring","你想将更改保存吗?",
  241. QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  242. //取消
  243. if(QMessageBox::Cancel == result){
  244. return false;
  245. }
  246. //保存
  247. if(QMessageBox::Save == result){
  248. return slotSave();
  249. }
  250. //不保存
  251. if(QMessageBox::Discard == result){
  252. return true;
  253. }
  254. }
  255. return true;
  256. }
  257. bool noteWindow::slotSave(){
  258. if(currentFile.isEmpty()){
  259. return slotSaveAs();
  260. } else{
  261. return saveFile(currentFile);
  262. }
  263. }
  264. bool noteWindow::slotSaveAs(){
  265. QString fileName =QFileDialog::getSaveFileName(this,tr("Save As"),
  266. "/home",tr("Text Files (*.txt)"));
  267. if(fileName.isEmpty()){
  268. return false;
  269. }else{
  270. return saveFile(fileName);
  271. }
  272. }
  273. bool noteWindow::saveFile(QString fileName){
  274. QFile* file = new QFile(fileName);
  275. if(false == file->open(QFile::WriteOnly | QFile::Text)){
  276. QMessageBox::critical(this,
  277. "critical",
  278. "cannot write file"
  279. );
  280. return false;
  281. }else{
  282. QTextStream out(file);
  283. out<<textEdit->toPlainText();
  284. file->setFileName(fileName);
  285. return true;
  286. }
  287. }
  288. void noteWindow::slotNew(){
  289. if(canBeSaveOrNot()){
  290. textEdit->clear();
  291. setFileName("");
  292. }
  293. }
  294. void noteWindow::setFileName(QString fileName){
  295. currentFile = fileName;
  296. textEdit->document()->setModified(false);
  297. this->setWindowModified(false);
  298. fileName.isEmpty() ? this->setWindowFilePath("new.txt") : this->setWindowFilePath(fileName);
  299. }
  300. void noteWindow::slotOpen(){
  301. if(canBeSaveOrNot()){
  302. QString fileName = QFileDialog::getOpenFileName(this);
  303. if(!fileName.isEmpty()){
  304. loadFile(fileName);
  305. }
  306. }
  307. }
  308. //加载文件
  309. void noteWindow::loadFile(QString fileName){
  310. QFile* file = new QFile(fileName);
  311. if(false == file->open(QFile::ReadOnly | QFile::Text)){
  312. QMessageBox::critical(this,
  313. "critical",
  314. "cannot read file"
  315. );
  316. }else{
  317. QTextStream in(file);
  318. textEdit->setText(in.readAll());
  319. setFileName(fileName);
  320. }
  321. }
  322. void noteWindow::quitApp(){
  323. if(canBeSaveOrNot()){
  324. //关闭所有窗口
  325. qApp->closeAllWindows();
  326. }
  327. }
  328. void noteWindow::closeEvent(QCloseEvent *e){
  329. if(canBeSaveOrNot()){
  330. e->accept();
  331. }else{
  332. e->ignore();//忽略,直接关闭
  333. }
  334. }
  335. void noteWindow::slotNewWin()
  336. {
  337. //创建新的窗口
  338. noteWindow *pNewWin = new noteWindow(this);
  339. pNewWin->show();
  340. }
  341. void noteWindow::slotDelete()
  342. {
  343. //得到当前text的光标
  344. QTextCursor cursor=textEdit->textCursor();
  345. //如果有选中则取消,以免受受影响
  346. if(cursor.hasSelection())
  347. cursor.clearSelection();
  348. //删除前一个字符
  349. cursor.deletePreviousChar();
  350. textEdit->setTextCursor(cursor);
  351. }
  352. void noteWindow::slotTime()
  353. {
  354. QString str;
  355. QDateTime time = QDateTime::currentDateTime();
  356. str = time.toString("yyyy-MM-dd hh:mm:ss");
  357. //qDebug() << str ;
  358. textEdit->append(str);
  359. }
  360. void noteWindow::slotFont()
  361. {
  362. //获取当前用户设置的字体样式
  363. QFont getFont(bool *ok, const QFont &initial, QWidget *parent = Q_NULLPTR, const QString &title = QString());
  364. QFont getFont(bool *ok, QWidget *parent = Q_NULLPTR);
  365. bool bFlag = true;
  366. QFont font = QFontDialog::getFont(&bFlag);
  367. //修改字体
  368. textEdit->setFont(QFont(font.family(), font.pointSize(),font.bold(), font.italic()));
  369. //打印字体信息
  370. qDebug("当前选择的字体是[%s]-是否加粗[%d]-是否倾斜[%d]-字号[%d]", font.family().toUtf8().data(), font.bold(), font.italic(), font.pointSize());
  371. }
  372. void noteWindow::slotShowStatusOrNot()
  373. {
  374. //触发时修改showOrNot状态
  375. if(true == showOrNot){
  376. showOrNot = false;
  377. actionStatus->setText("状态栏");
  378. }else{
  379. showOrNot = true;
  380. actionStatus->setText("• 状态栏");
  381. }
  382. //判定当前状态
  383. if (false == showOrNot){
  384. sBar->removeWidget(labelText);
  385. sBar->removeWidget(labelText2);
  386. sBar->removeWidget(labelText3);
  387. sBar->removeWidget(labelText4);
  388. }else{
  389. //编辑标签labelText
  390. labelText = new QLabel();
  391. labelText->setText("第1行,第1列");
  392. //添加标签labelText2
  393. labelText2 = new QLabel();
  394. labelText2->setText("100% ");
  395. //添加标签labelText3
  396. labelText3 = new QLabel();
  397. labelText3->setText("Windows(CRLF) ");
  398. //添加标签labelText4
  399. labelText4 = new QLabel();
  400. labelText4->setText("UTF-8 ");
  401. sBar->addWidget(labelText);
  402. sBar->addWidget(labelText2);
  403. sBar->addWidget(labelText3);
  404. sBar->addWidget(labelText4);
  405. }
  406. }

notewindow.h

  1. #ifndef NOTEWINDOW_H
  2. #define NOTEWINDOW_H
  3. #include <QMainWindow>
  4. #include <QTextEdit>
  5. #include <QLabel>
  6. QT_BEGIN_NAMESPACE
  7. namespace Ui { class noteWindow; }
  8. QT_END_NAMESPACE
  9. class noteWindow : public QMainWindow
  10. {
  11. Q_OBJECT
  12. public:
  13. //主要文本区
  14. QTextEdit *textEdit=new QTextEdit();
  15. //标签
  16. QLabel *labelText;
  17. QLabel* labelText2;
  18. QLabel* labelText3;
  19. QLabel* labelText4;
  20. //当前文件
  21. QString currentFile;
  22. //状态栏动作
  23. QAction *actionStatus;
  24. //状态栏
  25. QStatusBar *sBar;
  26. //判定
  27. bool showOrNot = true;
  28. noteWindow(QWidget *parent = nullptr);
  29. ~noteWindow();
  30. public slots:
  31. void showlabelText();
  32. void showAbout();
  33. void slotCut();
  34. void slotCopy();
  35. void slotPaste();
  36. void slotUndo();
  37. bool canBeSaveOrNot();
  38. bool slotSave();
  39. bool slotSaveAs();
  40. bool saveFile(QString);
  41. void slotNew();
  42. void setFileName(QString fileName);
  43. void slotOpen();
  44. void loadFile(QString fileName);
  45. void quitApp();
  46. void closeEvent(QCloseEvent *e);
  47. void slotNewWin();
  48. void slotDelete();
  49. void slotTime();
  50. void slotFont();
  51. void slotShowStatusOrNot();
  52. private:
  53. Ui::noteWindow *ui;
  54. };
  55. #endif // NOTEWINDOW_H

notewindow.ui

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>noteWindow</class>
  4. <widget class="QMainWindow" name="noteWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>800</width>
  10. <height>600</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>noteWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralwidget"/>
  17. <widget class="QMenuBar" name="menubar">
  18. <property name="geometry">
  19. <rect>
  20. <x>0</x>
  21. <y>0</y>
  22. <width>800</width>
  23. <height>21</height>
  24. </rect>
  25. </property>
  26. </widget>
  27. <widget class="QStatusBar" name="statusbar"/>
  28. </widget>
  29. <resources/>
  30. <connections/>
  31. </ui>

notewindow.cpp创建主窗体及完成各功能。

总结

本次Qt学习过程中完成了记事本的制作,进一步提高了自己对于知识的掌握程度和应用能力~

项目源码:https://github.com/YourHealer/Qt-Notepad.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/615894
推荐阅读
相关标签
  

闽ICP备14008679号