当前位置: 首页 > news >正文

Qt中实现文件(文本文件)内容对比

假如想实现类似VSCode的文件对比功能的话
在这里插入图片描述

有个库【diff-match-patch】,可以实现类似的功能。
将库下载下来后,我们只要将其源码cpp文件夹下的diff_match_patch.h, diff_match_patch.cpp这两个文件加到我们工程中
在这里插入图片描述

在这里插入图片描述
然后自己利用其提供的功能实现一下即可(使用QTextEdit来显示):

#include "diff_match_patch.h"// 读取文件内容
QString readFile(const QString& filePath) {QFile file(filePath);if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {return QString();}return QString::fromUtf8(file.readAll());
}// 在QTextEdit中显示带颜色标记的差异
void showColoredDiff(QTextEdit* textEdit, const QString& text1, const QString& text2) {diff_match_patch dmp;// 计算差异auto diffs = dmp.diff_main(text1, text2);dmp.diff_cleanupSemantic(diffs);  // 优化差异结果// 准备文本格式QTextCharFormat normalFormat;QTextCharFormat addedFormat;QTextCharFormat removedFormat;addedFormat.setBackground(Qt::green);      // 新增内容绿色背景removedFormat.setBackground(Qt::red);       // 删除内容红色背景removedFormat.setFontStrikeOut(true);       // 删除线// 清空并重置文本编辑器textEdit->clear();QTextCursor cursor(textEdit->document());// 应用差异格式for (const auto& diff : diffs) {qDebug() << diff.toString() << diff.text;switch (diff.operation) {case INSERT:cursor.setCharFormat(addedFormat);cursor.insertText(diff.text);break;case DELETE:{cursor.setCharFormat(removedFormat);QString tmpStr = diff.text;qDebug() << diff.text.endsWith("\n");if(diff.text.endsWith("\n")){tmpStr.replace("\n", "\n ");}cursor.insertText(tmpStr);}break;case EQUAL:cursor.setCharFormat(normalFormat);cursor.insertText(diff.text);break;}}// textEdit->setHtml(dmp.diff_prettyHtml(diffs));
}// 使用示例
void compareFiles(const QString& filePath1, const QString& filePath2, QTextEdit* output) {QString content1 = readFile(filePath1);QString content2 = readFile(filePath2);if (content1.isEmpty() || content2.isEmpty()) {output->setPlainText("Error reading files");return;}showColoredDiff(output, content1, content2);
}// 调用
{QTextEdit edit;compareFiles("main.txt", "main_.txt", &edit);edit.resize(640, 480);edit.show();
}

在这里插入图片描述

这样子就可以体现从a.txt–>b.txt需要发生哪些变化,图上的红色背景表示删除、绿色背景表示增加、白色背景表示不改变。
从他的源码看看
在这里插入图片描述
可以看到,两个文件的内容(字符串),从其中的一个变成另外一个,无非是这三种操作(DELETE, INSERT, EQUAL)的排列组合,
而使用了diff_main之后,便可以得到这些操作的组合:
在这里插入图片描述
然后将这些组合依次组合起来便得到了差异。


参考
【diff-match-patch】

http://www.lryc.cn/news/589992.html

相关文章:

  • 若依框架下前后端分离项目交互流程详解
  • ScratchCard刮刮卡交互元素的实现
  • MR 处于 WIP 状态的WIP是什么
  • Django+Celery 进阶:Celery可视化监控与排错
  • 手撕Spring底层系列之:IOC、AOP
  • hadoop 集群问题处理
  • gem install报错解析
  • mac电脑无法阅读runc源码
  • UE5多人MOBA+GAS 24、创建属性UI(一)
  • 从 “洗澡难” 到 “洗得爽”:便携智能洗浴机如何重塑生活?
  • RK3566-EVB开发板如何新建一个产品分支
  • Jetpack Compose 中 Kotlin 协程的使用
  • 基于Hadoop与LightFM的美妆推荐系统设计与实现
  • Chrome紧急更新,谷歌修复正遭活跃利用的关键零日漏洞
  • iPhone 数据擦除软件评测(最新且全面)
  • 力扣面试150题--建立四叉树
  • 分布式光伏气象站:光伏产业的智慧守护者
  • 秘塔AI搜索的深度研究推出:它的“免费午餐”还能走多远?
  • 分布式弹性故障处理框架——Polly(1)
  • PyCharm(入门篇)
  • Python设计模式深度解析:建造者模式(Builder Pattern)完全指南
  • vivo S30评测:用设计诠释科技,以性能书写情怀
  • Git版本控制完全指南:从入门到精通
  • RoMa: Robust Dense Feature Matching论文精读(逐段解析)
  • 【Call For Paper| EI会议】第五届计算机图形学、人工智能与数据处理国际学术会议 (ICCAID 2025)
  • Weblogic历史漏洞利用
  • 5.Java类与对象
  • SenseGlove力反馈手套:医疗、生产制造、军事模拟与远程机器人控制新革命
  • python基础语法9,用os库实现系统操作并用sys库实现文件操作(简单易上手的python语法教学)
  • 【人工智能99问】损失函数有哪些,如何选择?(6/99)