QT记事本+登陆界面的简单实现

主体头文件

#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 // 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 clicked
                this->save_slot();
                break;
            case QMessageBox::Discard:
                // Don't Save was clicked
                event->ignore();
                break;
            case QMessageBox::Cancel:
                // Cancel was clicked
                event->accept();
                break;
            default:
                // should never be reached
                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);
       //UTF-8
       stream.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-8
        stream.setCodec("UTF-8");

        //QString str = this->edit->toPlainText();
         QString str = this->edit->toHtml();
        stream<<str;
        //关闭文件
        file->close();
        delete file;

        //清空edit
        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 // 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绘制

QT记事本+登陆界面的简单实现_第1张图片

版本展示头文件

#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 // 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绘制

QT记事本+登陆界面的简单实现_第2张图片

效果图

QT记事本+登陆界面的简单实现_第3张图片
QT记事本+登陆界面的简单实现_第4张图片

你可能感兴趣的:(qt)