├── images ├── cut.png ├── new.png ├── bold.png ├── copy.png ├── create.png ├── exit.png ├── font.png ├── info.png ├── italic.png ├── open.png ├── paste.png ├── pencil.png ├── print.png ├── save.png ├── save_as.png ├── edit_redo.png ├── edit_undo.png └── underline.png ├── main.cpp ├── demo_notepad.pro ├── images.qrc ├── mainwindow.h ├── mainwindow.cpp ├── mainwindow.ui ├── README.md └── demo_notepad.pro.user /images/cut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/cut.png -------------------------------------------------------------------------------- /images/new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/new.png -------------------------------------------------------------------------------- /images/bold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/bold.png -------------------------------------------------------------------------------- /images/copy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/copy.png -------------------------------------------------------------------------------- /images/create.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/create.png -------------------------------------------------------------------------------- /images/exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/exit.png -------------------------------------------------------------------------------- /images/font.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/font.png -------------------------------------------------------------------------------- /images/info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/info.png -------------------------------------------------------------------------------- /images/italic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/italic.png -------------------------------------------------------------------------------- /images/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/open.png -------------------------------------------------------------------------------- /images/paste.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/paste.png -------------------------------------------------------------------------------- /images/pencil.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/pencil.png -------------------------------------------------------------------------------- /images/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/print.png -------------------------------------------------------------------------------- /images/save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/save.png -------------------------------------------------------------------------------- /images/save_as.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/save_as.png -------------------------------------------------------------------------------- /images/edit_redo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/edit_redo.png -------------------------------------------------------------------------------- /images/edit_undo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/edit_undo.png -------------------------------------------------------------------------------- /images/underline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libaroma/course-qt-widget-notepad-demo/HEAD/images/underline.png -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | QApplication a(argc, argv); 8 | MainWindow w; 9 | w.show(); 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /demo_notepad.pro: -------------------------------------------------------------------------------- 1 | QT += core gui 2 | 3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 | 5 | CONFIG += c++11 6 | 7 | qtHaveModule(printsupport): QT+=printsupport 8 | 9 | # You can make your code fail to compile if it uses deprecated APIs. 10 | # In order to do so, uncomment the following line. 11 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 12 | 13 | SOURCES += \ 14 | main.cpp \ 15 | mainwindow.cpp 16 | 17 | HEADERS += \ 18 | mainwindow.h 19 | 20 | FORMS += \ 21 | mainwindow.ui 22 | 23 | # Default rules for deployment. 24 | qnx: target.path = /tmp/$${TARGET}/bin 25 | else: unix:!android: target.path = /opt/$${TARGET}/bin 26 | !isEmpty(target.path): INSTALLS += target 27 | 28 | RESOURCES += \ 29 | images.qrc 30 | -------------------------------------------------------------------------------- /images.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | images/bold.png 4 | images/copy.png 5 | images/create.png 6 | images/cut.png 7 | images/edit_redo.png 8 | images/edit_undo.png 9 | images/exit.png 10 | images/font.png 11 | images/info.png 12 | images/italic.png 13 | images/new.png 14 | images/open.png 15 | images/paste.png 16 | images/pencil.png 17 | images/print.png 18 | images/save.png 19 | images/save_as.png 20 | images/underline.png 21 | 22 | 23 | -------------------------------------------------------------------------------- /mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | QT_BEGIN_NAMESPACE 7 | namespace Ui { class MainWindow; } 8 | QT_END_NAMESPACE 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | MainWindow(QWidget *parent = nullptr); 16 | ~MainWindow(); 17 | 18 | private slots: 19 | void on_new_file_triggered(); 20 | 21 | void on_open_file_triggered(); 22 | 23 | void on_save_file_triggered(); 24 | 25 | void on_save_as_triggered(); 26 | 27 | void on_paste_triggered(); 28 | 29 | void on_cut_triggered(); 30 | 31 | void on_copy_triggered(); 32 | 33 | void on_italics_triggered(bool italics); 34 | 35 | void on_underline_triggered(bool underline); 36 | 37 | void on_bolder_triggered(bool bolder); 38 | 39 | void on_font_triggered(); 40 | 41 | void on_about_triggered(); 42 | 43 | void on_undo_triggered(); 44 | 45 | void on_redo_triggered(); 46 | 47 | void on_exit_triggered(); 48 | 49 | void on_print_triggered(); 50 | 51 | private: 52 | Ui::MainWindow *ui; 53 | QString currentFile; 54 | 55 | }; 56 | #endif // MAINWINDOW_H 57 | -------------------------------------------------------------------------------- /mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include 4 | #include "QFileDialog" 5 | #include "QMessageBox" 6 | #include "QFontDialog" 7 | 8 | #if defined(QT_PRINTSUPPORT_LIB) 9 | #include 10 | #if QT_CONFIG(printer) 11 | #if QT_CONFIG(printdialog) 12 | #include 13 | #endif 14 | #include 15 | #endif 16 | #endif 17 | 18 | 19 | MainWindow::MainWindow(QWidget *parent) 20 | : QMainWindow(parent) 21 | , ui(new Ui::MainWindow) 22 | { 23 | ui->setupUi(this); 24 | this->setCentralWidget(ui->textEdit); 25 | #if !QT_CONFIG(printer) 26 | ui->print->setEnabled(false); 27 | #endif 28 | } 29 | 30 | MainWindow::~MainWindow() 31 | { 32 | delete ui; 33 | } 34 | 35 | //新建文件 36 | void MainWindow::on_new_file_triggered() 37 | { 38 | qDebug()<<"Start Create New File ..."; 39 | currentFile.clear(); 40 | ui->textEdit->setText(""); 41 | } 42 | 43 | //打开文件 44 | void MainWindow::on_open_file_triggered() 45 | { 46 | QString fileName = QFileDialog::getOpenFileName(this,"打开文件"); 47 | QFile file(fileName); 48 | if(!file.open(QIODevice::ReadOnly|QFile::Text)){ 49 | QMessageBox::warning(this,"警告","无法打开此文件:"+file.errorString()); 50 | return; 51 | } 52 | currentFile = fileName; 53 | setWindowTitle(fileName); 54 | QTextStream in(&file); 55 | QString text = in.readAll(); 56 | ui->textEdit->setText(text); 57 | file.close(); 58 | } 59 | 60 | 61 | //保存文件 62 | void MainWindow::on_save_file_triggered() 63 | { 64 | QString fileName; 65 | if(currentFile.isEmpty()){ 66 | fileName =QFileDialog::getSaveFileName(this,"保存文件"); 67 | currentFile =fileName; 68 | }else{ 69 | fileName =currentFile; 70 | } 71 | QFile file(fileName); 72 | if(!file.open(QIODevice::WriteOnly|QFile::Text)){ 73 | QMessageBox::warning(this,"警告","无法保存文件:"+file.errorString()); 74 | return; 75 | } 76 | setWindowTitle(fileName); 77 | QTextStream out(&file); 78 | QString text =ui->textEdit->toHtml(); 79 | out<textEdit->toHtml(); 96 | out<textEdit->copy(); 104 | } 105 | 106 | //粘贴 107 | void MainWindow::on_paste_triggered() 108 | { 109 | ui->textEdit->paste(); 110 | } 111 | 112 | //剪切 113 | void MainWindow::on_cut_triggered() 114 | { 115 | ui->textEdit->cut(); 116 | } 117 | 118 | //斜体 119 | void MainWindow::on_italics_triggered(bool italics) 120 | { 121 | ui->textEdit->setFontItalic(italics); 122 | } 123 | 124 | //下划线 125 | void MainWindow::on_underline_triggered(bool underline) 126 | { 127 | ui->textEdit->setFontUnderline(underline); 128 | } 129 | 130 | //加粗 131 | void MainWindow::on_bolder_triggered(bool bolder) 132 | { 133 | ui->textEdit->setFontWeight(bolder?QFont::Bold:QFont::Normal); 134 | } 135 | 136 | //撤销 137 | void MainWindow::on_undo_triggered() 138 | { 139 | ui->textEdit->undo(); 140 | } 141 | 142 | //取消撤销 143 | void MainWindow::on_redo_triggered() 144 | { 145 | ui->textEdit->redo(); 146 | } 147 | 148 | //字体 149 | void MainWindow::on_font_triggered() 150 | { 151 | bool fontSelected; 152 | QFont font = QFontDialog::getFont(&fontSelected,this); 153 | if(fontSelected){ 154 | ui->textEdit->setFont(font); 155 | } 156 | } 157 | 158 | //打印 159 | void MainWindow::on_print_triggered() 160 | { 161 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) 162 | QPrinter printDev; 163 | #if QT_CONFIG(printdialog) 164 | QPrintDialog dialog(&printDev,this); 165 | if(dialog.exec()==QDialog::Rejected) 166 | return; 167 | #endif 168 | ui->textEdit->print(&printDev); 169 | #endif 170 | } 171 | 172 | //关于 173 | void MainWindow::on_about_triggered() 174 | { 175 | QMessageBox::about(this,"这是我的Notepad!","这是我的Notepad,欢迎学习和使用!"); 176 | } 177 | 178 | 179 | //退出 180 | void MainWindow::on_exit_triggered() 181 | { 182 | QCoreApplication::exit(); 183 | } 184 | 185 | -------------------------------------------------------------------------------- /mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 800 10 | 600 11 | 12 | 13 | 14 | Demo-Notepad 15 | 16 | 17 | 18 | :/images/images/pencil.png:/images/images/pencil.png 19 | 20 | 21 | 22 | 23 | 24 | 13 25 | 10 26 | 131 27 | 111 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 0 36 | 0 37 | 800 38 | 23 39 | 40 | 41 | 42 | 43 | 文件 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 编辑 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 关于 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | toolBar 83 | 84 | 85 | TopToolBarArea 86 | 87 | 88 | false 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | toolBar_2 99 | 100 | 101 | TopToolBarArea 102 | 103 | 104 | false 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | toolBar_3 113 | 114 | 115 | TopToolBarArea 116 | 117 | 118 | false 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | toolBar_4 128 | 129 | 130 | TopToolBarArea 131 | 132 | 133 | false 134 | 135 | 136 | 137 | 138 | 139 | 140 | toolBar_5 141 | 142 | 143 | TopToolBarArea 144 | 145 | 146 | false 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | :/images/images/new.png:/images/images/new.png 155 | 156 | 157 | 新建文件 158 | 159 | 160 | Ctrl+N 161 | 162 | 163 | 164 | 165 | 166 | :/images/images/open.png:/images/images/open.png 167 | 168 | 169 | 打开文件 170 | 171 | 172 | Ctrl+O 173 | 174 | 175 | 176 | 177 | 178 | :/images/images/save.png:/images/images/save.png 179 | 180 | 181 | 保存文件 182 | 183 | 184 | Ctrl+S 185 | 186 | 187 | 188 | 189 | 190 | :/images/images/save_as.png:/images/images/save_as.png 191 | 192 | 193 | 另存为 194 | 195 | 196 | Ctrl+Shift+S 197 | 198 | 199 | 200 | 201 | 202 | :/images/images/paste.png:/images/images/paste.png 203 | 204 | 205 | 粘贴 206 | 207 | 208 | Ctrl+V 209 | 210 | 211 | 212 | 213 | 214 | :/images/images/cut.png:/images/images/cut.png 215 | 216 | 217 | 剪切 218 | 219 | 220 | Ctrl+X 221 | 222 | 223 | 224 | 225 | 226 | :/images/images/copy.png:/images/images/copy.png 227 | 228 | 229 | 复制 230 | 231 | 232 | Ctrl+C 233 | 234 | 235 | 236 | 237 | true 238 | 239 | 240 | 241 | :/images/images/bold.png:/images/images/bold.png 242 | 243 | 244 | 加粗 245 | 246 | 247 | Ctrl+B 248 | 249 | 250 | 251 | 252 | true 253 | 254 | 255 | 256 | :/images/images/italic.png:/images/images/italic.png 257 | 258 | 259 | 斜体 260 | 261 | 262 | Ctrl+I 263 | 264 | 265 | 266 | 267 | true 268 | 269 | 270 | 271 | :/images/images/underline.png:/images/images/underline.png 272 | 273 | 274 | 下划线 275 | 276 | 277 | Ctrl+U 278 | 279 | 280 | 281 | 282 | 283 | :/images/images/font.png:/images/images/font.png 284 | 285 | 286 | 字体 287 | 288 | 289 | Ctrl+Shift+F 290 | 291 | 292 | 293 | 294 | 295 | :/images/images/info.png:/images/images/info.png 296 | 297 | 298 | 关于 299 | 300 | 301 | Ctrl+Shift+I 302 | 303 | 304 | 305 | 306 | 307 | :/images/images/edit_undo.png:/images/images/edit_undo.png 308 | 309 | 310 | 撤回 311 | 312 | 313 | Ctrl+Z 314 | 315 | 316 | 317 | 318 | 319 | :/images/images/edit_redo.png:/images/images/edit_redo.png 320 | 321 | 322 | 取消撤回 323 | 324 | 325 | Ctrl+Shift+Z 326 | 327 | 328 | 329 | 330 | 331 | :/images/images/exit.png:/images/images/exit.png 332 | 333 | 334 | 退出 335 | 336 | 337 | Esc 338 | 339 | 340 | 341 | 342 | 343 | :/images/images/print.png:/images/images/print.png 344 | 345 | 346 | 打印 347 | 348 | 349 | Ctrl+P 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 用Qt开发Windows桌面应用程序,以Notepad为案例 2 | 3 | - Github 项目[【地址】](https://github.com/libaroma/course-qt-widget-notepad-demo) 4 | - 源代码[【下载】](https://files.hyz.cool/files/files/demo_notepad.zip) 5 | ![Qt开发Windows桌面应用程序](https://files.hyz.cool/files/articles/f5b246c38e4bebc0a1c309e10f4120b9.jfif) 6 | 7 | ### 00.项目介绍 8 | 9 | - **视频教程:** [【Qt开发Windows——Notepad】—— 00.项目介绍]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=1&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 10 | 11 | ### 01.安装QT 12 | 13 | - **视频教程:** [【Qt开发Windows——Notepad】—— 01.安装QT]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=2&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 14 | 15 | - **下载地址**:https://download.qt.io/archive/qt/ 16 | 17 | ### 02.创建项目 18 | 19 | - **视频教程:** [【Qt开发Windows——Notepad】—— 02.创建项目]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=3&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 20 | 21 | ### 03.文件结构 22 | 23 | - **视频教程:** [【Qt开发Windows——Notepad】—— 03.文件结构]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=4&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 24 | 25 | ### 04.窗口UI 26 | 27 | - **视频教程:** [【Qt开发Windows——Notepad】—— 04.窗口UI]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=5&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 28 | 29 | ### 05.窗口图标 30 | 31 | - **视频教程:** [【Qt开发Windows——Notepad】—— 05.窗口图标]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=6&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 32 | 33 | 图标文件[【下载】](https://files.hyz.cool/files/files/6262d3781f03e2bca26bcfe13d8f0851.zip)地址 34 | 35 | ### 06.创建菜单 36 | 37 | - **视频教程:** [【Qt开发Windows——Notepad】——06.创建菜单]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=7&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 38 | 39 | ### 07.创建工具栏 40 | 41 | - **视频教程:** [【Qt开发Windows——Notepad】—— 07.创建工具栏]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=8&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 42 | 43 | ### 08.创建文本框 44 | 45 | - **视频教程:** [【Qt开发Windows——Notepad】—— 08.创建文本框]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=9&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 46 | 47 | ### 09.创建槽函数 48 | 49 | - **视频教程:** [【Qt开发Windows——Notepad】—— 09.创建槽函数]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=10&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 50 | 51 | ### 10.编辑槽函数 52 | 53 | - **视频教程:** [【Qt开发Windows——Notepad】—— 10.编辑槽函数]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=11&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 54 | 55 | ### 11.文本框居中 56 | 57 | - **视频教程:** [【Qt开发Windows——Notepad】—— 11.文本框居中]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=12&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 58 | 59 | ```c++ 60 | //mainwindow.cpp 61 | MainWindow::MainWindow(QWidget *parent) 62 | : QMainWindow(parent) 63 | , ui(new Ui::MainWindow) 64 | { 65 | ui->setupUi(this); 66 | this->setCentralWidget(ui->textEdit); 67 | } 68 | ``` 69 | 70 | ### 12.新建文档 71 | 72 | - **视频教程:** [【Qt开发Windows——Notepad】—— 12.新建文档]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=13&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 73 | 74 | ```c++ 75 | //mainwindow.h 76 | 77 | class MainWindow : public QMainWindow 78 | { 79 | Q_OBJECT 80 | 81 | public: 82 | MainWindow(QWidget *parent = nullptr); 83 | ~MainWindow(); 84 | 85 | private: 86 | Ui::MainWindow *ui; 87 | QString currentFile; 88 | 89 | }; 90 | ``` 91 | 92 | ```c++ 93 | //mainwindow.cpp 94 | 95 | //新建文件 96 | void MainWindow::on_new_file_triggered() 97 | { 98 | qDebug()<<"Start Create New File ..."; 99 | currentFile.clear(); 100 | ui->textEdit->setText(""); 101 | } 102 | ``` 103 | 104 | ### 13.打开文档 105 | 106 | - **视频教程:** [【Qt开发Windows——Notepad】—— 13.打开文档]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=14&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 107 | 108 | ```c++ 109 | //mainwindow.cpp 110 | 111 | #include "QFileDialog" 112 | #include "QMessageBox" 113 | 114 | //打开文件 115 | void MainWindow::on_open_file_triggered() 116 | { 117 | QString fileName = QFileDialog::getOpenFileName(this,"打开文件"); 118 | QFile file(fileName); 119 | if(!file.open(QIODevice::ReadOnly|QFile::Text)){ 120 | QMessageBox::warning(this,"警告","无法打开此文件:"+file.errorString()); 121 | return; 122 | } 123 | currentFile = fileName; 124 | setWindowTitle(fileName); 125 | QTextStream in(&file); 126 | QString text = in.readAll(); 127 | ui->textEdit->setText(text); 128 | file.close(); 129 | } 130 | ``` 131 | 132 | ### 14.保存文档 133 | 134 | - **视频教程:** [【Qt开发Windows——Notepad】—— 14.保存文档]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=15&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 135 | 136 | ```c++ 137 | //mainwindow.cpp 138 | 139 | #include "QFileDialog" 140 | #include "QMessageBox" 141 | 142 | //保存文件 143 | void MainWindow::on_save_file_triggered() 144 | { 145 | QString fileName; 146 | if(currentFile.isEmpty()){ 147 | fileName =QFileDialog::getSaveFileName(this,"保存文件"); 148 | currentFile =fileName; 149 | }else{ 150 | fileName =currentFile; 151 | } 152 | QFile file(fileName); 153 | if(!file.open(QIODevice::WriteOnly|QFile::Text)){ 154 | QMessageBox::warning(this,"警告","无法保存文件:"+file.errorString()); 155 | return; 156 | } 157 | setWindowTitle(fileName); 158 | QTextStream out(&file); 159 | QString text =ui->textEdit->toHtml(); 160 | out<textEdit->toHtml(); 189 | out<textEdit->copy(); 205 | } 206 | 207 | //粘贴 208 | void MainWindow::on_paste_triggered() 209 | { 210 | ui->textEdit->paste(); 211 | } 212 | 213 | //剪切 214 | void MainWindow::on_cut_triggered() 215 | { 216 | ui->textEdit->cut(); 217 | } 218 | ``` 219 | 220 | ### 17.粗体斜体下划线 221 | 222 | - **视频教程:** [【Qt开发Windows——Notepad】—— 17.粗体斜体下划线]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=18&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 223 | 224 | ```c++ 225 | //mainwindow.cpp 226 | 227 | //斜体 228 | void MainWindow::on_italics_triggered(bool italics) 229 | { 230 | ui->textEdit->setFontItalic(italics); 231 | } 232 | 233 | //下划线 234 | void MainWindow::on_underline_triggered(bool underline) 235 | { 236 | ui->textEdit->setFontUnderline(underline); 237 | } 238 | 239 | //加粗 240 | void MainWindow::on_bolder_triggered(bool bolder) 241 | { 242 | ui->textEdit->setFontWeight(bolder?QFont::Bold:QFont::Normal); 243 | } 244 | ``` 245 | 246 | ### 18.撤销取消撤销 247 | 248 | - **视频教程:** [【Qt开发Windows——Notepad】—— 18.撤销取消撤销]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=19&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 249 | 250 | ```c++ 251 | //mainwindow.cpp 252 | 253 | //撤销 254 | void MainWindow::on_undo_triggered() 255 | { 256 | ui->textEdit->undo(); 257 | } 258 | 259 | //取消撤销 260 | void MainWindow::on_redo_triggered() 261 | { 262 | ui->textEdit->redo(); 263 | } 264 | ``` 265 | 266 | ### 19.字体 267 | 268 | - **视频教程:** [【Qt开发Windows——Notepad】—— 19.字体]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=20&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 269 | 270 | ```c++ 271 | //mainwindow.cpp 272 | 273 | #include "QFontDialog" 274 | 275 | //字体 276 | void MainWindow::on_font_triggered() 277 | { 278 | bool fontSelected; 279 | QFont font = QFontDialog::getFont(&fontSelected,this); 280 | if(fontSelected){ 281 | ui->textEdit->setFont(font); 282 | } 283 | } 284 | ``` 285 | 286 | ### 20.打印 287 | 288 | - **视频教程:** [【Qt开发Windows——Notepad】—— 20.打印]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=21&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 289 | 290 | ```c++ 291 | //demo_notepad.pro 292 | qtHaveModule(printsupport): QT+=printsupport 293 | 294 | ``` 295 | 296 | ```c++ 297 | //mainwindow.cpp 298 | 299 | #if defined(QT_PRINTSUPPORT_LIB) 300 | #include 301 | #if QT_CONFIG(printer) 302 | #if QT_CONFIG(printdialog) 303 | #include 304 | #endif 305 | #include 306 | #endif 307 | #endif 308 | 309 | MainWindow::MainWindow(QWidget *parent) 310 | : QMainWindow(parent) 311 | , ui(new Ui::MainWindow) 312 | { 313 | #if !QT_CONFIG(printer) 314 | ui->print->setEnabled(false); 315 | #endif 316 | } 317 | 318 | //打印 319 | void MainWindow::on_print_triggered() 320 | { 321 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) 322 | QPrinter printDev; 323 | #if QT_CONFIG(printdialog) 324 | QPrintDialog dialog(&printDev,this); 325 | if(dialog.exec()==QDialog::Rejected) 326 | return; 327 | #endif 328 | ui->textEdit->print(&printDev); 329 | #endif 330 | } 331 | ``` 332 | 333 | ### 21.关于退出 334 | 335 | - **视频教程:** [【Qt开发Windows——Notepad】—— 21.关于退出]( https://www.bilibili.com/video/BV16Y4y1y7iE/?p=22&share_source=copy_web&vd_source=7b61313e652a9e58e9a373dd521bb6d9) 336 | 337 | 338 | ```c++ 339 | //关于 340 | void MainWindow::on_about_triggered() 341 | { 342 | QMessageBox::about(this,"这是我的Notepad!","这是我的Notepad,欢迎学习和使用!"); 343 | } 344 | 345 | 346 | //退出 347 | void MainWindow::on_exit_triggered() 348 | { 349 | QCoreApplication::exit(); 350 | } 351 | ``` 352 | 353 | ### 主要部分代码 354 | 355 | - mainwindow.h 356 | 357 | ```c++ 358 | //mainwindow.h 359 | 360 | #ifndef MAINWINDOW_H 361 | #define MAINWINDOW_H 362 | 363 | #include 364 | 365 | QT_BEGIN_NAMESPACE 366 | namespace Ui { class MainWindow; } 367 | QT_END_NAMESPACE 368 | 369 | class MainWindow : public QMainWindow 370 | { 371 | Q_OBJECT 372 | 373 | public: 374 | MainWindow(QWidget *parent = nullptr); 375 | ~MainWindow(); 376 | 377 | 378 | private slots: 379 | void on_new_file_triggered(); 380 | 381 | void on_open_file_triggered(); 382 | 383 | void on_save_file_triggered(); 384 | 385 | void on_save_as_triggered(); 386 | 387 | void on_paste_triggered(); 388 | 389 | void on_cut_triggered(); 390 | 391 | void on_copy_triggered(); 392 | 393 | void on_italics_triggered(bool italics); 394 | 395 | void on_underline_triggered(bool underline); 396 | 397 | void on_bolder_triggered(bool bolder); 398 | 399 | void on_font_triggered(); 400 | 401 | void on_about_triggered(); 402 | 403 | void on_undo_triggered(); 404 | 405 | void on_redo_triggered(); 406 | 407 | void on_exit_triggered(); 408 | 409 | void on_print_triggered(); 410 | 411 | void on_clear_history_triggered(); 412 | 413 | private: 414 | Ui::MainWindow *ui; 415 | QString currentFile; 416 | 417 | }; 418 | #endif // MAINWINDOW_H 419 | ``` 420 | 421 | - mainwindow.cpp 422 | 423 | ```c++ 424 | //mainwindow.cpp 425 | 426 | #include "mainwindow.h" 427 | #include "ui_mainwindow.h" 428 | #include 429 | #include "QFileDialog" 430 | #include "QMessageBox" 431 | #include "QFontDialog" 432 | 433 | #if defined(QT_PRINTSUPPORT_LIB) 434 | #include 435 | #if QT_CONFIG(printer) 436 | #if QT_CONFIG(printdialog) 437 | #include 438 | #endif 439 | #include 440 | #endif 441 | #endif 442 | 443 | 444 | MainWindow::MainWindow(QWidget *parent) 445 | : QMainWindow(parent) 446 | , ui(new Ui::MainWindow) 447 | { 448 | ui->setupUi(this); 449 | this->setCentralWidget(ui->textEdit); 450 | 451 | #if !QT_CONFIG(printer) 452 | ui->print->setEnabled(false); 453 | #endif 454 | } 455 | 456 | MainWindow::~MainWindow() 457 | { 458 | delete ui; 459 | } 460 | 461 | //新建文件 462 | void MainWindow::on_new_file_triggered() 463 | { 464 | qDebug()<<"Start Create New File ..."; 465 | currentFile.clear(); 466 | ui->textEdit->setText(""); 467 | } 468 | 469 | //打开文件 470 | void MainWindow::on_open_file_triggered() 471 | { 472 | QString fileName = QFileDialog::getOpenFileName(this,"打开文件"); 473 | QFile file(fileName); 474 | if(!file.open(QIODevice::ReadOnly|QFile::Text)){ 475 | QMessageBox::warning(this,"警告","无法打开此文件:"+file.errorString()); 476 | return; 477 | } 478 | currentFile = fileName; 479 | setWindowTitle(fileName); 480 | QTextStream in(&file); 481 | QString text = in.readAll(); 482 | ui->textEdit->setText(text); 483 | file.close(); 484 | } 485 | 486 | 487 | //保存文件 488 | void MainWindow::on_save_file_triggered() 489 | { 490 | QString fileName; 491 | if(currentFile.isEmpty()){ 492 | fileName =QFileDialog::getSaveFileName(this,"保存文件"); 493 | currentFile =fileName; 494 | }else{ 495 | fileName =currentFile; 496 | } 497 | QFile file(fileName); 498 | if(!file.open(QIODevice::WriteOnly|QFile::Text)){ 499 | QMessageBox::warning(this,"警告","无法保存文件:"+file.errorString()); 500 | return; 501 | } 502 | setWindowTitle(fileName); 503 | QTextStream out(&file); 504 | QString text =ui->textEdit->toHtml(); 505 | out<textEdit->toHtml(); 522 | out<textEdit->copy(); 530 | } 531 | 532 | //粘贴 533 | void MainWindow::on_paste_triggered() 534 | { 535 | ui->textEdit->paste(); 536 | } 537 | 538 | //剪切 539 | void MainWindow::on_cut_triggered() 540 | { 541 | ui->textEdit->cut(); 542 | } 543 | 544 | //斜体 545 | void MainWindow::on_italics_triggered(bool italics) 546 | { 547 | ui->textEdit->setFontItalic(italics); 548 | } 549 | 550 | //下划线 551 | void MainWindow::on_underline_triggered(bool underline) 552 | { 553 | ui->textEdit->setFontUnderline(underline); 554 | } 555 | 556 | //加粗 557 | void MainWindow::on_bolder_triggered(bool bolder) 558 | { 559 | ui->textEdit->setFontWeight(bolder?QFont::Bold:QFont::Normal); 560 | } 561 | 562 | //撤销 563 | void MainWindow::on_undo_triggered() 564 | { 565 | ui->textEdit->undo(); 566 | } 567 | 568 | //取消撤销 569 | void MainWindow::on_redo_triggered() 570 | { 571 | ui->textEdit->redo(); 572 | } 573 | 574 | //字体 575 | void MainWindow::on_font_triggered() 576 | { 577 | bool fontSelected; 578 | QFont font = QFontDialog::getFont(&fontSelected,this); 579 | if(fontSelected){ 580 | ui->textEdit->setFont(font); 581 | } 582 | } 583 | 584 | //打印 585 | void MainWindow::on_print_triggered() 586 | { 587 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) 588 | QPrinter printDev; 589 | #if QT_CONFIG(printdialog) 590 | QPrintDialog dialog(&printDev,this); 591 | if(dialog.exec()==QDialog::Rejected) 592 | return; 593 | #endif 594 | ui->textEdit->print(&printDev); 595 | #endif 596 | } 597 | 598 | //关于 599 | void MainWindow::on_about_triggered() 600 | { 601 | QMessageBox::about(this,"这是我的Notepad!","这是我的Notepad,欢迎学习和使用!"); 602 | } 603 | 604 | 605 | //退出 606 | void MainWindow::on_exit_triggered() 607 | { 608 | QCoreApplication::exit(); 609 | } 610 | 611 | ``` 612 | 613 | ### 资料获取 614 | 615 | - 图标文件[【下载】](https://files.hyz.cool/files/files/6262d3781f03e2bca26bcfe13d8f0851.zip)地址 616 | - Github 项目[【地址】](https://github.com/libaroma/course-qt-widget-notepad-demo) 617 | -------------------------------------------------------------------------------- /demo_notepad.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {09dcd219-4e68-4927-869c-908263b10b6b} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | false 41 | true 42 | false 43 | 0 44 | true 45 | true 46 | 0 47 | 8 48 | true 49 | false 50 | 1 51 | true 52 | true 53 | true 54 | *.md, *.MD, Makefile 55 | false 56 | true 57 | 58 | 59 | 60 | ProjectExplorer.Project.PluginSettings 61 | 62 | 63 | true 64 | false 65 | true 66 | true 67 | true 68 | true 69 | 70 | 71 | 0 72 | true 73 | 74 | -fno-delayed-template-parsing 75 | 76 | true 77 | Builtin.BuildSystem 78 | 79 | true 80 | true 81 | Builtin.DefaultTidyAndClazy 82 | 4 83 | 84 | 85 | 86 | true 87 | 88 | 89 | 90 | 91 | ProjectExplorer.Project.Target.0 92 | 93 | Desktop 94 | Desktop Qt 5.12.12 MinGW 64-bit 95 | Desktop Qt 5.12.12 MinGW 64-bit 96 | qt.qt5.51212.win64_mingw73_kit 97 | 0 98 | 0 99 | 0 100 | 101 | 0 102 | E:\Mirror\Projects\Qt\course\build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Debug 103 | E:/Mirror/Projects/Qt/course/build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Debug 104 | 105 | 106 | true 107 | QtProjectManager.QMakeBuildStep 108 | false 109 | 110 | 111 | 112 | true 113 | Qt4ProjectManager.MakeStep 114 | 115 | 2 116 | Build 117 | Build 118 | ProjectExplorer.BuildSteps.Build 119 | 120 | 121 | 122 | true 123 | Qt4ProjectManager.MakeStep 124 | clean 125 | 126 | 1 127 | Clean 128 | Clean 129 | ProjectExplorer.BuildSteps.Clean 130 | 131 | 2 132 | false 133 | 134 | 135 | Debug 136 | Qt4ProjectManager.Qt4BuildConfiguration 137 | 2 138 | 139 | 140 | E:\Mirror\Projects\Qt\course\build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Release 141 | E:/Mirror/Projects/Qt/course/build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Release 142 | 143 | 144 | true 145 | QtProjectManager.QMakeBuildStep 146 | false 147 | 148 | 149 | 150 | true 151 | Qt4ProjectManager.MakeStep 152 | 153 | 2 154 | Build 155 | Build 156 | ProjectExplorer.BuildSteps.Build 157 | 158 | 159 | 160 | true 161 | Qt4ProjectManager.MakeStep 162 | clean 163 | 164 | 1 165 | Clean 166 | Clean 167 | ProjectExplorer.BuildSteps.Clean 168 | 169 | 2 170 | false 171 | 172 | 173 | Release 174 | Qt4ProjectManager.Qt4BuildConfiguration 175 | 0 176 | 0 177 | 178 | 179 | 0 180 | E:\Mirror\Projects\Qt\course\build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Profile 181 | E:/Mirror/Projects/Qt/course/build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Profile 182 | 183 | 184 | true 185 | QtProjectManager.QMakeBuildStep 186 | false 187 | 188 | 189 | 190 | true 191 | Qt4ProjectManager.MakeStep 192 | 193 | 2 194 | Build 195 | Build 196 | ProjectExplorer.BuildSteps.Build 197 | 198 | 199 | 200 | true 201 | Qt4ProjectManager.MakeStep 202 | clean 203 | 204 | 1 205 | Clean 206 | Clean 207 | ProjectExplorer.BuildSteps.Clean 208 | 209 | 2 210 | false 211 | 212 | 213 | Profile 214 | Qt4ProjectManager.Qt4BuildConfiguration 215 | 0 216 | 0 217 | 0 218 | 219 | 3 220 | 221 | 222 | 0 223 | Deploy 224 | Deploy 225 | ProjectExplorer.BuildSteps.Deploy 226 | 227 | 1 228 | 229 | false 230 | ProjectExplorer.DefaultDeployConfiguration 231 | 232 | 1 233 | 234 | true 235 | true 236 | true 237 | 238 | 2 239 | 240 | Qt4ProjectManager.Qt4RunConfiguration:E:/Mirror/Projects/Qt/course/demo_notepad/demo_notepad.pro 241 | E:/Mirror/Projects/Qt/course/demo_notepad/demo_notepad.pro 242 | false 243 | true 244 | true 245 | false 246 | true 247 | E:/Mirror/Projects/Qt/course/build-demo_notepad-Desktop_Qt_5_12_12_MinGW_64_bit-Debug 248 | 249 | 1 250 | 251 | 252 | 253 | ProjectExplorer.Project.TargetCount 254 | 1 255 | 256 | 257 | ProjectExplorer.Project.Updater.FileVersion 258 | 22 259 | 260 | 261 | Version 262 | 22 263 | 264 | 265 | --------------------------------------------------------------------------------