Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Contexting of an QAbstractListModel
QtWS25 Last Chance

Contexting of an QAbstractListModel

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VerhoherV Offline
    VerhoherV Offline
    Verhoher
    wrote on last edited by
    #1

    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?

    raven-worxR 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you register your model type with qmlRegisterType ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      VerhoherV 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Did you register your model type with qmlRegisterType ?

        VerhoherV Offline
        VerhoherV Offline
        Verhoher
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • VerhoherV Verhoher

          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?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          VerhoherV 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @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.

            VerhoherV Offline
            VerhoherV Offline
            Verhoher
            wrote on last edited by
            #5

            @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?

            raven-worxR 1 Reply Last reply
            0
            • VerhoherV Verhoher

              @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?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

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

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • VerhoherV Offline
                VerhoherV Offline
                Verhoher
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved