主体代码实现

#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. E开始使用webdriver

    GETTING STARTED WITH WEBDRIVER Selenium supports automation of all the major browsers in the market ...

  2. Linux vi 命令 – 文本编辑器

    vi命令是linux系统字符界面下的最常用的文本编辑器. vi编辑器是所有linux的标准编辑器,用于编辑任何ASCⅡ文本,对于编辑源程序尤其有用.iv编辑器功能非常强大,可以对文本进行创建,查找,替 ...

  3. 帆软报表(finereport)图表操作细节

    图表间之间的组件间隔:body-->属性-->布局-->组件间隔 决策报表背景水印:body-->属性-->水印 仪表盘指针/枢纽/背景颜色:样式-->系列 柱形图 ...

  4. 图计算 on nLive:Nebula 的图计算实践

    本文首发于 Nebula Graph Community 公众号 在 #图计算 on nLive# 直播活动中,来自 Nebula 研发团队的 nebula-plato 维护者郝彤和 nebula-a ...

  5. 为什么使用Mybatis对JDBC进行包装探究

    一.原生JDBC在实际生产中使用存在的影响性能的问题 首先分析使用JDBC的代码: Connection connection = null; PreparedStatement preparedSt ...

  6. Python中编码encode()与解码decode()

    1 print('这是编码'.encode('utf-8')) # 结果 b'\xe8\xbf\x99\xe6\x98\xaf\xe7\xbc\x96\xe7\xa0\x81' 2 print('这是 ...

  7. python-logging日志模块,以及封装实现

    前言 是Python内置的标准模块,主要用于输出运行日志 基本语法 import logging # # logging模块默认设置的日志级别是warning,而debug和info的级别是低于war ...

  8. 密码破解工具Brutus

    实验目的 利用brutus将暴力破解ftp密码 实验原理 brutus将多次尝试ftp密码进行密码爆破 实验内容 利用brutus将暴力破解ftp密码 实验环境描述 1. 学生机与实验室网络直连; 2 ...

  9. 移动BI,移动报表平台

    ​随着大数据时代的到来,随着商业智能衍生出来的移动BI也将处于一片大好的形式中,由于智能手机.移动应用的普及,越来越多的办公软件均已支持了移动办公,这也给移动BI带来了更多的想象,通过一部手机就可以随 ...

  10. 思迈特软件Smartbi:机器学习高深难懂?本文深入浅出给你讲明白!

    人工智能(Artificial Intelligence,缩写为AI)是对人的意识.思维过程进行模拟的一门新学科.如今,人工智能从虚无缥缈的科学幻想变成了现实.计算机科学家们在人工智能的技术核心--机 ...