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