主体头文件
#ifndef JSB_H
#define JSB_H#include <QMainWindow>
#include <QMenuBar>//菜单栏
#include <QToolBar>//工具栏
#include <QStatusBar>//状态栏
#include <QTextEdit>//文本
#include <QLabel>//标签
#include <QDebug>//打印
#include <QDateTime>//获取当前时间
#include <QDesktopServices>//桌面服务
#include <QUrl>//资源定位
#include "ipv.h"
#include <QFileDialog>//文件对话框
#include <QColorDialog>//颜色对话框
#include <QFontDialog>//字体对话框
#include <QTextStream>//文本流
#include <QMessageBox>
#include <QCloseEvent>
#include <QDebug>
class JSB : public QMainWindow
{Q_OBJECTpublic:JSB(QWidget *parent = nullptr);~JSB();public slots:void open_slot();void new_slot();void save_slot();void exit_slot();void copy_slot();void patse_slot();void cut_slot();void redo_slot();void undo_slot();void time_slot();void color_slot();void font_slot();void about_slot();void web_slot();void ipv_slot();void closeEvent(QCloseEvent *event);private://菜单栏QMenuBar * bar=NULL;//文件菜单QMenu *File = NULL;//打开QAction *Open = NULL;//新建QAction *New = NULL;//保存QAction *Save = NULL;//退出QAction *Exit = NULL;//编辑菜单QMenu *Edit = NULL;//恢复QAction *Redo = NULL;//撤销QAction *Undo = NULL;//复制QAction *Copy = NULL;//粘贴QAction *Patse = NULL;//剪切QAction *Cut = NULL;//工具菜单QMenu * Tool = NULL;//时间QAction *Time = NULL;//格式菜单QMenu * ForMat = NULL;//颜色QAction *Color = NULL;//字体QAction *Font = NULL;//帮助QMenu *Help = NULL;//关于QAction *About = NULL;//官网QAction *Web = NULL;//版本QAction *IPv = NULL;//工具栏QToolBar *ToolBar = NULL;//状态栏QStatusBar *staBar = NULL;//文本QTextEdit *edit = NULL;};
#endif // JSB_H
主体的函数实现
#include "jsb.h"JSB::JSB(QWidget *parent): QMainWindow(parent)
{//设置窗口大小this->resize(600,600);//设置窗口文本this->setWindowTitle("记事本");//设置窗口图标this->setWindowIcon(QIcon(":/image/Jsb.png"));//初始化菜单this->bar = menuBar();//指定父类bar->setParent(this);//将菜单栏放入到窗口setMenuBar(bar);//创建文件菜单this->File = bar->addMenu("文件(&F)");//菜单项this->New = File->addAction("新建");//快捷键this->New->setShortcut(tr("Ctrl+N"));//分隔符this->File->addSeparator();//新建图标this->New->setIcon(QIcon(":/image/New.png"));connect(New,&QAction::triggered,this,&JSB::new_slot);this->Open = File->addAction("打开");//打开快捷键this->Open->setShortcut(tr("Ctrl+O"));//图标this->Open->setIcon(QIcon(":/image/Open.png"));connect(Open,&QAction::triggered,this,&JSB::open_slot);this->Save = File->addAction("保存");this->Save->setShortcut(tr("Ctrl+S"));this->Save->setIcon(QIcon(":/image/Save.png"));connect(Save,&QAction::triggered,this,&JSB::save_slot);//分隔符this->File->addSeparator();this->Exit = File->addAction("退出");this->Exit->setShortcut(tr("Ctrl+Q"));this->Exit->setIcon(QIcon(":/image/Exit.png"));connect(Exit,&QAction::triggered,this,&JSB::exit_slot);//创建编辑菜单this->Edit = bar->addMenu("编辑(&E)");this->Redo = Edit->addAction("恢复");this->Redo->setShortcut(tr("Ctrl+R"));this->Redo->setIcon(QIcon(":/image/Redo.png"));connect(Redo,&QAction::triggered,this,&JSB::redo_slot);this->Undo = Edit->addAction("撤销");this->Undo->setShortcut(tr("Ctrl+U"));this->Undo->setIcon(QIcon(":/image/Undo.png"));//分隔符this->Edit->addSeparator();connect(Undo,&QAction::triggered,this,&JSB::undo_slot);this->Copy = Edit->addAction("复制");this->Copy->setShortcut(tr("Ctrl+C"));this->Copy->setIcon(QIcon(":/image/Copy.png"));connect(Copy,&QAction::triggered,this,&JSB::copy_slot);this->Patse = Edit->addAction("粘贴");this->Patse->setShortcut(tr("Ctrl+V"));this->Patse->setIcon(QIcon(":/image/Paste.png"));connect(Patse,&QAction::triggered,this,&JSB::patse_slot);this->Cut = Edit->addAction("剪切");this->Cut->setShortcut(tr("Ctrl+X"));this->Cut->setIcon(QIcon(":/image/Cut.png"));connect(Cut,&QAction::triggered,this,&JSB::cut_slot);//创建工具菜单this->Tool = bar->addMenu("工具(&T)");this->Time = Tool->addAction("时间");this->Time->setShortcut(tr("Ctrl+A"));this->Time->setIcon(QIcon(":/image/time.png"));connect(Time,&QAction::triggered,this,&JSB::time_slot);//创建格式菜单this->ForMat = bar->addMenu("格式(&M)");this->Color = ForMat->addAction("颜色");this->Color->setShortcut(tr("Ctrl+Alt+C"));this->Color->setIcon(QIcon(":/image/Color (5).png"));connect(Color,&QAction::triggered,this,&JSB::color_slot);this->Font = ForMat->addAction("字体");this->Font->setShortcut(tr("Ctrl+Alt+F"));this->Font->setIcon(QIcon(":/image/Font (2).png"));connect(Font,&QAction::triggered,this,&JSB::font_slot);//创建帮助菜单this->Help = bar->addMenu("帮助(&H)");this->About = Help->addAction("关于");this->About->setShortcut(tr("Ctrl+Shift+A"));this->About->setIcon(QIcon(":/image/About.png"));this->Web = Help->addAction("官网");this->Web->setShortcut(tr("Ctrl+Shift+W"));this->Web->setIcon(QIcon(":/image/web.png"));connect(Web,&QAction::triggered,this,&JSB::web_slot);this->IPv = Help->addAction("版本");this->IPv->setShortcut(tr("Ctrl+Shift+I"));this->IPv->setIcon(QIcon(":/image/Window.png"));connect(IPv,&QAction::triggered,this,&JSB::ipv_slot);//工具栏this->ToolBar = new QToolBar;this->ToolBar->setParent(this);addToolBar(ToolBar);this->ToolBar->setMovable(false);this->ToolBar->addAction(Open);this->ToolBar->addAction(Exit);this->ToolBar->addSeparator();this->ToolBar->addAction(Redo);this->ToolBar->addAction(Undo);this->ToolBar->addSeparator();this->ToolBar->addAction(Color);this->ToolBar->addAction(Font);this->ToolBar->addSeparator();this->ToolBar->addAction(Web);this->ToolBar->addAction(IPv);//状态栏this->staBar = statusBar();staBar->setParent(this);setStatusBar(staBar);//创建标签QLabel *label = new QLabel("UTF-8",this);staBar->addWidget(label);//文本this->edit = new QTextEdit(this);setCentralWidget(edit);//设置默认字体QFont font;font.setFamily("楷体");font.setPixelSize(20);font.setBold(true);this->edit->setFont(font);//设置默认颜色QColor color;color.setRed(100);this->edit->setTextColor(color);}JSB::~JSB()
{
}void JSB::closeEvent(QCloseEvent *event){if(this->edit->document()->isModified()){QMessageBox msgBox;msgBox.setText("内容以改变.");msgBox.setInformativeText("是否保存新内容?");msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);msgBox.setButtonText(QMessageBox::Save,"保存");msgBox.setButtonText(QMessageBox::Discard,"取消");msgBox.setButtonText(QMessageBox::Cancel,"不保存");msgBox.setDefaultButton(QMessageBox::Save);int ret = msgBox.exec();switch (ret) {case QMessageBox::Save:// Save was clickedthis->save_slot();break;case QMessageBox::Discard:// Don't Save was clickedevent->ignore();break;case QMessageBox::Cancel:// Cancel was clickedevent->accept();break;default:// should never be reachedbreak;}}else {event->accept();}}
void JSB::open_slot()
{QString filename = QFileDialog::getOpenFileName(this,"打开",".","*.*");QFile *file = new QFile;//设置打开的路径file->setFileName(filename);//只读bool ok = file->open(QIODevice::ReadOnly);if(ok){//问件指针关联文本流QTextStream stream(file);//UTF-8stream.setCodec("UTF-8");this->edit->setText(stream.readAll());//关闭文件file->close();delete file;//获取当前光标的位置QTextCursor cursor = this->edit->textCursor();//将光标移动到末尾cursor.movePosition(QTextCursor::End);//将设置好的光标放到edit中this->edit->setTextCursor(cursor);}}
void JSB::new_slot()
{JSB *w = new JSB;w->show();this->close();
}
void JSB::save_slot()
{QString filename = QFileDialog::getSaveFileName(this,"保存",".","*.*");QFile *file = new QFile;//设置打开的路径file->setFileName(filename);//只写bool ok = file->open(QIODevice::WriteOnly);if(ok){//文件指针关联文本流QTextStream stream(file);//UTF-8stream.setCodec("UTF-8");//QString str = this->edit->toPlainText();QString str = this->edit->toHtml();stream<<str;//关闭文件file->close();delete file;//清空editthis->edit->clear();}
}
void JSB::exit_slot()
{this->close();
}void JSB::copy_slot()
{this->edit->copy();
}
void JSB::patse_slot()
{this->edit->paste();
}
void JSB::cut_slot()
{this->edit->cut();
}
void JSB::redo_slot()
{this->edit->redo();
}
void JSB::undo_slot()
{this->edit->undo();
}void JSB::time_slot()
{QDateTime Time = QDateTime::currentDateTime();QString str = Time.toString("yyyy-MM-dd hh:mm:ss");this->edit->append(str);
}void JSB::color_slot()
{QColor color = QColorDialog::getColor(Qt::red,this,"颜色");if(color.isValid()){this->edit->setTextColor(color);}
}
void JSB::font_slot()
{bool ok;QFont font = QFontDialog::getFont(&ok,QFont("楷体",18,5,true),this,"字体");if(ok){this->edit->setFont(font);}
}void JSB::about_slot()
{}
void JSB::web_slot()
{QDesktopServices::openUrl(QUrl("https://blog.csdn.net/m0_72847002?spm=1010.2135.3001.5421"));
}
void JSB::ipv_slot()
{IPV * ipv = new IPV;ipv->show();}
登录功能的头文件
#ifndef LOGIN_H
#define LOGIN_H#include <QWidget>namespace Ui {
class Login;
}class Login : public QWidget
{Q_OBJECTpublic:explicit Login(QWidget *parent = nullptr);~Login();
public slots:void login();private:Ui::Login *ui;
};#endif // LOGIN_H
登录功能的函数实现
#include "login.h"
#include "ui_login.h"
#include "jsb.h"
Login::Login(QWidget *parent) :QWidget(parent),ui(new Ui::Login)
{ui->setupUi(this);this->setWindowTitle("登录");this->setWindowIcon(QIcon(":/image/Jsb3.png"));connect(ui->dl,&QPushButton::clicked,this,&Login::login);}Login::~Login()
{delete ui;
}void Login::login()
{if(ui->id->text() == "1" && ui->passwd->text()=="1"){JSB *jsb = new JSB;jsb->show();this->close();}
}
登录功能的UI绘制
版本展示头文件
#ifndef IPV_H
#define IPV_H#include <QWidget>
#include <QMovie>
namespace Ui {
class IPV;
}class IPV : public QWidget
{Q_OBJECTpublic:explicit IPV(QWidget *parent = nullptr);~IPV();private:Ui::IPV *ui;
};#endif // IPV_H
版本号功能实现
#include "ipv.h"
#include "ui_ipv.h"IPV::IPV(QWidget *parent) :QWidget(parent),ui(new Ui::IPV)
{ui->setupUi(this);QMovie *movie = new QMovie(":/image/q.gif");ui->label->setMovie(movie);movie->start();}IPV::~IPV()
{delete ui;
}
版本号界面UI绘制
效果图