Using ListModel inside C++ class.

  • I !
    I can see there are several documentation about using a C++ model from within a .qml file.
    But I fail to found any 'reverse' sample :
    I'm creating a new Item by extending a QDeclarativeItem class, my item have a QVariant 'model' property.
    I'm able to import such item in a qml file and set the model property to a ListModel.
    What I can see debugging the write 'model' method from C++ code, is that model property is a: QVariant(QObject*, QDeclarativeListModel(0x10265aa70) )

    What I Can do with QDeclarativeListModel class ? which seems to be private part of QtDeclarative.
    (not included by "#include <QtDeclarative>", I failed to find any .h file for that class, the only reference I found on the web is a .../src/declarative/util/private/qdeclarativelistmodel_p_p.h)

    Is there a way to get the content of a ListModel from C++ source code ?
    (number of ListElement, access to role defined in list element, ...)

    Any example or suggestion is welcome, thanks a lot !

  • Hi,

    Unfortunately it isn't really possible to use this class from C++ right now (it is based on the new list model interface developed for the itemviews-ng project, and is still private). This isn't ideal, and at some point all of our models should start using a public model class (either by making the new model interfaces public, or converting our models to use QAbstractItemModel).


  • Hi, arno

    You need one example , then see the below link :

    Best regards,

  • [quote author="qtrahul" date="1292917569"]Hi, arno

    You need one example , then see the below link :

    Best regards,[/quote]
    How does that help the OP to access that model from C++?

  • Really bad news to know it's not possible right now. I hope the ListModel and friends classes will be available at public level of QtQuick in next few release.

    @Andre: my purpose is to create a new visual item that behave like a popup menu", to compute size and to draw more complex shape than rectangle I need to create my own C++ class (like PieSlice example), but I want my menu to be populated by .qml side using : ListModel with element defining 'label' and 'id' roles (ie when user select a text the id is fired by a signal) and store in a PopupMenu.model property ... exactly as ListView does.

    Create that property as a QVariant works, assign it to a ListModel works, but debugging C++ side of my Item I can see the ListModel uses QDeclarativeListModel internal (private) class as qtrahul said.

    As a workaround for now, I create a C++ class for only the shape of my popup menu, and create a .qml that define texts and id. Which is strongly linked to the usage of this popupmenu in my qmlApp ... far from a reusable popupmenu component.

  • What shape is your pop-up menu by the way? You can achieve any shape using images. Is it that you want a non-rectangular mouseArea for the clicks? If so, yeah only C++ for that.

    Is there anything wrong with having the qml file define texts and ids? What do you need the C++ to do in this case? I have a log program that reads a text file and generates a list in C++ and passes it as a model in QML. The QML then interacts with the model and passes updated model back to the C++. Not sure if this is what you want.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.