1 环境:
系统:windows 10
代码编写运行环境:Qt Creator 4.4.1 (community)
GitHub:https://github.com/zhengcixi/Qt_Demo/tree/master/notepad
2 参考代码
https://www.cnblogs.com/Forever-Kenlen-Ja/p/4985133.html
3 实现效果
主界面如下:
下面依次演示操作各个菜单:
(1)文件菜单
包括:新建、新窗口、打开、保存、另存为、打印、退出等功能。
(2)编辑菜单
包括:撤销、剪切、复制、粘贴、删除、查找、替换、全选等功能。
全部替换的功能还存在bug。
(3)格式菜单
包括:更换字体,自动换行功能。
自动换行功能还没有实现。
(4)查看菜单
实现功能:放大、缩小以及状态栏的查看。
放大和缩小功能使用的是QTextEdit自带的槽函数处理,但是没有效果,不知道为什么。
状态栏功能没有实现,现在状态栏默认是打开的,显示的是文本中的鼠标所在的字符是第几个字符。
(5)帮助菜单栏
实现功能:帮助菜单和关于记事本。
点击帮助菜单,会弹出一个网页并跳转到本博客。
点击关于记事本,会弹出一个消息框。
至此,记事本实现的功能就大致如此了,上述的功能大部分也可以通过快捷键的方式进行访问。
下面简单说说工程的组织结构,及部分代码。
4 工程结构及代码说明
工程包含的文件有:
其中,mainwindow.h和mainwindow.cpp是主窗口的相关文件,finddialog.h和finddialog.cpp是查找对话框的相关文件,replacedialog.h和replacedialog.cpp是替换对话框的相关文件。
先给出各个文件的源代码:
finddialog.h
1 #ifndef FINDDIALOG_H 2 #define FINDDIALOG_H 3 4 #include <QWidget> 5 #include <QLabel> 6 #include <QLineEdit> 7 #include <QPushButton> 8 #include <QRadioButton> 9 #include <QCheckBox> 10 #include <QGridLayout> 11 #include <QHBoxLayout> 12 #include <QGroupBox> 13 #include <QCheckBox> 14 #include <QDialog> 15 16 class FindDialog : public QDialog 17 { 18 Q_OBJECT 19 public: 20 explicit FindDialog(QWidget *parent = nullptr); 21 22 signals: 23 //参数:查找关键字、是否区分大小写、向前或向后查找 24 void findTextDataButtonClickedSignal(QString, bool, bool); 25 26 public slots: 27 void findButtonState(); 28 void findDataButtonClickedState(); 29 30 private: 31 QLineEdit *find_edit; 32 QLabel *find_label, *ignore_label, *next_label, *back_label; 33 QPushButton *find_button; 34 QRadioButton *next_radio; 35 QRadioButton *back_radio; 36 QCheckBox *ignore_flag; 37 }; 38 39 #endif // FINDDIALOG_H