文本搜索程序(Qt)

头文件

cpp 复制代码
#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_NAMESPACE

class textFinder : public QWidget
{
    Q_OBJECT

public:
    textFinder(QWidget *parent = nullptr);
    ~textFinder();

public slots:
    void lodeTextFile();

private:
    Ui::textFinder *ui;
    void searchText();
    bool search_flog;
};
#endif // TEXTFINDER_H

cpp文件

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

cpp 复制代码
#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字符串匹配修改查找方案

相关推荐
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击2 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
m0_555762904 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
lzb_kkk6 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼6 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客7 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
程序员爱钓鱼7 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt