1.相关描述
QFileSystemModel与QTreeView、QTableView、QListView的组合,当QTreeView点击发生改变,QTableView和QListView也会发生变化
2.相关界面
3.相关代码
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileSystemModel> #include <QFileDialog> #include <QHeaderView> #include <QStandardItemModel> MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);m_model = new QFileSystemModel(this);ui->treeView->setModel(m_model);ui->listView->setModel(m_model);ui->tableView->setModel(m_model);m_model->setRootPath(QDir::currentPath());// 树形视图与列表视图的信息联动connect(ui->treeView, &QTreeView::clicked, ui->listView, &QListView::setRootIndex);// 树形视图与表格视图的信息联动connect(ui->treeView, &QTreeView::clicked, ui->tableView, &QTableView::setRootIndex);this->initHeader();}MainWindow::~MainWindow() {delete ui; }void MainWindow::initHeader() {QStringList heardList;//表头heardList << "名称" << "大小" << "文件类型" << "修改时间";QHeaderView *hearviewTable = new QHeaderView(Qt::Horizontal, this);QHeaderView *hearviewTree = new QHeaderView(Qt::Horizontal, this);QStandardItemModel* model = new QStandardItemModel(this);model->setHorizontalHeaderLabels(heardList);hearviewTable->setModel(model);hearviewTree->setModel(model);hearviewTable->setSectionResizeMode(QHeaderView::Stretch); //先自适应宽度hearviewTable->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列hearviewTree->setSectionResizeMode(QHeaderView::Stretch); //先自适应宽度hearviewTree->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列ui->tableView->setHorizontalHeader(hearviewTable);ui->treeView->setHeader(hearviewTree); } // 设置根目录 void MainWindow::on_actionSetRootDir_triggered() {QString dir = QFileDialog::getExistingDirectory(this, "选择目录", QDir::currentPath());if(dir.isEmpty()){return;}ui->treeView->setRootIndex(m_model->index(dir));ui->listView->setRootIndex(m_model->index(dir));ui->tableView->setRootIndex(m_model->index(dir)); } // 显示目录和文件 void MainWindow::on_rBtnShowDirAndFile_clicked() {ui->groupBoxFilter->setEnabled(true);m_model->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot); } // 只显示目录 void MainWindow::on_rBtnShowOnlyDir_clicked() {ui->groupBoxFilter->setEnabled(false);m_model->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot); } // 文件名过滤 void MainWindow::on_chkFileFilter_clicked(bool checked) {ui->comboBox->setEnabled(checked);ui->btnApply->setEnabled(checked);m_model->setNameFilterDisables(!checked); } // 应用 void MainWindow::on_btnApply_clicked() {QStringList filter = ui->comboBox->currentText().trimmed().split(";", Qt::SkipEmptyParts);m_model->setNameFilters(filter); } // 显示底部状态 void MainWindow::on_treeView_clicked(const QModelIndex &index) {ui->labFileDir->setText(m_model->fileName(index));ui->labFilePath->setText(m_model->filePath(index));ui->labFileType->setText(m_model->type(index));ui->chkNodeDir->setEnabled(m_model->isDir(index));ui->chkNodeDir->setChecked(m_model->isDir(index));int sz = m_model->size(index)/1024;if(sz < 1024){ui->labFileSize->setText(QString("%1 Kb").arg(sz));} else {ui->labFileSize->setText(QString::asprintf("%.1f MB", sz/1024.0));} }