我构建的有点像标准的文件资源管理器-左窗格是文件夹树,右边的一个显示文件在选定的文件夹。
QTreeView和QFileSystemModel一起用于显示文件夹。模型的过滤器设置为QDir::Dirs | QDir::NoDotAndDotDot,仅列出dirs,没有文件。我只想对带有子文件夹的文件夹显示扩展标记,也就是说,如果某些dir是空的,或者只包含文件,那么它不应该是可扩展的。但是,树形视图保持对空dir的扩展标记。这就是问题:如何隐藏他们--
我已经在这里搜索过解决方案,在谷歌,在QT的例子-没有成功。我认为这个问题很容易回答。我现在想要的唯一解决方案是子类QAbstractItemModel。好痛啊。
QT 4.8,QT创造者,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();
}发布于 2013-02-19 12:30:53
我已经解决了这个问题
QFileSystemModel::fetchMore
在当前级别的每个QModelIndex上。要知道某个文件夹是否已加载到模型中,可以使用信号
void directoryLoaded ( const QString & path )
发布于 2013-08-02 01:19:33
最简单的方法:way这样实现hasChildren:
/*!
* Returns true if parent has any children and haven't the Qt::ItemNeverHasChildren flag set;
* otherwise returns false.
*
*
* \remarks Reimplemented to avoid empty directories to be collapsables
* and to implement the \c Qt::ItemNeverHasChildren flag.
* \see rowCount()
* \see child()
*
*/
bool YourModelName::hasChildren(const QModelIndex &parent) const
{
// return false if item cant have children
if (parent.flags() & Qt::ItemNeverHasChildren) {
return false;
}
// return if at least one child exists
return QDirIterator(
filePath(parent),
filter() | QDir::NoDotAndDotDot,
QDirIterator::NoIteratorFlags
).hasNext();
}https://stackoverflow.com/questions/14545711
复制相似问题