当前位置: 首页 > news >正文

QT 实现文件资源浏览器

先看效果代码如下ExplorerWindow.h#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#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#include QApplication #include ExplorerWindow.h int main(int argc, char *argv[]) { QApplication app(argc, argv); ExplorerWindow window; window.show(); return app.exec(); }
http://www.zskr.cn/news/1321312.html

相关文章:

  • Adobe Illustrator脚本终极指南:如何将设计效率提升10倍的完整教程
  • 2026年国内干细胞机构怎么选?干细胞公司、研究所、制备中心TOP5名单出炉,认准这5家权威认证! - 资讯速览
  • 仅限本周开放!Perplexity电影插件Beta权限获取通道(附内部文档+调试日志样本)
  • 高性能RISC-V MCU移植OpenMV:嵌入式机器视觉开发实践
  • 虚拟内存到物理地址转换:操作系统内存管理的核心机制
  • 嘉善银城驾驶员培训:嘉善C1、C2驾校招生机构 - LYL仔仔
  • 终极网盘直链下载助手完整使用指南:如何高效获取八大网盘文件直链
  • PCB设计前必知:五种生产工艺方案选型与Layout实战指南
  • 2026年无锡高端首饰回收科普:从行情到机构,一篇读懂 - 奢侈品回收测评
  • 北京道闸选型答疑及正规厂家联系渠道梳理 - 真知灼见33
  • fullPage.js:企业级全屏滚动解决方案的技术架构与性能优化策略
  • 猎头疯抢、VC踏破门槛——这家排名第12的公司,名字你读都读不顺 - 资讯焦点
  • 别再怕sudo rm -rf了!手把手教你用Win32DiskImager备份树莓派SD卡(附恢复教程)
  • 用好 Codex Goal,关键就这三步
  • 2026年5月常州包包回收行情指南:看懂保值款,避坑高效变现 - 奢侈品回收测评
  • 从决策树到XGBoost:核心原理、目标函数与工程优化全解析
  • 厂区多设备集中联网,如何避免信号拥堵与互相干扰?
  • Windows本地搞定3DGS复现:从COLMAP手动配准到SIBR可视化,保姆级避坑指南
  • 2026年高端商务办公杯适合送礼吗?5个品牌横向对比 - 科技焦点
  • 工具使用-AI
  • Linux内核pr_xx()日志宏:从printk封装到动态调试实战
  • 京东618家电优惠券怎么领?2026京东淘宝618红包口令是什么?空调冰箱洗衣机电视大额家电券+红包口令+国补优惠保姆级教程 - 资讯焦点
  • 别再无视YOLO训练日志里的WARNING了!手把手教你用OpenCV修复corrupt JPEG图像
  • 树莓派Python多进程并行计算实战:解锁多核性能,加速AI与图像处理
  • 伯远生物基因组编辑|给作物 “改个妆”,优良性状随心造
  • 2026 年张家口靠谱建筑设备租赁源头厂家推荐:脚手架 / 模板木方 / 塔吊租赁选择指南 - 海棠依旧大
  • Workflow Ruby Gem终极指南:10分钟掌握有限状态机建模
  • 2026年匠心精选:香港收楼后多久可以装修? - 品牌推广大师
  • 在数据爬取脚本中集成 Taotoken 多模型 API 进行内容摘要
  • Perplexity法律文献搜索实战指南:7步构建精准检索式,避开90%的无效结果