Important: Please read the Qt Code of Conduct -

Contexting of an QAbstractListModel

  • Hello,

    I'm trying to use a QAbstractListModel for a MapItemView to delegate some custom coordinates on a map. I'm having problems setting the context for the model.

    I'm trying to set context for it in a QObject class I use in QML in a Q_INVOKABLE method:

     QQmlApplicationEngine engine;
            AdventureOnMapModel model;
            foreach (const QJsonValue & value, resourceInstance) {
                model.addAdventureOnMap(AdventureOnMap(adventureId, tagId, name, description, clue, award, geoLat, geoLong));
            engine.rootContext()->setContextProperty("AdventuresOnMapModel", &model);

    But with no luck, i get an error from QML

    ReferenceError: AdventuresOnMapModel is not defined

    When MapItemView tries to build it, although following qDebug output I see that the setContextProperty had executed before the error.

    What am i doing wrong?

  • Lifetime Qt Champion


    Did you register your model type with qmlRegisterType ?

  • @SGaist Hmm, I tried both registering the model and then setting the context for it in the Q_INVOKABLE, also tried changing the Q_INVOKABLE to return AdventureOnMapModel* and call it when declaring the model. In both cases there was no error, but the model seemed to be empty. The view is as:

    MapItemView {
            model: thisAdvendture.buildAdventuresOnMap()
    //this returns AdventureOnMapModel*
            delegate: MapQuickItem {
                coordinate: QtPositioning.coordinate(geoLat,geoLong)
                //Anhors pointer right in the middle of bottom
                anchorPoint.x: image.width * 0.5
                anchorPoint.y: image.height
                sourceItem: Column {
                    Image { id: image; source: "../Resources/marker.png" }
                    Text { text: name;
                            font.bold: true

    and I have the model header as:

    class AdventureOnMap
        AdventureOnMap(const int &adventureId, const int &tagId, const QString &name, const QString &desc, const QString &clue, const int &award, const double &geoLat, const double &geoLong);
        int     adventureId() const;
        int     tagId() const;
        QString name() const;
        QString desc() const;
        QString clue() const;
        int     award() const;
        double  geoLat() const;
        double geoLong() const;
        int     l_adventureId;
        int     l_tagId;
        QString l_name;
        QString l_desc;
        QString l_clue;
        int     l_award;
        double  l_geoLat;
        double  l_geoLong;
    class AdventureOnMapModel : public QAbstractListModel
        enum AdventureOnMapRoles {
            AdventureIdRole  = Qt::UserRole + 1,
        AdventureOnMapModel(QObject *parent = 0);
        void addAdventureOnMap(const AdventureOnMap &AdventureOnMap);
        int rowCount(const QModelIndex & parent = QModelIndex()) const;
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
        QHash<int, QByteArray> roleNames() const;
        QList<AdventureOnMap> l_adventuresOnMap;

    Am i accessing the data in the model wrong? am I delegating it wrong?

  • Moderators

    where is the code you've posted in your first post located?
    I am asking because it will only work if you have written this code in the main() for example.

    You are creating the model on the stack, thus it will get deleted once the control returns from the current method. And if that isn't the main() where you also start the event loop, your model will get deleted shortly after.

  • @raven-worx said:

    where is the code you've posted in your first post located?

    Yeah, the loop is not located in main, currently it's under a custom C++ QObject in a Q_INVOKABLE method that returns AdventureOnMapModel*.
    When defining the model for the MapItemView I pass this method for it.
    So if I understood correctly then the method returns ne model but, deletes it afterwards?
    What are my options here, to have the Model building logic execute in a custom QObject class and be available in QML?

  • Moderators

    create a pointer instead.
    And make sure it get's deleted appropriately. E.g. by setting a QObject parent.

  • Ahh, I got it! I added a Q_PROPERTY(AdventureOnMapModel*... On the C++ QObject im working with and I can get his model through this property. Thank you very much!

Log in to reply