Qt记事本,美化版
2024-09-03 19:45:28
主体代码实现
#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
最新文章
- MD5加密
- BZOJ 1086: [SCOI2005]王室联邦
- 在后台启动受管服务器经常报错:Server may already be running
- 前端CSS参考阅读
- Bootstrap——导航栏编写
- Ubuntu上部署Ghost博客
- POJ 1703 Find them, catch them (并查集)
- github创建tag
- Linux环境进程间通信
- git ignore已经checked in files
- 原生JavaScript的省市县三级联动
- Swift中的ViewController
- Visual Studio 2010 将网站直接发布到远程站点
- WCF/WPF公司内部订餐程序开发
- AJAX多级下拉联动【JSON】
- PowerEdge R430 机架式服务器安装( Ubuntu server 14.04.1 、PHP5.5.9、PHP-redis2.8、Phalcon3.1)
- VS2010与Qt5.1.0集成(非源码方式)
- MySQL:日期类型
- discuz模板介绍
- cratedb nodejs 试用