Filebrowser using QML
-
Could some one please help me out in implementing Filebrowser in QML.
Currently this is what is being done:main.cpp
@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemModel *model = new QFileSystemModel();
model->setRootPath("/home/user/");
QDeclarativeView view;
QDeclarativeEngine *engine = view.engine();
QDeclarativeContext *context = view.rootContext();
context->setContextProperty("qml_model",model);
view.setSource(QUrl::fromLocalFile("example.qml"));
QObject rootObject = dynamic_cast<QObject>(view.rootObject());
view.setGeometry(QRect(0,0,840,480));
view.showFullScreen();
return app.exec();
}
@example.qml:
@
Rectangle {id:file10 width: 840 height: 240 x: 0 y: 100 ListView { id: view width: 840 height: 240 highlight: Rectangle { color: "lightsteelblue"; radius: 5 } focus: true model: VisualDataModel { model: qml_model delegate: Rectangle { width: 100; height: 100 Text { text: fileName ; anchors.horizontalCenter: parent.horizontalCenter } MouseArea { anchors.fill: parent onClicked: { if (model.hasModelChildren) { view.model.rootIndex = view.model.modelIndex(index) } else { console.log("no folders") } } } } } } }
@
The problem is i do not get to browse inside /home/user/ even if there are multiple folders.And my starting point is always '/' and not /home/user as I have mentioned in .cpp
[EDIT: code formatting, please use @-tags, Volker]
-
Please format your code. Will help others to read and understand it better.
-
thanks .I think this post is really help full.
-
Hi,
I'd suggest taking a look at the FolderListModel element if you haven't done so already (http://doc.qt.nokia.com/4.7-snapshot/qml-folderlistmodel.html). You can find an example of its use in the qmlviewer application (the QML files are in the source tree under tools/qml/browser).
In regards to model->setRootPath("/home/user/"), from the documentation it looks like this function only determines what directory is watched by the model for changes, and doesn't impact what is exposed to/shown by the view (rootIndex needs to be set for that).
Regards,
Michael -
I am assuming this is the dialog used on the qmlviewer application when file--> open is selected?
-
[quote author="kyleplattner" date="1296434643"]I am assuming this is the dialog used on the qmlviewer application when file--> open is selected?[/quote]
It depends on the platform as to what qmlviewer shows by default. You can explicitly turn on the QML-based file browser by passing -qmlbrowser to qmlviewer.
Regards,
Michael -
I have actually raised a bug for the issue http://bugreports.qt.nokia.com/browse/QTBUG-17141