博客
关于我
Qt学习笔记 QFileSystemModel 只显示名称
阅读量:227 次
发布时间:2019-03-01

本文共 1599 字,大约阅读时间需要 5 分钟。

通过修改表头,只显示一列

通过修改表头,只显示一列

Qt 提供的 QFileSystemModel 可以提供文件目录树预览功能,但是预览的都自带了Name,size,type, modified等信息。我现在只想显示 name 一列,不想显示 size, type, modified 的信息。

解决办法

方法1:修改 QFileSystemModel

写一个子类,继承自 QFileSystemModel,然后在需要显示 size, type, modified 的地方,把这些信息屏蔽掉。我知道 Qt 的 Model 显示数据,主要是在 data 这个函数中,然后 Model 的列是通过 columnCount 这个函数返回的。所以我重写 columnCount 函数,只返回一列,这一列就是我们想要的名称。

#include 
#include
#include
class MyFileSytemModel : public QFileSystemModel {public: int columnCount(const QModelIndex &parent) const { return 1; }};int main(int argc, char *argv[]) { QApplication a(argc, argv); MyFileSytemModel* model = new MyFileSytemModel; model->setRootPath(QDir::currentPath()); QTreeView* treeView = new QTreeView; treeView->setModel(model); treeView->setRootIndex(model->index(QDir::currentPath())); treeView->show(); return a.exec();}

方法2:修改 TreeView

如果我能把 TreeView 的第 2,3,4 列隐藏,也可以达到我想要的效果。QTreeView 中有个函数 setColumnHidden,可以使用这个函数来隐藏指定的列。

#include 
#include
#include
int main(int argc, char *argv[]) { QApplication a(argc, argv); QFileSystemModel* model = new QFileSystemModel; model->setRootPath(QDir::currentPath()); QTreeView* treeView = new QTreeView; treeView->setModel(model); treeView->setRootIndex(model->index(QDir::currentPath())); treeView->show(); treeView->setColumnHidden(1, true); treeView->setColumnHidden(2, true); treeView->setColumnHidden(3, true); return a.exec();}

作者:snail_hunan来源:CSDN原文:https://blog.csdn.net/snail_hunan/article/details/75250992

你可能感兴趣的文章
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用node-red-contrib-image-output节点实现图片预览
查看>>
Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
查看>>
node-request模块
查看>>
Node.js 8 中的 util.promisify的详解
查看>>
Node.js 函数是什么样的?
查看>>
Node.js 历史
查看>>