1 Star 0 Fork 0

朱洪君/QT-MultiTextEdit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mytextedit.cpp 4.07 KB
一键复制 编辑 原始数据 按行查看 历史
朱洪君 提交于 2023-12-14 15:21 . beta 1.0 finished
#include "mytextedit.h"
#include <QCloseEvent>
#include <QDir>
#include <QEvent>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
MyTextEdit::MyTextEdit(QWidget *parent, QString filePath, bool isExisted) : QTextEdit(parent)
{
isSaved = true;
setCurPath(filePath);
setWindowTitle(fileName);
if(isExisted) open(filePath);
connect(this, &MyTextEdit::textChanged, this, &MyTextEdit::isModified);
}
bool MyTextEdit::save()
{
QString outputPath = getFilePath();
QFile file(outputPath);
if(!file.open(QFile::WriteOnly | QFile::NewOnly)){
int result = QMessageBox::question(this, "文件已存在", "是否覆盖", QMessageBox::Yes, QMessageBox::No);
if(result == QMessageBox::No){
return false;
}
file.open(QFile::WriteOnly);
}
QTextStream stream(&file);
stream << this->toPlainText();
file.close();
isSaved = true;
setWindowTitle(fileName);
return true;
}
bool MyTextEdit::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("另存为"), getCurPath(),
tr("Text files (*.txt)"));
qDebug() << "要保存的文件" << fileName;
if(!fileName.isEmpty()){
setCurPath(fileName);
if(save()){
return true;
}
}
return false;
}
bool MyTextEdit::open(QString filePath)
{
QString inputPath = getFilePath();
QFile file(inputPath);
if(!file.open(QFile::ReadOnly | QFile::ExistingOnly)){
QMessageBox::warning(this, "打开出错", "文件不存在!");
return false;
}
QTextStream instream(&file);
QString text = instream.readAll();
file.close();
qDebug() << text;
setText(text);
setWindowTitle(fileName);
}
QString MyTextEdit::getFileName()
{
return this->fileName;
}
void MyTextEdit::setFileName(QString newName)
{
this->fileName = newName;
isModified();
}
QString MyTextEdit::getCurPath()
{
return this->curPath;
}
void MyTextEdit::setCurPath(QString newPath)
{
QList<QString> splits = newPath.split('/');
if(splits.length() > 0){
setFileName(splits[splits.length() - 1]);
curPath = newPath.left(newPath.length() - splits[splits.length() - 1].length());
}
}
void MyTextEdit::isModified()
{
setWindowTitle(getFileName().append("*"));
isSaved = false;
}
QString MyTextEdit::getFilePath()
{
QString path;
// 拼凑路径
if(curPath.last(1) == '/'){
path = curPath + fileName;
}else{
path = curPath + '/' + fileName;
}
return path;
}
void MyTextEdit::closeEvent(QCloseEvent *event)
{
if(isSaved){
event->accept();
}else{
int result = QMessageBox::information(this, "文件未保存", tr("文件%1尚未保存,是否保存到本地").arg(fileName), QMessageBox::Yes, QMessageBox::No);
if(result == QMessageBox::Yes){
if(saveAs()){
event->accept();
}else{
event->ignore();
}
}else{
event->accept();
}
}
}
void MyTextEdit::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu(this);
QAction *undo = menu->addAction("撤销(&U)");
connect(undo, &QAction::triggered, this, &MyTextEdit::undo);
QAction *redo = menu->addAction("恢复(&R)");
connect(redo, &QAction::triggered, this, &MyTextEdit::redo);
menu->addSeparator();
QAction *cut = menu->addAction("剪切(&T)");
connect(cut, &QAction::triggered, this, &MyTextEdit::cut);
QAction *copy = menu->addAction("复制(&C)");
connect(copy, &QAction::triggered, this, &MyTextEdit::copy);
QAction *paste = menu->addAction("粘贴(&V)");
connect(paste, &QAction::triggered, this, &MyTextEdit::paste);
menu->addSeparator();
QAction *select = menu->addAction("全选(&A)");
connect(select, &QAction::triggered, this, &MyTextEdit::selectAll);
QAction *clear = menu->addAction("清除");
connect(clear, &QAction::triggered, this, &MyTextEdit::clear);
menu->exec(event->globalPos());
delete menu;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhu_hong_jun/qt-multi-text-edit.git
git@gitee.com:zhu_hong_jun/qt-multi-text-edit.git
zhu_hong_jun
qt-multi-text-edit
QT-MultiTextEdit
master

搜索帮助