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?
-
Hi,
Did you register your model type with qmlRegisterType ?
-
Hi,
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 { public: 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; private: 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 { Q_OBJECT public: enum AdventureOnMapRoles { AdventureIdRole = Qt::UserRole + 1, TagIdRole, NameRole, DescRole, ClueRole, AwardRole, GeoLatRole, GeoLongRole }; 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; protected: QHash<int, QByteArray> roleNames() const; private: QList<AdventureOnMap> l_adventuresOnMap; };
Am i accessing the data in the model wrong? am I delegating it wrong?
-
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?
@Verhoher
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.
-
@Verhoher
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 aQ_INVOKABLE
method that returnsAdventureOnMapModel*
.
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 customQObject
class and be available in QML? -
@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 aQ_INVOKABLE
method that returnsAdventureOnMapModel*
.
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 customQObject
class and be available in QML?@Verhoher
create a pointer instead.
And make sure it get's deleted appropriately. E.g. by setting a QObject parent.