c++ - QML File Browser QDirModel vs QFileSystemModel -
i trying implement qml based file browser. there 2 file models in qt 5.1, qdirmodel , qfilesystemmodel, qdirmodel documentation says
this class obsolete. provided keep old source code working. advise against using in new code.
my code works qdirmodel not qfilesystemmodel, here code:
main.cpp
#include <qtwidgets/qapplication> #include <qquickview> #include <qqmlcontext> #include <qfilesystemmodel> #include <qurl> #include <qdirmodel> int main(int argc, char *argv[]) { qapplication a(argc, argv); qquickview view; qdirmodel model; view.rootcontext()->setcontextproperty("dirmodel", &model); view.setsource(qurl::fromlocalfile("main.qml")); view.setresizemode(qquickview::sizerootobjecttoview); view.show(); return a.exec(); }
and here main.qml:
import qtquick 2.0 rectangle { width: 400; height: 400; listview { id: view; anchors.fill: parent; model: visualdatamodel { model: dirmodel; delegate: rectangle { width: parent.width; height: 40; text { text: filename } } } } }
this code works when use qfilesystemmodel instead of qdirmodel doesn't work ,that is, doesn't display files properly.
you should use qfilesystemmodel. if don't know how use it, doc more carefully, or use 'folderlistmodel' item directly in qml, it's in 'qt.labs.folderlistmodel 2.0' module, , it's made used in qml, , quite easy use. myself have file browser made it.
Comments
Post a Comment