[Qt-qml] QML: DataModel

Gregory Schlomoff gregory.schlomoff at gmail.com
Tue Jul 13 02:51:46 CEST 2010


You should probably create some slots on C++ that interact with your
QAbstractItemModel, then call those slots from QML.

The rule is that QML can only call slots or Q_INVOKABLE, and not normal methods.

On Tue, Jul 13, 2010 at 4:40 AM, Almo Nito <AlmoNito at gmx.de> wrote:
> Hello There,
>
>
>
>
>
> I played around with QML today and ended in trying on of the examples in the
> docs that actually tried to use the QAbstractItemModel as a file browser.
>
>
>
> So I copy-pasted the source and tried out their example with the result that
> the root directory is showing up (the “/”) and the onClick handler is
> working as well,
>
>
>
> The only problem is that it don’t seems possible to brows the File system,
> any ideas on that one?
>
>
>
>
>
>
>
> QML:
>
>
>
>
>
> import Qt 4.7
>
> import "ressource" as Ressources
>
>
>
> Rectangle {
>
>     id: background
>
>     width: 770;
>
>     height: 480;
>
>     color: "lightgray"
>
>
>
>     ListView {
>
>         id: view
>
>         width: 300
>
>         height: 400
>
>
>
>
>
>         model: VisualDataModel {
>
>
>
>             model: myModel
>
>
>
>             delegate: Rectangle {
>
>                 width: 200; height: 25
>
>                 Text { text: filePath }
>
>
>
>                 MouseArea {
>
>                     anchors.fill: parent
>
>                     onClicked: {
>
>
>
>
>
>                         if (view.model.hasModelChildren) {
>
>                             console.log("childs")
>
>                             view.model.rootIndex = model.modelIndex(index)
>
>                         }
>
>                         else {
>
>                             console.log("NoChilds")
>
>
>
>                         }
>
>
>
>                     }
>
>                 }
>
>             }
>
>
>
>         }
>
>
>
>     }
>
> }
>
>
>
>
>
>
>
>
>
> C++:
>
>
>
>
>
> QDeclarativeView *view = new QDeclarativeView(this);
>
>
>
>
>
>     QFileSystemModel *modelDir = new QFileSystemModel;
>
>     modelDir->setRootPath("/home");
>
>
>
>
>
>     ui->treeView->setModel(modelDir);
>
>
>
>
>
>
>
>     QDeclarativeContext *ctxt = view->rootContext();
>
>     ctxt->setContextProperty("myModel",modelDir);
>
>
>
>
>
>     QUrl url = QUrl::fromLocalFile(“browsertest.qml");
>
>     view->setSource(url);
>
>
>
>
>
>     view->show();
>
>
>
>
>
>
>
>
>
>
>
>
>
> Further in the C++ I have anoter issue, if I set the RootDir as in
> modelDir->setRootPath("/home"); it seems not to do any changes at all.
>
>
>
> Any Ideas?
>
> Thanks!
>
> Almo
>
>
>
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml
>
>




More information about the Qt-qml mailing list