QListview, QTreeview which is right to show files in a list
I like to show files like in the Windows Explorer or any other file manager with Qt in a list. This was no problem using QFilesystemModel开发者_开发技巧 and QListView but there were no columns like size or the last modified date. Next I tried to use QTreeView and now there have been columns but unfortunately for moving into the filesystem every time an folder expanded and not like the filemangers do, only show the content of the actual folder.
How can I have columns and an listviewstyle navigation?
Thank you for your answers.
If I understand you correctly, you want multiple columns (which QListView does not support), but a flat list without the content of subfolders? This here works for me, tested on OS X: It uses setRootIndex to hide the root folder ("/" in this case) and proxy model to filter all children of the root node's children.
#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();
}
精彩评论