您的位置:首页 > 娱乐 > 明星 > QTreeView模糊查询

QTreeView模糊查询

2024/12/23 11:13:28 来源:https://blog.csdn.net/Sakuya__/article/details/142302763  浏览:    关键词:QTreeView模糊查询

        本文代码效果如下:

本文代码:

https://download.csdn.net/download/Sakuya__/89759410icon-default.png?t=O83Ahttps://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)));}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com