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

文本搜索程序(Qt)

头文件

#ifndef TEXTFINDER_H
#define TEXTFINDER_H#include <QWidget>
#include <QFileDialog>
#include <QFile>
#include <QTextEdit>
#include <QLineEdit>
#include <QTextStream>
#include <QPushButton>
#include <QMessageBox>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui {
class textFinder;
}
QT_END_NAMESPACEclass textFinder : public QWidget
{Q_OBJECTpublic:textFinder(QWidget *parent = nullptr);~textFinder();public slots:void lodeTextFile();private:Ui::textFinder *ui;void searchText();bool search_flog;
};
#endif // TEXTFINDER_H

cpp文件

#include "textfinder.h"
#include "./ui_textfinder.h"textFinder::textFinder(QWidget *parent): QWidget(parent), ui(new Ui::textFinder)
{ui->setupUi(this);ui->textEdit->setReadOnly(true);connect(ui->loadFile,&QPushButton::clicked,[=](){lodeTextFile();});connect(ui->search,&QPushButton::clicked,[=](){searchText();});
}textFinder::~textFinder()
{delete ui;
}void textFinder::lodeTextFile()//加载文本文件
{QString path = QFileDialog::getOpenFileName();if(!path.isEmpty()){QFile file(path);if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){QMessageBox::warning(this, tr("Read Text File"),tr("Cannot open file:\n%1").arg(path));return;}QTextStream in(&file);ui->textEdit->setText(in.readAll());//读取文本file.close();}else{QMessageBox::warning(this, tr("Path"),tr("You did not select any file."));}}void textFinder::searchText()//查找文本
{if(ui->textEdit->document()->isEmpty() || ui->lineEdit->text().isEmpty()){QMessageBox::warning(this, tr("Warning"), "The text or searchline is empty!");return;}QString searchText = ui->lineEdit->text();QTextDocument* document = ui->textEdit->document();bool foundWholeWord = false;//查找标志位QTextCursor highlightCursor(document);//文本光标QTextCursor cursor(document);ui->textEdit->document()->undo(); // 清除之前的高亮cursor.beginEditBlock();QTextCharFormat plainFormat(highlightCursor.charFormat());//文本样式QTextCharFormat colorFormat = plainFormat;colorFormat.setForeground(Qt::red);//如果找到则置红while (!highlightCursor.isNull() && !highlightCursor.atEnd()){highlightCursor = document->find(searchText, highlightCursor);if (!highlightCursor.isNull()){foundWholeWord = true;highlightCursor.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor);highlightCursor.mergeCharFormat(colorFormat);}}cursor.endEditBlock();if (!foundWholeWord) {QMessageBox::information(this, tr("Partial Match Found"),tr("The text '%1' was found as part of other words but not as a whole word.").arg(searchText));}
}

main

#include "textfinder.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);textFinder w;w.show();return a.exec();
}

功能:可查找想要收索的关键字,找到则标红
当前问题:如果给的文本例如:abcd efg,只搜索a时abcd会一起标红,或许是设置查找模式出了问题
解决方案:查看是否是查找模式设置出了问题,如不是,则可通过kmp字符串匹配修改查找方案

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

相关文章:

  • 使用 Python 剪辑视频的播放速度
  • 深入理解计算机系统,源码到可执行文件翻译过程:预处理、编译,汇编和链接
  • Linux开发者的CI/CD(11)jenkins变量
  • 深度学习视频编解码开源项目介绍【持续更新】
  • Canva迁移策略深度解析:应对每日5000万素材增长,从MySQL到DynamoDB的蜕变
  • nacos常见面试题(2024)
  • 68000汇编实战01-编程基础
  • 你的网站真的安全吗?如何防止网站被攻击?
  • UE5 材质编辑器CheapContrast 节点
  • 健身房小程序服务渠道开展
  • Java基础面试题08:Java中Exception和Error有什么区别?
  • 什么是axios?怎么使用axios封装Ajax?
  • Web前端学习_CSS盒子模型
  • JAVA项目-------医院挂号系统
  • [工具分享] 根据Excel数据根据Word文档模板,批量创建生成Word文档并重命名,方便快速查找打印
  • Redis的管道操作
  • IT监控 | Oracle云监控全解析
  • 前端面试题-1(详解事件循环)
  • Redis(5):哨兵
  • 【人工智能】Transformers之Pipeline(二十五):图片特征抽取(image-feature-extraction)
  • podman 源码 5.3.1编译
  • 矩阵重新排列——rot90函数
  • Leetcode 51 N Queens Leetcode N Queens II
  • 0.查找命令
  • HarmonyOS-初级(一)
  • Oracle 11gR2 坏块修复实例一则
  • 解决FinalShell 连接virtual box安装的Linux centos/7系统 一直让输入密码,输入什么密码都没用
  • 华为E9000刀箱(HWE9000V2)服务器硬件监控指标解读
  • Python基础学习-12匿名函数lambda和map、filter
  • 民安:助力提升城市安全水平