文本搜索程序(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字符串匹配修改查找方案

相关推荐
侃侃_天下19 分钟前
最终的信号类
开发语言·c++·算法
echoarts1 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号3 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 小时前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty3 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再3 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame