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. QAbstractListModel get method
Qt 6.11 is out! See what's new in the release blog

QAbstractListModel get method

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.9k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by
    #1

    Hello,
    I implemented templated QAbstractListModel with get method which I want to be accessible from QML:

    template <class T> class ListModel : public QAbstractListModel
    {
    // ...
    public:
        Q_INVOKABLE T *get(int index) {
            return m_objects[index];
        }
    // ...
    private:
        QList<T *> m_objects;
    // ...
    }
    

    I instantiate a list in C++, register it, expose it and use it in QML:

    Q_PROPERTY(ListModel<MyData> *myDataList READ myDataList CONSTANT)
      ListModel<MyData> *myDataList (void) {
        return &m_dataList;
      }
    ListModel<MyData> m_dataList;
    Q_DECLARE_METATYPE(ListModel<MyData>*)
    qRegisterMetaType<ListModel<MyData>*>();
    qmlRegisterType<ListModel<MyData>>("xyz", 1, 0, "ListModelMyData");
    
    

    However, when I call in QML:

    myDataList .get(0)
    

    I get:

    qrc:/MyComponent.qml:1: TypeError: Property 'get' of object QAbstractListModel(0xc26748) is not a function
    

    Can you please help?

    raven-worxR 1 Reply Last reply
    0
    • M MartinD

      Hello,
      I implemented templated QAbstractListModel with get method which I want to be accessible from QML:

      template <class T> class ListModel : public QAbstractListModel
      {
      // ...
      public:
          Q_INVOKABLE T *get(int index) {
              return m_objects[index];
          }
      // ...
      private:
          QList<T *> m_objects;
      // ...
      }
      

      I instantiate a list in C++, register it, expose it and use it in QML:

      Q_PROPERTY(ListModel<MyData> *myDataList READ myDataList CONSTANT)
        ListModel<MyData> *myDataList (void) {
          return &m_dataList;
        }
      ListModel<MyData> m_dataList;
      Q_DECLARE_METATYPE(ListModel<MyData>*)
      qRegisterMetaType<ListModel<MyData>*>();
      qmlRegisterType<ListModel<MyData>>("xyz", 1, 0, "ListModelMyData");
      
      

      However, when I call in QML:

      myDataList .get(0)
      

      I get:

      qrc:/MyComponent.qml:1: TypeError: Property 'get' of object QAbstractListModel(0xc26748) is not a function
      

      Can you please help?

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

      @MartinD said in QAbstractListModel get method:

      template <class T> class ListModel : public QAbstractListModel
      {
      // ...
      public:

      did you maybe miss the Q_OBJECT macro?

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

      M 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @MartinD said in QAbstractListModel get method:

        template <class T> class ListModel : public QAbstractListModel
        {
        // ...
        public:

        did you maybe miss the Q_OBJECT macro?

        M Offline
        M Offline
        MartinD
        wrote on last edited by
        #3

        @raven-worx said in QAbstractListModel get method:

        Q_OBJECT

        Yes, I missed that. But it seems there is another problem - template classes don't support Q_OBJECT. I think some workaround will be needed...

        mrjjM 1 Reply Last reply
        0
        • M MartinD

          @raven-worx said in QAbstractListModel get method:

          Q_OBJECT

          Yes, I missed that. But it seems there is another problem - template classes don't support Q_OBJECT. I think some workaround will be needed...

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MartinD
          Hi
          I dont think template classes and the moc tool can ever work.
          There was talk about it on mailing lists
          http://lists.qt-project.org/pipermail/interest/2016-February/021029.html
          (Thanks to mr @kshegunov )
          But it seems that the code changes to allow it was never integrated/moved along.

          1 Reply Last reply
          2

          • Login

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