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

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

 

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

m_model = new QFileSystemModel();    m_model->setRootPath(QDir::currentPath());    ui->treeView->setModel(m_model);    ui->listView->setModel(m_model);    ui->tableView->setModel(m_model);    ui->treeView->setRootIndex(m_model->index(QDir::currentPath()));    QStringList list;    list.append("名称");//    list.append("大小");//    list.append("类型");//    list.append("修改日期");    QStandardItemModel *itemModel =new QStandardItemModel();    itemModel->setHorizontalHeaderLabels(list);    ui->treeView->header()->setModel(itemModel);

 

 

转自:

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: //第1列显示名称,其他几列都屏蔽掉 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 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

你可能感兴趣的文章
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>