Last active
May 8, 2018 10:38
-
-
Save mhoeher/be850f091143a70ca187a69a6af6ee0c to your computer and use it in GitHub Desktop.
QFileSystemModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
#include <QQmlContext> | |
#include <QFileSystemModel> | |
class FileSystemModel : public QFileSystemModel | |
{ | |
public: | |
explicit FileSystemModel(QObject *parent = nullptr) : QFileSystemModel(parent) {} | |
enum Roles { | |
FileSizeRole = Qt::UserRole + 1 | |
}; | |
QVariant data(const QModelIndex &index, int role) const | |
{ | |
switch (role) { | |
case FileSizeRole: | |
return QFileSystemModel::data(this->index(index.row(), 1, index.parent()), | |
Qt::DisplayRole); | |
default: | |
return QFileSystemModel::data(index, role); | |
} | |
} | |
QHash<int, QByteArray> roleNames() const | |
{ | |
auto result = QFileSystemModel::roleNames(); | |
result.insert(FileSizeRole, "fileSize"); | |
return result; | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | |
QGuiApplication app(argc, argv); | |
FileSystemModel *model = new FileSystemModel(&app); | |
model->setRootPath("/"); | |
QQmlApplicationEngine engine; | |
engine.rootContext()->setContextProperty("fsModel", model); | |
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); | |
if (engine.rootObjects().isEmpty()) | |
return -1; | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.9 | |
import QtQuick.Window 2.2 | |
import QtQuick.Controls 1.4 | |
Window { | |
visible: true | |
width: 640 | |
height: 480 | |
title: qsTr("Hello World") | |
TreeView { | |
model: fsModel | |
anchors.fill: parent | |
TableViewColumn { | |
role: "display" | |
} | |
TableViewColumn { | |
role: "fileSize" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment