QFileSystemModel和QTreeView仅显示目录.如何在空目录中隐藏扩展标记? [英] QFileSystemModel and QTreeView showing dirs only. How to hide expansion marks against the empty dirs?

查看:787
本文介绍了QFileSystemModel和QTreeView仅显示目录.如何在空目录中隐藏扩展标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建类似于标准文件浏览器的功能-左窗格用于文件夹树,右窗格用于显示所选文件夹中的文件.

I'm building somewhat like standard file explorer - left pane is for folders tree, and the right one to display files within selected folder.

带有QFileSystemModel的QTreeView用于显示文件夹.模型的过滤器设置为QDir::Dirs | QDir::NoDotAndDotDot,仅列出目录,不列出文件. 我只想对带有子文件夹的文件夹显示扩展标记,即. e.如果某个目录为空或仅包含文件,则该目录不可扩展.但是,树形视图会针对空目录保留扩展标记.问题是:如何隐藏它们?

QTreeView with QFileSystemModel is used to display folders. Model's filter is set to QDir::Dirs | QDir::NoDotAndDotDot to list dirs only, no files. I want to display expansion marks only against the folders with subfolders, i. e. if some dir is empty or contains only files, it shouldn't be expandable. But instead, tree view keeps expansion marks against the empty dir. And that's the question: how to hide them?

我已经在Google的QT示例中搜索了解决方案-没有成功.我认为这个问题很容易回答. 我目前唯一的解决方案是对QAbstractItemModel进行子类化.那很痛苦.

I've searched the solution here, in the google, in the QT examples - no success. While the question, I think, is very easy to answer. The only solution I came for at the moment is to subclass QAbstractItemModel. That's pain.

QT 4.8,QT Creator,C ++.

QT 4.8, QT Creator, C++.

以下代码演示:

#include <QApplication>

#include <QFileSystemModel>
#include <QTreeView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTreeView w;

    QFileSystemModel m;
    m.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    m.setRootPath("C:\\");

    w.setModel(&m);
    w.setRootIndex(m.index(m.rootPath()));
    w.hideColumn(3);
    w.hideColumn(2);
    w.hideColumn(1);

    w.show();

    return a.exec();
}

推荐答案

我已使用

QFileSystemModel :: fetchMore

QFileSystemModel::fetchMore

当前级别的每个QModelIndex上的

.要知道是否已将文件夹加载到模型中,可以使用信号

on each QModelIndex of the current level. To know if a folder has been loaded into the model, you can use the signal

void directoryLoaded(const QString& path)

void directoryLoaded ( const QString & path )

这篇关于QFileSystemModel和QTreeView仅显示目录.如何在空目录中隐藏扩展标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