#include "notepad.h"
#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Notepad)
{
ui->setupUi(this);
// 设置文件图标
setWindowIcon(QIcon(":/jishiben.png"));
// 页面多大文本框就显示多大
this->setCentralWidget(ui->textEdit);
// 设置窗口标题
setWindowTitle("HGLNotePad");
// 页面右下角显示实时时间
timeLabel = new QLabel(this);
ui->statusbar->addPermanentWidget(timeLabel);
QTimer *timer = new QTimer(this); // 创建定时器
connect(timer, &QTimer::timeout, this, &Notepad::showTime); // 定时器与showTime函数连接
timer->start(1000); // 每隔1秒发送一个timeout()信号
// 页面右下角显示编码方式
ui->statusbar->addPermanentWidget(new QLabel("UTF-8", this));
// 页面左下角显示行列号
ui->statusbar->addWidget(statusLabel,0); // 添加到状态栏的最左边
connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, &Notepad::updateCursorPosition);
}
Notepad::~Notepad()
{
delete ui;
}
// 更新光标位置
void Notepad::updateCursorPosition()
{
// 获取光标,textCursor()返回文本光标
QTextCursor cursor = ui->textEdit->textCursor();
int lineNumber = cursor.blockNumber() + 1; // 行号从1开始
int columnNumber = cursor.columnNumber() + 1; // 列号从1开始
statusLabel->setText(QString("行: %1, 列: %2").arg(lineNumber).arg(columnNumber));
}
// 显示时间
void Notepad::showTime()
{
QTime time = QTime::currentTime();
//年月日时分秒
QString text = time.toString("hh:mm:ss");
timeLabel->setText(text);
}
// 新建文件
void Notepad::on_actionNew_triggered()
{
currentFile.clear(); // 清空文件路径
ui->textEdit->setText(QString()); // 清空文本编辑器
}
//新疆一个文件窗口
void Notepad::on_actionNew_Window_triggered()
{
Notepad *newNotepad = new Notepad;
newNotepad->show();
}
// 打开文件
void Notepad::on_actionOpen_triggered()
{
// 打开文件对话框
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
// 如果文件不存在,则弹出警告对话框
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
return;
}
currentFile = fileName;
setWindowTitle(fileName); // 设置窗口标题
QTextStream in(&file);
QString text = in.readAll(); // 读取文件内容
ui->textEdit->setText(text); // 将文件内容显示到文本编辑器中
file.close();
}
// 保存文件
void Notepad::on_actionSave_triggered()
{
QString fileName;
if (currentFile.isEmpty())
{
// 如果文件不存在,则弹出保存文件对话框
fileName = QFileDialog::getSaveFileName(this, "Save");
currentFile = fileName;
} else
{
fileName = currentFile; // 如果文件已经存在,则直接保存
}
// fileName为空时,表示用户取消了保存操作
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QFile::Text))
{
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
// 退出程序
void Notepad::on_actionExit_triggered()
{
QApplication::quit();
}
// 复制
void Notepad::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
// 剪切
void Notepad::on_actionCut_triggered()
{
ui->textEdit->cut();
}
// 粘贴
void Notepad::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
// 撤销
void Notepad::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
// 重做
void Notepad::on_actionRedo_triggered()
{
ui->textEdit->redo();//快捷键ctrl+y
}
// 字体颜色
void Notepad::on_actionColor_triggered()
{
// 获取颜色
QColor color = QColorDialog::getColor(Qt::black, this, "Choose Color");
if (color.isValid())
{
ui->textEdit->setTextColor(color);
}
else return;
}
// 字体
void Notepad::on_actionFont_triggered()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, this); // 获取字体
if (ok)
{
ui->textEdit->setFont(font);
} else return;
}
// 关于
void Notepad::on_actionAbout_triggered()
{
QString aboutText;
aboutText = "Author: HuangGL\n";
aboutText += "Date: 2023-11-24\n";
aboutText += "(C) MyNotepad 2023 (HGL)";
QMessageBox::about(this, "About MyNotePad", aboutText);
}
// 查找
void Notepad::on_actionFind_triggered()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Find Text"), tr("Text to find:"), QLineEdit::Normal, "", &ok);
if (ok && !text.isEmpty())
{
QTextCursor cursor = ui->textEdit->document()->find(text, ui->textEdit->textCursor());
if (!cursor.isNull())
{
ui->textEdit->setTextCursor(cursor);
} else
{
QMessageBox::information(this, tr("Find"), tr("The following text was not found:\n%1").arg(text));
}
}
}
// 关闭事件
void Notepad::closeEvent(QCloseEvent *event)
{
if (true)
{ //当文档被修改时设为true。
QMessageBox::StandardButton resBtn = QMessageBox::question( this, "AppName",
tr("你的文档已经被修改。\n你想在关闭之前保存吗?\n"),
QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resBtn == QMessageBox::Yes)
{
//保存文档
on_actionSave_triggered();
event->accept();
}else if (resBtn == QMessageBox::No)
{
//关闭文档
event->accept();
}else
{
//不关闭文档
event->ignore();
}
}else
{
event->accept();
}
}
// 删除选中的文本
void Notepad::on_actionDelete_triggered()
{
ui->textEdit->textCursor().removeSelectedText();
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于Qt 5.15.2 写的记事本
共8个文件
cpp:3个
h:2个
pro:1个
需积分: 0 1 下载量 176 浏览量
2024-02-22
19:02:05
上传
评论
收藏 6KB 7Z 举报
温馨提示
这个主要是用文本框实现的,可以打开,保存,快捷键等操作,可以自己根据需要加一些小功能
资源推荐
资源详情
资源评论
收起资源包目录
NotePad.7z (8个子文件)
NotePad
notepad.ui 4KB
NotePad.pro.user 19KB
finddialog.cpp 99B
main.cpp 177B
notepad.cpp 6KB
notepad.h 2KB
finddialog.h 281B
NotePad.pro 643B
共 8 条
- 1
资源评论
落叶未闻知秋声~
- 粉丝: 90
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功