qt实现类似everything搜索功能,速度没有everything那么快

本文介绍了一个使用全局搜索功能的Widget应用,通过模糊查询遍历文件和目录,尽管速度不快但实现了按名称匹配。源码展示了如何在QTreeWidget中实时更新搜索结果,并使用了QThread进行异步处理。
摘要由CSDN通过智能技术生成

其实就是使用全局搜索,匹配相同的文件或者文件夹名字,使用的是模糊查询,所以速度并没有那么快
源码:
.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDir>
#include <QFile>
#include <QLineEdit>
#include <QPushButton>
#include <qlabel.h>
#include <QTreeWidget>
#include <qthread.h>

class Widget : public QWidget
{
    Q_OBJECT

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

    void SearchFolder(QString i_path);

private:
    void Init();



private slots:
    void onSearchTextChanged();

private:
    QLineEdit *m_pSearchEdit=nullptr;
    QTreeWidget *m_pSearchTreeWgt=nullptr;
    QString m_SearchText="";

};

class SearchFolderThread :public QThread
{
    Q_OBJECT

public:
    SearchFolderThread(Widget *pwgt,QString i_path)
    {
        m_pJobWgt = pwgt;
        m_Path=i_path;
    }

private:

    void run()
    {
        m_pJobWgt->SearchFolder(m_Path);
    }

    Widget* m_pJobWgt = nullptr;
    QString m_Path="";

};

#endif // WIDGET_H

.cpp

#include "widget.h"
#include <QHBoxLayout>
#include <QFileInfo>
#include <qdebug.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(800,600);
    Init();


}

Widget::~Widget()
{
}

void Widget::Init()
{
    QFont textFont;
    textFont.setPointSize(12);
    textFont.setFamily("Microsoft YaHei");
    QLabel *SearchLabel=new QLabel(tr("Search File :"));
    SearchLabel->setFixedWidth(100);
    SearchLabel->setFont(textFont);
    m_pSearchEdit=new QLineEdit(this);
    m_pSearchEdit->setFont(textFont);
    QWidget *searchWgt=new QWidget(this);
    QHBoxLayout *searchLayout=new QHBoxLayout(this);
    searchLayout->addWidget(SearchLabel);
    searchLayout->addWidget(m_pSearchEdit);
    searchLayout->setMargin(0);
    searchLayout->setSpacing(5);
    searchWgt->setLayout(searchLayout);
    m_pSearchTreeWgt=new QTreeWidget(this);
    m_pSearchTreeWgt->setHeaderLabel("");
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(searchWgt);
    mainLayout->addWidget(m_pSearchTreeWgt);
    mainLayout->setMargin(10);
    mainLayout->setSpacing(5);
    this->setLayout(mainLayout);

    connect(m_pSearchEdit,&QLineEdit::textChanged,this,&Widget::onSearchTextChanged);

}

void Widget::SearchFolder(QString i_path)
{
    QDir dir(i_path);
    for(int i=0;i<dir.count();++i)
    {
        if(dir[i]=="."||dir[i]=="..")
        {
            continue;
        }
        QFileInfo fileInfo(i_path+"/"+dir[i]);
        if(fileInfo.fileName().toLower().contains(m_SearchText.toLower()))
        {
            QTreeWidgetItem *item=new QTreeWidgetItem(m_pSearchTreeWgt);
            item->setText(0,fileInfo.fileName());
            m_pSearchTreeWgt->addTopLevelItem(item);
            //qDebug()<<"searchfile::"<<fileInfo.fileName();
        }
        if(fileInfo.isDir())
        {
            //qDebug()<<"dir::"<<fileInfo.filePath();
            SearchFolder(fileInfo.filePath());
        }
    }


}

void Widget::onSearchTextChanged()
{
    m_pSearchTreeWgt->clear();
    m_SearchText=m_pSearchEdit->text();
    QFileInfoList infoList= QDir::drives();
    for(int i=0;i<infoList.size();++i)
    {
        QDir dir(infoList[i].filePath());
        for(int j=0;j<dir.count();++j)
        {
            QFileInfo info(infoList[i].filePath()+dir[j]);
            if(info.isDir())
            {
                //qDebug()<<"dir[j]::"<<info.filePath();
                SearchFolderThread* thread = new SearchFolderThread(this,info.filePath());
                thread->wait();
                thread->quit();
                thread->start();
                //SearchFolder(info.filePath());
            }
            if(info.fileName().toLower().contains(m_SearchText.toLower()))
            {
                QTreeWidgetItem *item=new QTreeWidgetItem(m_pSearchTreeWgt);
                item->setText(0,info.fileName());
                m_pSearchTreeWgt->addTopLevelItem(item);
            }
        }
    }
}


效果图:
在这里插入图片描述
good luck to everything

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油小杜(接qt定制功能,单模块开发等)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值