【问题标题】:QListview, QTreeview which is right to show files in a listQListview, QTreeview 正确显示列表中的文件
【发布时间】:2011-03-30 17:51:35
【问题描述】:
我喜欢在 Windows 资源管理器或任何其他带有 Qt 的文件管理器中显示文件。使用 QFilesystemModel 和 QListView 没有问题,但是没有像大小或上次修改日期这样的列。接下来我尝试使用 QTreeView,现在有列,但不幸的是,每次扩展文件夹时都移入文件系统,而不像文件管理器那样,只显示实际文件夹的内容。
如何拥有列和列表视图样式导航?
感谢您的回答。
【问题讨论】:
标签:
c++
qt
file
qt4
filesystems
【解决方案1】:
如果我对您的理解正确,您想要多个列(QListView 不支持),但是没有子文件夹内容的平面列表?这对我有用,在 OS X 上测试:
它使用 setRootIndex 隐藏根文件夹(在本例中为“/”)并使用代理模型过滤根节点子节点的所有子节点。
#include <QApplication>
#include <QFileSystemModel>
#include <QTreeView>
#include <QSortFilterProxyModel>
class Proxy : public QSortFilterProxyModel {
public:
explicit Proxy( QObject* parent=0 ) : QSortFilterProxyModel( parent ) {}
bool filterAcceptsRow( int, const QModelIndex& parent ) const {
return !parent.parent().isValid();
}
};
int main( int argc, char** argv ) {
QApplication app( argc, argv );
QFileSystemModel model;
Proxy proxy;
proxy.setSourceModel( &model );
const QModelIndex rootIdx = proxy.mapFromSource( model.setRootPath( QLatin1String("/") ) );
QTreeView view;
view.setModel( &proxy );
view.setRootIndex( rootIdx );
view.setRootIsDecorated( false );
view.show();
return app.exec();
}