主体代码实现

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenu>
#include<QMenuBar>
#include<QToolBar>
#include<QDockWidget>
#include<QTextEdit>
#include<QDialog>
#include<QAction>
#include<QMessageBox>
#include<QWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QIcon>
#include<QString>
#include<QFont>
#include<QDebug>
#include<QFontDialog>
#include<QColorDialog>
#include<QPalette>
#include<QColor>
#include<QMessageBox>
#include<QImage>
#include<QTextDocumentFragment>
#include<QPicture>
#include<QFile> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//背景
edit->setParent(this);
setCentralWidget(edit);
QPalette pl = edit->palette();
setWindowTitle("记事本鬼灭之刃版");
this->resize(1000,800);
setStyleSheet("border-image:url(:/a3.jpg)"); //菜单
QMenuBar *bar=menuBar();
setMenuBar(bar);
bar->addSeparator();
QMenu *m8=bar->addMenu("文件");
QMenu *m1=bar->addMenu("工具");
QMenu *m2=bar->addMenu("字号");
QMenu *m3=bar->addMenu("颜色");
QMenu *m9=bar->addMenu("字体");
QMenu *m4=bar->addMenu("撤回");
QMenu *m5=bar->addMenu("恢复");
QMenu *m7=bar->addMenu("帮助");
//文件
//新建
QAction *file=new QAction(tr("新建"));
file->setShortcut(tr("Ctrl+N"));
m8->addAction(file);
m8->addSeparator();
connect(file,&QAction::triggered,this,&MainWindow::newfile);
//打开
QAction *open=new QAction(tr("打开"));
open->setShortcut(tr("Ctrl+X"));
m8->addAction(open);
m8->addSeparator();
connect(open,&QAction::triggered,this,&MainWindow::open);
//关闭
QAction *close=new QAction(tr("关闭"));
close->setShortcut(tr("Ctrl+L"));
m8->addAction(close);
m8->addSeparator();
connect(close,&QAction::triggered,this,&MainWindow::closefun);
//工具
//复制
QAction *copy=new QAction(tr("复制"));
copy->setShortcut(tr("Ctrl+C"));
m1->addAction(copy);
m1->addSeparator();
connect(copy,&QAction::triggered,this,&MainWindow::copyfun);
//粘贴
QAction *copywrite=new QAction(tr("粘贴"));
copywrite->setShortcut(tr("Ctrl+V"));
m1->addAction(copywrite);
m1->addSeparator();
connect(copywrite,&QAction::triggered,this,&MainWindow::copywritefun);
//另存为
QAction *down=new QAction(tr("另存为"));
down->setShortcut(tr("Ctrl+A"));
m1->addAction(down);
m1->addSeparator();
connect(down,&QAction::triggered,this,&MainWindow::downfun);
//字号
//手写
QAction *a1=new QAction("一号");
m2->addAction(a1);
m2->addSeparator();
connect(a1,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 12px");
});
QAction *a2=new QAction("二号",m2);
m2->addAction(a2);
m2->addSeparator();
connect(a2,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 14px");
});
QAction *a3=new QAction("三号",m2);
m2->addAction(a3);
m2->addSeparator();
connect(a3,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 16px");
});
QAction *a4=new QAction("四号",m2);
m2->addAction(a4);
m2->addSeparator();
connect(a4,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 18px");
});
QAction *a5=new QAction("五号",m2);
m2->addAction(a5);
m2->addSeparator();
connect(a5,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 20px");
});
QAction *a6=new QAction("六号",m2);
m2->addAction(a6);
m2->addSeparator();
connect(a6,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 22px");
});
QAction *a7=new QAction("七号",m2);
m2->addAction(a7);
m2->addSeparator();
connect(a7,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 24px");
});
QAction *a8=new QAction("八号",m2);
m2->addAction(a8);
m2->addSeparator();
connect(a8,&QAction::triggered,[=](){
edit->setStyleSheet("font-size : 26px");
});
//对话框
QAction *a9=new QAction("更多选择");
m2->addAction(a9);
m2->addSeparator();
connect(a9,&QAction::triggered,[=](){
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
edit->setFont(font);
else
qDebug()<<tr("没有选择字体");
} );
//颜色
//手写— 红
QAction *red=new QAction("红色");
m3->addAction(red);
m3->addSeparator();
connect(red,&QAction::triggered,[=](){
edit->setTextColor(QColor(255,0,0));
});
//手写- 绿
QAction *green=new QAction("绿色");
m3->addAction(green);
m3->addSeparator();
connect(green,&QAction::triggered,[=](){
edit->setTextColor(QColor(0,255,0));
});
//手写- 蓝
QAction *blue=new QAction("蓝色",m3);
m3->addAction(blue);
m3->addSeparator();
connect(blue,&QAction::triggered,[=](){
edit->setTextColor(QColor(0,0,255));
});
//对话框
QAction *cmore=new QAction("更多颜色");
m3->addAction(cmore);
connect(cmore,&QAction::triggered,[=](){
QColor color = QColorDialog::getColor();
if(color.isValid()){
edit->setTextColor(color);
}
});
//字体
//手写加粗斜体
QAction *w1=new QAction("加粗");
QAction *w2=new QAction("斜体");
m9->addAction(w1);
m9->addSeparator();
m9->addAction(w2);
m9->addSeparator();
connect(w1,&QAction::triggered,[=]{
edit->setStyleSheet("font-weight: bold;");
});
connect(w2,&QAction::triggered,[=]{
edit->setFontItalic(true);
});
//更换字体
//撤回
QAction *x1=new QAction("撤回");
m4->addAction(x1);
m4->addSeparator();
x1->setShortcut(tr("Ctrl+Z"));
connect(x1,&QAction::triggered,[=](){
edit->undo();
});
//恢复
QAction *x2=new QAction("恢复");
m5->addAction(x2);
m5->addSeparator();
x2->setShortcut(tr("Ctrl+M"));
connect(x2,&QAction::triggered,[=](){
edit->redo();
}); //帮助
QAction *hlep=new QAction("帮助");
m7->addAction(hlep);
hlep->setShortcut(tr("Ctrl+H"));
connect(hlep,&QAction::triggered,[=](){
QDialog dialog;
dialog.setWindowTitle(tr("懒得写帮助,自己用用看吧"));
dialog.exec();
});
} MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newfile(){
edit->clear();
edit->setText(QString());
}
void MainWindow::closefun(){
QMessageBox box;
box.setText("确定要关闭吗?");//用模态对话框来实现。
box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
if(box.exec()==QMessageBox::No){
return;
}
else{
this->close();
}
}
void MainWindow::copyfun(){
edit->copy();
}
void MainWindow::copywritefun(){
edit->paste();
}
void MainWindow::downfun(){
QString fn = QFileDialog::getSaveFileName(this,"另存为");
QFile file(fn);
file.open(QIODevice::Text|QIODevice::WriteOnly);//打开方式只写,并且是Text
QTextStream in(&file);
QString filecontent = edit->toPlainText();//toPlainText读取
in << filecontent;
file.close();
}
void MainWindow::open(){
QString FileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
QFile *file=new QFile;
file->setFileName(FileName);
bool ok=file->open(QIODevice::ReadOnly); //判断是否只读模式打开
if(ok){
QTextStream in(file); //类比c++的io流,使之与file绑定
edit->setText(in.readAll()); //这个用法学c++的时候我还没看过
file->close();//关闭文件和c++一样
delete file;
}
else return;//打不开
}

