本文代码效果如下:
本文代码:
https://download.csdn.net/download/Sakuya__/89759410https://download.csdn.net/download/Sakuya__/89759410
代码之路
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QStandardItemModel>
#include <QFileInfo>
#include <QDir>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void InitTreeView(QString path);bool GetAllFiles(QString path, QStandardItem *parentItem);void GetItem(QStandardItem *item);void ParentExpand(QStandardItem *item);private slots:void on_Btn_Search_clicked();private:Ui::MainWindow *ui;QStandardItemModel *m_treeViewModel;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->setWindowIcon(QIcon(":/Resouce/heart.ico"));// 设置树形结构m_treeViewModel = new QStandardItemModel(ui->treeView);m_treeViewModel->setHorizontalHeaderLabels(QStringList() << "文件名称" << "文件路径");ui->treeView->setModel(m_treeViewModel);ui->treeView->header()->setStretchLastSection(true);ui->treeView->hideColumn(1);ui->treeView->header()->hide();ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);InitTreeView("E:/MyWorkSpace/Qt/Demo");
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::InitTreeView(QString path)
{QFileInfo fileInfo(path);if (!fileInfo.isDir()){return;}m_treeViewModel->removeRows(0, m_treeViewModel->rowCount());// 绑定文件夹的图标和文件路径QStandardItem *item = new QStandardItem(QIcon(":/Resouce/Folder.png"), path.split("/").last());m_treeViewModel->appendRow(item);m_treeViewModel->setItem(m_treeViewModel->indexFromItem(item).row(), 1, new QStandardItem(fileInfo.path()));GetAllFiles(path, item);
}bool MainWindow::GetAllFiles(QString path, QStandardItem *parentItem)
{QDir sourceDir(path);// 文件按时间排序QStringList filter;sourceDir.setNameFilters(filter);QList<QFileInfo> fileInfoList = QList<QFileInfo>(sourceDir.entryInfoList(filter, QDir::AllEntries, QDir::Time));// 遍历当前文件夹下的文件列表foreach (QFileInfo fileInfo, fileInfoList){if (fileInfo.fileName() == "." || fileInfo.fileName() == ".."){continue;}if (fileInfo.isDir()){// 如果存在子级,添加子级数据QStandardItem *item = new QStandardItem(QIcon(":/Resouce/Folder.png"), fileInfo.baseName());parentItem->appendRow(item);parentItem->setChild(item->row(), 1, new QStandardItem(fileInfo.filePath()));GetAllFiles(fileInfo.filePath(), item);}else if (fileInfo.isFile()){// 如果存在子级,添加子级数据QStandardItem *item = new QStandardItem(QIcon(":/Resouce/Document.png"), fileInfo.baseName());parentItem->appendRow(item);parentItem->setChild(item->row(), 1, new QStandardItem(fileInfo.filePath()));}}return true;
}void MainWindow::GetItem(QStandardItem *item)
{Q_ASSERT(item);// 如果该行包含要查找的字符串,展开所有的父节点并把背景设为红色if (item->text().contains(ui->lineEdit->text())){item->setBackground(QBrush(qRgb(255, 125, 125)));ParentExpand(item);}else{item->setBackground(QBrush(qRgb(255, 255, 255)));}// 如果有子项,继续查找所有子项if (item->hasChildren()){for (int i = 0; i < item->rowCount(); i++){item->setSelectable(true);QStandardItem *childItem = item->child(i);GetItem(childItem);}}
}void MainWindow::ParentExpand(QStandardItem *item)
{// 如果有父节点,就展开if (item->parent() != nullptr){QStandardItem *parentItem = item->parent();ui->treeView->expand(parentItem->index());ParentExpand(parentItem);}
}void MainWindow::on_Btn_Search_clicked()
{// 所有行全部折叠ui->treeView->collapseAll();if (!ui->lineEdit->text().isEmpty()){// 遍历查找所有行for (int i = 0; i < m_treeViewModel->rowCount(); i++){GetItem(m_treeViewModel->item(i));}}else{for (int i = 0; i < m_treeViewModel->rowCount(); i++){m_treeViewModel->item(i)->setBackground(QBrush(qRgb(255, 255, 255)));}}
}