ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【QT】QT+VS2019基本功能的记事本(附项目资源)

2020-04-03 09:56:18  阅读:316  来源: 互联网

标签:QT VS2019 QAction Notepad deleteLater codec toUnicode new 记事本


链接地址:【QT】QT+VS2019极简的记事本(附项目资源)

目录

一、需求分析

基础功能:

  1. 输入文字并显示
  2. 复制粘贴(一般系统都支持)
  3. 保存到本地(存档功能, 下次还能打开)
  4. 可以打开外部txt文件
  5. 可调节字体大小&颜色
  6. 可打印当前本地时间

二、界面布局

此次记事本的界面设计并没有使用QT Designer,界面的设计布局完全由代码编写,想要简单了解纯代码设计界面的读者,或许可以给你一些启发

在这里插入图片描述

三、实现代码

代码注释的很详细,品尝起来很美味

Notepad.h

#pragma once

#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPlainTextEdit>
#include <QClipboard>

#include <QFileDialog>
#include <QFile>
#include <QFontDialog>
#include <QKeySequence>

#include <QtWidgets>
#include <QtCore>

class Notepad : public QMainWindow
{
	Q_OBJECT

public:
	Notepad(QWidget *parent = Q_NULLPTR);
	~Notepad();

private slots:
	void OpenFile();		//打开文件
	void SaveFile();		//保存文件
	void SetFont();			//设置字体
	void SetColor();		//设置颜色
	void OpenTime();		//时间日期

private:
	QTextCodec* codec;		//处理中文

	QMenuBar* mainMenu;		//主菜单
	QMenu* fileMenu;		//文件菜单
	QAction* openAction;		//打开文件
	QAction* saveAction;		//保存文件

	QMenu* editMenu;		//编辑菜单
	QAction* copyAciton;		//复制
	QAction* pasteAction;		//粘贴

	QMenu* formatMenu;		//格式菜单
	QAction* fontAciton;	//字体
	QAction* colorAction;	//颜色

	QMenu* helpMenu;		//帮助菜单
	QAction* timeAction;	//日期和时间

	QTextEdit* textEdit;	//编辑窗口
	QClipboard* clipboard;		//剪贴板
};

Notepad.cpp

#include "Notepad.h"

Notepad::Notepad(QWidget *parent)
	: QMainWindow(parent)
{
    codec = QTextCodec::codecForName("gbk");

    //设置窗口大小及起始位置
    this->setGeometry(200, 200, 800, 500);
    //设置窗口名称
    this->setWindowTitle(codec->toUnicode("QT记事本"));

    mainMenu = new QMenuBar();
    //文件菜单
    fileMenu = new QMenu(codec->toUnicode("文件"));
    mainMenu->addMenu(fileMenu);
    //打开动作
    openAction = new QAction(codec->toUnicode("打开"));
    openAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
    fileMenu->addAction(openAction);
    //保存动作
    saveAction = new QAction(codec->toUnicode("保存"));
    saveAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
    fileMenu->addAction(saveAction);

    //编辑菜单
    editMenu = new QMenu(codec->toUnicode("编辑"));
    mainMenu->addMenu(editMenu);
    //复制动作
    copyAciton = new QAction(codec->toUnicode("复制"));
    copyAciton->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
    editMenu->addAction(copyAciton);
    //粘贴动作
    pasteAction = new QAction(codec->toUnicode("粘贴"));
    pasteAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_V));
    editMenu->addAction(pasteAction);

    //格式菜单
    formatMenu = new QMenu(codec->toUnicode("格式"));
    mainMenu->addMenu(formatMenu);
    //字体动作
    fontAciton = new QAction(codec->toUnicode("字体"));
    formatMenu->addAction(fontAciton);
    //颜色动作
    colorAction = new QAction(codec->toUnicode("颜色"));
    formatMenu->addAction(colorAction);

    //帮助菜单
    helpMenu = new QMenu(codec->toUnicode("帮助"));
    mainMenu->addMenu(helpMenu);
    //时间日期动作
    timeAction = new QAction(codec->toUnicode("时间日期"));
    helpMenu->addAction(timeAction);

    //主菜单栏添加至窗口
    this->setMenuBar(mainMenu);

    //文本编辑
    textEdit = new QTextEdit();
    //文本编辑器添加至窗口
    this->setCentralWidget(textEdit);

    //链接信号和槽
    connect(openAction, SIGNAL(triggered()), this, SLOT(OpenFile()));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(SaveFile()));
    connect(copyAciton, SIGNAL(triggered()), this, SLOT(copy()));
    connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
    connect(fontAciton, SIGNAL(triggered()), this, SLOT(SetFont()));
    connect(colorAction, SIGNAL(triggered()), this, SLOT(SetColor()));
    connect(timeAction, SIGNAL(triggered()), this, SLOT(OpenTime()));
}