头文件代码

Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QTextEdit>
#include <QMouseEvent>
#include<QDockWidget>
#include <QPaintEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QString now;
QTextEdit *edit=new QTextEdit;
public slots:
void open();
void copyfun();
void copywritefun();
void downfun();
void newfile();
void closefun();
};
#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. python继承关系中,类属性的修改

    class Grandfather(object): mylist = [] def __init__(self): pass class Father(Grandfather): pass Gran ...

  2. Redis 学习笔记(六)Redis 如何实现消息队列

    一.消息队列 消息队列(Messeage Queue,MQ)是在分布式系统架构中常用的一种中间件技术,从字面表述看,是一个存储消息的队列,所以它一般用于给 MQ 中间的两个组件提供通信服务. 1.1 ...

  3. netty系列之:JVM中的Reference count原来netty中也有

    目录 简介 ByteBuf和ReferenceCounted ByteBuf的基本使用 ByteBuf的回收 ByteBuf的衍生方法 ChannelHandler中的引用计数 内存泄露 总结 简介 ...

  4. 框架3.1--V·P·N简介

    目录 框架3.1-VPN简介 1.晨考 2.昨日问题 3.今日内容 4.vpn的简介 5.VPN的作用 6.VPN的种类 7.介绍OpenVPN 框架3.1-VPN简介 1.晨考 1.画iptable ...

  5. Note -「单位根反演」学习笔记

    \(\mathcal{Preface}\)   单位根反演,顾名思义就是用单位根变换一类式子的形式.有关单位根的基本概念可见我的这篇博客. \(\mathcal{Formula}\)   单位根反演的 ...

  6. 痞子衡嵌入式:介绍i.MXRT定时器PIT的多通道链接模式及其在coremark测试工程里的应用

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT定时器PIT的多通道链接模式及其在coremark测试里的应用. 早在 2018 年 i.MXRT 系列跨界处理器刚推出的时 ...

  7. 微服务从代码到k8s部署应有尽有系列(六、订单服务)

    我们用一个系列来讲解从需求到上线.从代码到k8s部署.从日志到监控等各个方面的微服务完整实践. 整个项目使用了go-zero开发的微服务,基本包含了go-zero以及相关go-zero作者开发的一些中 ...

  8. Linux文件处理三剑客(grep、sed、awk)

    下面所说的是Linux中最重要的三个命令在业界被称为"三剑客",它们是grep.sed.awk. 我们现在知道Linux下一切皆文件,对Linux的操作就是对文件的处理,那么怎么能 ...

  9. CentOS7更新OpenSSH8

    今天使用漏洞扫描工具扫描了一下系统漏洞,发现有一个openssh版本的漏洞.所以本着安全的原则修复一下. 第一时间打开百度搜索相关内容,大多数是编译安装的.每个人的环境和版本也不一样.有一定连不上去的 ...

  10. window 消息传递机制【复杂版本】

    一.消息概述     众人周知,window系统是一个消息驱动的系统, windows操作系统本身有自己的消息队列称做系统消息队列(操作系统队列),消息循环,它捕捉键盘,鼠标的动作生成消息,并将这个消 ...