Important: Please read the Qt Code of Conduct -

Creating a QAbstractItemModel plugin object from QML?

  • I have written a C++ plug-in which defines a data model derived from QAbstractListModel. My QML code is successfully loading the plugin; that part of the code actually works...

    The documentation suggests that it is possible to define the model class in C++, export it via the plugin interface, then actually create the model and populate it from the QML side.

    I have the following QML code:
    @import QtQuick 1.0
    import MyModelPlugin 1.0

    ListView {
    id: list
    width: 640; height: 480;
    model: MyListModel {
    ListElement {source: "element 1";}
    ListElement {source: "element 2";}
    delegate: Text { text: source; }

    When I run this, however, I receive:

    @file:///home/gordons/build/testapp/Debug/qml/testapp/main.qml:8:8: Cannot assign to non-existent default property
    ListElement {source: "element 1";}

    I believe this is what the documentation indicates I should be doing; has anyone managed to make this work?

  • Could you show the pertinent C++ parts too please?

  • Hi,

    If you haven't seen it yet, I'd suggest looking at for more information/examples on using C++ models from QML.

    It currently isn't possible to populate a QAbstractItemModel from QML out-of-the-box -- you'll need to add your own custom methods/properties to support that (ListElement only works with ListModel).


  • I got it working, actually - it was several arcane bits which all needed to be used together. I'll put the solution up on a wiki page or some such once I get it into order. (And yes, it even works with ListElement!)

  • Cool stuff. That would be good to see.

  • Indeed! Looking forward to seeing your magic, Gordon!

Log in to reply