Notepad::~Notepad()
{
    //清除缓存
    mainMenu->deleteLater();
    fileMenu->deleteLater();
    openAction->deleteLater();
    saveAction->deleteLater();
    editMenu->deleteLater();
    copyAciton->deleteLater();
    pasteAction->deleteLater();
    formatMenu->deleteLater();
    fontAciton->deleteLater();
    colorAction->deleteLater();
    helpMenu->deleteLater();
    timeAction->deleteLater();
    textEdit->deleteLater();
}

//打开文件
void Notepad::OpenFile()
{
    //显示文件打开对话框,并将选择的文件的路径存储在filePath
    QString filePath;
    filePath = QFileDialog::getOpenFileName
    (   this, 
        codec->toUnicode("打开文件"), 
        "", 
        tr("All Files (*.txt)")
    );
    qDebug() << "OpenFile(): "<<filePath;

    if (filePath != "")
    {
        QFile file;
        file.setFileName(filePath);
        if (file.open(QFile::ReadOnly))
        {
            //如果文件被打开,将其内容显示在编辑窗口
            textEdit->setPlainText(file.readAll());
            file.close();
        }
    }
}

//保存文件
void Notepad::SaveFile()
{
    //显示文件保存对话框,并将选择的文件的路径存储在filePath
    QString filePath;
    filePath = QFileDialog::getSaveFileName
    (   this,
        codec->toUnicode("保存文件"),
        "",
        tr("All Files (*.txt)")
    );
    qDebug() << "SaveFile(): " << filePath;

    if (filePath != "")
    {
        QFile file;
        file.setFileName(filePath);
        if (file.open(QFile::WriteOnly | QFile::Truncate)) 
        {
            //如果文件被打开,编辑内容
            file.write(textEdit->toPlainText().toUtf8());
            file.close();
        }
    }
}

//设置字体
void Notepad::SetFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, textEdit->font(), this,codec->toUnicode("选择字体"));
    if (ok) 
    {
        // 设置字体
        textEdit->setFont(font);
    }
}

//设置颜色
void Notepad::SetColor()
{
    bool ok;
    QColor color = QColorDialog::getColor(Qt::green, this, codec->toUnicode("选择颜色"));
    if (ok)
    {
        // 设置颜色
        textEdit->setTextColor(color);
    }
}

//时间日期
void Notepad::OpenTime()
{
    // 获得当前时间
    QDateTime current = QDateTime::currentDateTime();
    // 转换时间格式
    QString time = current.toString("yyyy-MM-dd hh:mm:ss");
    // 将时间追加到文本下一行
    textEdit->append(time);
}

结尾

如有不足之处,还望指正 [1]


  1. 如果对您有帮助可以点赞、收藏、关注,将会是我最大的动力 ↩︎

标签:QT,VS2019,QAction,Notepad,deleteLater,codec,toUnicode,new,记事本
来源: https://www.cnblogs.com/CoutCodes/p/12624645.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有