主体代码实现

#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. JS 获取JSON返回的时间值转换为通常格式展示

    var date = new Date(parseInt(数据源.slice(6)));   //获取到时间  年月日时分秒 var result = date.getFullYear() + '/' ...

  2. 一站式超全JavaScript数组方法大全

    一站式JavaScript数组方法大全(建议收藏) 方法一览表 详细操作 本人总结了JavaScript中有关数组的几乎所有方法(包含ES6之后新增的),并逐一用代码进行演示使用,希望可以帮助大家! ...

  3. python内置模块之re模块

    内容概要 re模块常用方法 findall search match re模块其他方法 split sub subn compile finditer findall 对无名分组优先展示 re实战之爬 ...

  4. Solution -「LOJ #141」回文子串 ||「模板」双向 PAM

    \(\mathcal{Description}\)   Link.   给定字符串 \(s\),处理 \(q\) 次操作: 在 \(s\) 前添加字符串: 在 \(s\) 后添加字符串: 求 \(s\ ...

  5. MySQL架构原理之体系架构

    MySQL是最流行的关系型数据库软件之一,由于其体量小.速度快.开源免费.简单易用.维护成本低等,在季军架构中易于扩展.高可用等优势,深受开发者和企业的欢迎,在互联网行业广泛使用. 其系统架构如下: ...

  6. jenkins针对不同用户显示不同项目

    网上看了别人写的博客有点头晕 比如:https://www.cnblogs.com/kazihuo/p/9022899.html  典型的权限混乱,te用户可以读re用户的项目,re用户可以读te用户 ...

  7. 记录一次dns劫持及其解决办法

    发现问题 偶然发现家里的私人云盘不能用了,最开始以为是云盘出现了问题,各种修复重启后发现云盘并没有问题.然后又发现电脑无法使用浏览器访问网页(或者加载异常缓慢),但是各种软件又可以正常使用,win+R ...

  8. 【转】int和Integer的区别

    int和Integer的区别: 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的 ...

  9. css文字超出指定行数显示省略号

    display: -webkit-box; overflow: hidden; word-break: break-all; /* break-all(允许在单词内换行.) */ text-overf ...

  10. jQuery下载安装使用教程

    一:下载jQuery 下载链接:jQuery官网 中文文档:jQuery AP中文文档 1.jQuery版本 1.x:兼容IE678,使用最为广泛的,官方只做BUG维护,功能不再新增.因此一般项目来说 ...