Qt File Browser based on QML

catinred picture catinred · May 26, 2011 · Viewed 9k times · Source

It is easy to implement a file browser by using QFileSystemModel. But the listview UI is not pretty. So I want to implement a file browser using QML. the QML has model/view support. But how to display the filesystem tree in QML? Any clue would be appreciated.

Answer

Libor Tomsik picture Libor Tomsik · Oct 12, 2015

Since Qt5.5 we have TreeView QML component available,

main.qml:

import QtQuick.Controls 1.4
TreeView {
    anchors.fill: parent
    TableViewColumn {
        title: "Name"
        role: "fileName"
        width: 300
    }
    model: my_model
}

main.cpp:

QFileSystemModel model;
model.setRootPath("/");
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("my_model", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));