QT 实现文件资源浏览器

先看效果:

代码如下:

ExplorerWindow.h

cpp 复制代码
#pragma once

#include <QMainWindow>
#include <QSplitter>
#include <QTreeView>
#include <QTableView>
#include <QFileSystemModel>

class ExplorerWindow : public QMainWindow
{
public:
    ExplorerWindow(QWidget *parent = nullptr);
    ~ExplorerWindow();
    
private:
    void initUi();
    void createTreeView();
    void createTableView();
    void buildConnection();
    
private:
    QSplitter           *m_splitter {nullptr};
    QTreeView           *m_treeView {nullptr};
    QTableView          *m_tableView {nullptr};
    QFileSystemModel    *m_model {nullptr};
};

ExplorerWindow.cpp

cpp 复制代码
#include "ExplorerWindow.h"
#include <QHeaderView>
#include <QDir>

ExplorerWindow::ExplorerWindow(QWidget *parent) : QMainWindow(parent)
{
    initUi();
    buildConnection();
}

ExplorerWindow::~ExplorerWindow()
{
}

void ExplorerWindow::initUi()
{
    setWindowTitle(QStringLiteral("文件浏览器"));
    resize(1000, 600);
    
    m_splitter = new QSplitter(this);
    m_model = new QFileSystemModel(this);
    m_model->setRootPath(QString());
    
    createTreeView();
    createTableView();
    
    m_splitter->setStretchFactor(0, 1);
    m_splitter->setStretchFactor(1, 3);
    
    setCentralWidget(m_splitter);
}

void ExplorerWindow::createTreeView()
{
    m_treeView = new QTreeView(m_splitter);
    m_treeView->setModel(m_model);
    m_treeView->setRootIndex(m_model->index(QString()));
    
    m_treeView->setColumnWidth(0, 235);
    m_treeView->hideColumn(1);
    m_treeView->hideColumn(2);
    m_treeView->hideColumn(3);
    //m_treeView->header()->setStretchLastSection(true);
    m_treeView->setHeaderHidden(true);
    
    //     m_treeView->setStyleSheet(R"(
    // QTreeView {
    //     background-color: white;
    //     border: 1px solid #d0d0d0;
    //     selection-background-color: #3399ff;
    //     selection-color: white;
    // }
    // QTreeView::item {
    //     padding: 5px;
    // }
    // QTreeView::item:hover {
    //     background-color: #f0f7ff;
    // }
    // QTreeView::item:selected {
    //     background-color: #3399ff;
    //     color: white;
    // }
    // )");
}

void ExplorerWindow::createTableView()
{
    m_tableView = new QTableView(m_splitter);
    m_tableView->setModel(m_model);
    m_tableView->setRootIndex(m_model->index(QDir::homePath()));
    
    m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_tableView->setSortingEnabled(true);
    m_tableView->horizontalHeader()->setStretchLastSection(true);
    //m_tableView->verticalHeader()->setDefaultSectionSize(20);
    //m_tableView->verticalHeader()->setDefaultAlignment(Qt::AlignRight | Qt::AlignVCenter);
    m_tableView->verticalHeader()->hide();
    
    int cnt = m_tableView->horizontalHeader()->count();
    if (cnt > 0)
        m_tableView->setColumnWidth(0, 360);
    if (cnt > 1)
        m_tableView->setColumnWidth(1, 100);
    if (cnt > 2)
        m_tableView->setColumnWidth(2, 100);
    
    m_tableView->setStyleSheet(R"(
TableView {
    background: #f8fafc;
    alternate-background-color: #eef3f8;
    border: 1px solid #cfd8e3;
    border-radius: 6px;
    gridline-color: #d9e2ec;
    color: #1f2933;
    font-size: 14px;
    selection-background-color: #4a90e2;
    selection-color: white;
    outline: 0;
}
QTableView::item {
    padding: 2px 2px;
    border: none;
}
QTableView::item:hover {
    background: #e6f2ff;
}
QTableView::item:selected {
    background: #4a90e2;
    color: white;
}
QHeaderView::section {
    background: #e9eef5;
    color: #334155;
    padding: 4px;
    border: none;
    border-right: 1px solid #d0d7e2;
    border-bottom: 1px solid #cbd5e1;
    font-weight: 600;
})");
}

void ExplorerWindow::buildConnection()
{
    connect(m_treeView, &QTreeView::clicked, this, [this](const QModelIndex &index) {
        if (m_model->isDir(index)) {
            m_tableView->setRootIndex(index);
        }
    });    
}

main.cpp

cpp 复制代码
#include <QApplication>
#include "ExplorerWindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    ExplorerWindow window;
    window.show();
    
    return app.exec();
}
相关推荐
长沙红胖子Qt3 小时前
Qt+TiePieHS3开发笔记(二):HS3发送器发出内置波形、直连示波器,示波器检测内置波形Demo
qt·示波器·tiepiehs3·发生器·同步发射和示波器采集
陈年老古董6 小时前
PyQt5 入门指南:环境搭建、PyCharm 外部工具配置与登录界面实战
开发语言·qt·pycharm
zhanghaha13146 小时前
Qt 类完整例程【逐词拆解 + 逐步骤执行保姆教程】
开发语言·qt
Cx330❀10 小时前
Qt 界面美化指南:从零掌握 QSS 核心机制与工程实践
开发语言·qt·ui·性能优化·图形渲染
zx_7414848112 小时前
PyQt5 环境搭建保姆级教程:PyCharm + QtDesigner + PyUIC 完整配置
开发语言·qt·pycharm·pyqt
Python图像识别14 小时前
46-【2027毕设】YOLO11田间杂草检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
沫璃染墨1 天前
《Qt从零入门系列(十三):QSS样式表详解——从选择器与层叠规则到控件美化实战》
开发语言·css·c++·qt·ui·qt6.3·qss
好好学习O(∩_∩)O1 天前
[QT] 多线程moveToThread 和易踩的8个坑
开发语言·qt
北i1 天前
Dubbo 默认超时重试:把下游打雪崩的隐形放大器
开发语言·qt·dubbo
扶尔魔ocy1 天前
【qt openGL入门笔记】OSG实现显示飞机模型飞行的点迹(二)
qt·opengl·osg