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

news/2024/12/2 21:32:04/

主体头文件

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

在这里插入图片描述

效果图

在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/1116622.html

相关文章

【JavaEE】多线程案例-单例模式

文章目录 1. 前言2. 什么是单例模式3. 如何实现单例模式3.1 饿汉模式3.2 懒汉模式4. 解决单例模式中遇到的线程安全问题4.1 加锁4.2 加上一个判断解决频繁加锁问题4.2 解决因指令重排序造成的线程不安全问题 1. 前言 单例模式是我们面试中最常考到的设计模式。什么是设计模式呢…

开发环境_Linux

环境搭建 文章目录 环境搭建[toc]DockerDocker运行权限Docker加速Docker容器创建 Python版本切换版本工具RepoGit 开发SDK代码拉取在线离线(推荐) Debian安装软件包编译打包 问题技巧 Docker sudo apt install docker.ioDocker运行权限 #添加docker group sudo groupadd doc…

c语言进阶部分详解(指针进阶1)

大家好&#xff01;指针的初阶内容我已经写好&#xff0c;可移步至我的文章&#xff1a;c语言进阶部分详解&#xff08;指针初阶&#xff09;_总之就是非常唔姆的博客-CSDN博客 基本内容我便不再赘述&#xff0c;直接带大家进入进阶内容&#xff1a; 目录 一.字符指针 1.讲解…

深眸科技迭代深度学习算法,以AI机器视觉技术扩围工业应用场景

智能制造是制造业数智化转型升级的发展方向&#xff0c;在当前以高端装备制造为核心的工业4.0时代背景下&#xff0c;越来越多的制造企业意识到机器视觉对于提高效率、降低成本&#xff0c;从而提升企业效益的意义。 目前&#xff0c;机器视觉已成为制造业迈向智能制造过程中极…

Ubuntu修改静态IP、网关和DNS的方法总结

Ubuntu修改静态IP、网关和DNS的方法总结 ubuntu系统&#xff08;其他debian的衍生版本好像也可以&#xff09;修改静态IP有以下几种方法。&#xff08;搜索总结&#xff0c;可能也不太对&#xff09; /etc/netplan (use) Ubuntu 18.04开始可以使用netplan配置网络&#xff0…

设计模式:桥接器模式(C++实现)

桥接器模式&#xff08;Bridge Pattern&#xff09;是一种结构设计模式&#xff0c;它将抽象部分与实现部分分离&#xff0c;使它们可以独立地变化。桥接器模式通常用于需要在多个维度上扩展和变化的情况下&#xff0c;将抽象和实现解耦。 以下是一个简单的C桥接器模式的示例&a…

掷骰子的多线程应用程序2基于互斥量的线程同步(复现《Qt C++6.0》)

说明&#xff1a;在复现过程中出现两点问题&#xff08;1&#xff09;run()函数中对m_diceValued的赋值&#xff08;2&#xff09;do_timeOut()函数中没有对m_seq、m_diceValued进行定义。修改后的复现程序如下所示&#xff1a; 主线程&#xff1a; .h #pragma once#include…

武汉凯迪正大—雷电冲击电压发生器

雷电冲击电压发生器系统特点 1&#xff0e;采用PLC可编程控制器技术&#xff0c;使控制系统实现超小型化及高可靠性能的智能自动控制和测量&#xff1b;配合我公司的数字化测控系统方便的与计算机连接&#xff0c;实现计算机智能自动控制、测量和管理 。 2&#xff0e;输出极…