主体头文件
#ifndef JSB_H
#define JSB_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ipv.h"
#include
#include
#include
#include
#include
#include
#include
class JSB : public QMainWindow
{
Q_OBJECT
public:
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
主体的函数实现
#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:
this->save_slot();
break;
case QMessageBox::Discard:
event->ignore();
break;
case QMessageBox::Cancel:
event->accept();
break;
default:
break;
}
}
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);
stream.setCodec("UTF-8");
this->edit->setText(stream.readAll());
file->close();
delete file;
QTextCursor cursor = this->edit->textCursor();
cursor.movePosition(QTextCursor::End);
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);
stream.setCodec("UTF-8");
QString str = this->edit->toHtml();
stream<<str;
file->close();
delete file;
this->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
namespace Ui {
class Login;
}
class Login : public QWidget
{
Q_OBJECT
public:
explicit Login(QWidget *parent = nullptr);
~Login();
public slots:
void login();
private:
Ui::Login *ui;
};
#endif
登录功能的函数实现
#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
#include
namespace Ui {
class IPV;
}
class IPV : public QWidget
{
Q_OBJECT
public:
explicit IPV(QWidget *parent = nullptr);
~IPV();
private:
Ui::IPV *ui;
};
#endif
版本号功能实现
#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绘制
效果图