I'm trying to implement a very basic file browser that only shows files in a set directory. Using the QFileSystemModel, I've set the root path but the connected QML VisualDataModel still shows the entire path. I have to keep clicking to get the directory I want ("/testfolder/test"). I tried using the Symbian FolderModel QML element but it's a bit buggy, it doesn't refresh properly when a file is added or deleted.
I tried exporting the index of the root path to the VisualDataModel using a QModelIndex but I keep getting the following compile errors:
../../QtSDK/Simulator/Qt/gcc/include/QtCore/qvariant.h:433: error: 'QVariant::QVariant(void*)' is private
../qfiledemo/main.cpp:46: error: within this context
I want to use the QModelIndex to set the VisualDataModel to jump directly to the path I want, rather than starting out at the top of the filesystem. Any way to expose a QVariant to QML without making a custom class? Thanks!
Here is my main.cpp:
int main(int argc, char *argv[])
{
QDeclarativeView view;
QFileSystemModel model;
model.
setRootPath(QString("/testfolder/test"));
QModelIndex rootidx
= model.
index("/testfolder/test");
// find the correct index to pass to VisualDataModel
view.rootContext()->setContextProperty("dirModel", &model);
view.rootContext()->setContextProperty("rootIndex", &rootidx); // this always fails!
view.
setSource(QUrl::fromLocalFile("qml/qfiledemo/main.qml"));
view.show();
return app.exec();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
QFileSystemModel model;
model.setRootPath(QString("/testfolder/test"));
QModelIndex rootidx = model.index("/testfolder/test"); // find the correct index to pass to VisualDataModel
view.rootContext()->setContextProperty("dirModel", &model);
view.rootContext()->setContextProperty("rootIndex", &rootidx); // this always fails!
view.setSource(QUrl::fromLocalFile("qml/qfiledemo/main.qml"));
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
My main.qml:
ListView {
id: view
width: 300
height: 400
model: VisualDataModel {
model: dirModel
delegate: Rectangle {
width: 200; height: 25
Text { text: filePath }
MouseArea {
anchors.fill: parent
onClicked: {
if (model.hasModelChildren) {
view.model.rootIndex = view.model.modelIndex(index)
// Would be great if I could set the rootIndex in here to jump straight to the directory I want
}
}
}
}
}
}
ListView {
id: view
width: 300
height: 400
model: VisualDataModel {
model: dirModel
delegate: Rectangle {
width: 200; height: 25
Text { text: filePath }
MouseArea {
anchors.fill: parent
onClicked: {
if (model.hasModelChildren) {
view.model.rootIndex = view.model.modelIndex(index)
// Would be great if I could set the rootIndex in here to jump straight to the directory I want
}
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks