一.第一阶段

widget.h

widget.cpp

上述程序直接运行:

二.第二阶段

widget.cpp
运行

三.第三阶段

xiaowen_EDIT.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.h# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
class QFile;
class QTextEdit;
class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();
private:void initMenuBar();QFile* _file;QTextEdit* _edit;QString _curPath;
public slots:void newSlot();void openSlot();void saveSlot();bool saveAsSlot();void closeSlot();
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include<QFile>
#include<QFileDialog>
#include<QTextEdit>
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{_file=new QFile(this);_edit=new QTextEdit(this);this->setCentralWidget(_edit);initMenuBar();
}void MainWindow::initMenuBar()
{QMenuBar* menuBar=this->menuBar();QMenu* fileMenu=menuBar->addMenu("文件(&F)");fileMenu->addAction("新建",this,SLOT(newSlot()),QKeySequence("Ctrl+N"));fileMenu->addAction("打开",this,SLOT(openSlot()),QKeySequence("Ctrl+O"));fileMenu->addAction("保存",this,SLOT(saveSlot()),QKeySequence("Ctrl+S"));fileMenu->addAction("另存为",this,SLOT(saveSlot()));fileMenu->addAction("关闭",this,SLOT(closeSlot()),QKeySequence("Ctrl+Q"));
}
void MainWindow::newSlot()
{if(_edit->document()->isModified()){int sb=QMessageBox::warning(this,"是否保存","是否保存",QMessageBox::Save|QMessageBox::No|QMessageBox::Cancel);switch (sb){case QMessageBox::Save:if(_curPath.isEmpty()){if(saveAsSlot())break;}elsesaveSlot();case QMessageBox::No:_edit->clear();_curPath.clear();break;default:break;}}else{_edit->clear();_curPath.clear();}
}
void MainWindow::openSlot()
{QString filename=QFileDialog::getOpenFileName(this,"打开",_curPath,"text(*.txt)");if(QFileInfo(filename).isFile()){_curPath=filename;_file->setFileName(filename);_file->open(QIODevice::ReadOnly);_edit->setText(_file->readAll());_file->close();}
}
void MainWindow::saveSlot()
{if(_curPath.isEmpty())saveAsSlot();_file->open(QIODevice::WriteOnly | QIODevice::Truncate);_file->write(_edit->toPlainText().toUtf8());_file->close();
}
bool MainWindow::saveAsSlot()
{QString filename = QFileDialog::getSaveFileName(this,"另存为",_curPath,"text(*.txt)");if(QFileInfo(filename).isNativePath()){_curPath=filename;_file->setFileName(filename);_file->open(QIODevice::WriteOnly | QIODevice::Truncate);_file->write(_edit->toPlainText().toUtf8());_file->close();}return filename.isEmpty();
}
void MainWindow::closeSlot()
{}
MainWindow::~MainWindow()
{}

main.cpp

#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

运行结果(GIF动图):

QT每日一练day19:设计一个记事本相关推荐

  1. QT每日一练day1:第一个程序

    参考博文:Qt常见类.窗口类继承关系树图 QT每日一练(1):第一个程序 最终效果(GIF动图): step1: step2: step3: step4: step5: step6: step7: s ...

  2. 【每日一练】100—一个渐变色计数器列表的实现

    文 | 杨小爱 写在前面 今天我们迎来了[每日一练]栏目的第100个练习,在这几个月的持续练习中,我不知道,你学到了什么?是持续学习的习惯,还是前端基础知识? 但是,不管怎么样,只要持续的去做去学习自 ...

  3. QT每日一练day4:ubuntu中使用QT

    (本文主要是为了说明QT的跨平台特性)   一.安装QT sudo apt-get install qt5-default qtcreator cmake 二.打开QT 可以点击图标或命令行方式: 三 ...

  4. 【每日一练】108—一个登录页面的实现

    写在前面 今天我们来写一个登录页面,这个在很多企业系统上都会用到,当然,一些网站上也用应用到,所以,它的应用场景还是很多的. 现在,我们一起来看一下它的最终效果: 以下是实现的源码. HTML代码: ...

  5. 【每日一练】134—一个好玩有趣的CSS 动画效果

    今天这个练习是一个用纯CSS实现的动画效果,非常有趣,大家可以看一下它的最终效果: HTML代码: <!doctype html> <html><head>< ...

  6. python设计一个动物类_【Python】每日一练:设计圆类计算周长和面积、设计动物类...

    编程题 1.设计一个 Circle(圆)类,包括半径和颜色属性,编写构造方法和其他方法,计算圆的周长和面积.请编写程序验证类的功能. 2.设计一个 Animal(动物)类,包括颜色属性和叫方法.再设计 ...

  7. python设计一个动物类_「Python」每日一练:设计圆类计算周长和面积、设计动物类...

    编程题 1.设计一个 Circle(圆)类,包括半径和颜色属性,编写构造方法和其他方法,计算圆的周长和面积.请编写程序验证类的功能. 2.设计一个 Animal(动物)类,包括颜色属性和叫方法.再设计 ...

  8. 【每日一练】146—一个漂亮的旅游网站模板

    作者 | 杨小爱 写在前面 今天这个练习,分享一个旅游网站模板案例,大家可以根据这个案例进行自我练习,我把源码放在文章后面了,根据自己需要自行下载获取即可. 以下是网站的演示效果: 源代码获取地址: ...

  9. QT每日一练day10:设计一个登陆界面

    目标 创建项目文件 下列代码编译运行: 继续添加代码: 在确认左边添加空白 设置英文显示为圆黑点显示 省略一些步骤描述 最终效果: work.pro widget.h widget.cpp main. ...

最新文章

  1. 大规模集群自动化部署SSH无密码登陆
  2. 新時代的開端:DELPHI.NET- 語言篇
  3. susan算子的运用
  4. easyScholar——文献数据库插件
  5. 概率编程编程_概率编程语言的温和介绍
  6. 前端手动封装数组的foreach,map,filter,every,some,Reduce,reduceRight方法
  7. 迁移到 Centos 7 遇到的一些常见问题
  8. Mysql分区表的使用
  9. Mendix的Hybrid App本地开发最佳实践
  10. 数据结构与算法 计算表达式(一)
  11. java生成卡号_java 生成银行卡号
  12. 家电哥回馈社会 心系老人忙公益
  13. fft算法c语言复数结果是啥,算法-为什么FFT产生复数而不是实数?
  14. 如何让TCP重传如丝般柔滑
  15. ASP中Split分割字符串函数的实例用法
  16. 8.认识robots.txt到爬取信息
  17. java游戏回转贝贝龙2下载,崩坏3:暴雨将至最后的剧情,为何贝贝龙拼死保护琪亚娜...
  18. markdown-Emoji
  19. hp 打印机更改 wifi direct 的密码
  20. ThinkPHP,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的开源轻量级PHP框架。

热门文章

  1. 采用Spring实现AOP功能
  2. 基于JAVA+SpringMVC+MYSQL的苗木销售系统
  3. 学生管理系统php网站,学生信息管理系统 网站之modify.php
  4. Fiddler-学习笔记-远程抓包
  5. python 面向对象 析构方法
  6. 第四周笔记 c++ Boolan
  7. 老子《道德经》第六章
  8. 我的软考之路(六)——数据结构与算法(4)之八大排序
  9. thinking of 抵制家乐福
  10. 东北林大计算机考研难吗,北京林业大学考研难吗?一般要什么水平才可以进入?...