Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. 'QQmlListProperty<int>' is not registered?
Forum Updated to NodeBB v4.3 + New Features

'QQmlListProperty<int>' is not registered?

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 5.6k Views 1 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.
  • C Offline
    C Offline
    cesupa
    wrote on last edited by
    #1

    I want to use QQmlListProperty to access list-values in QML. The class where I register the list with

    @ Q_PROPERTY(QQmlListProperty<int> items READ items)
    @

    is a singleton class. I register it with

    @ qmlRegisterSingletonTypeAppointment::MVC::ApModel("appointment.model.apmodel",1,0,"ApModel",Appointment::MVC::apModelSingletonTypeProvider);
    @

    in my main.cpp. The usage of other properties works fine, but when I want to access the list, QML prints out the following error:

    QMetaProperty::read: Unable to handle unregistered datatype 'QQmlListProperty<int>' for property 'Appointment::MVC::ApModel::items'

    Did I forget something?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cesupa
      wrote on last edited by
      #2

      I have done some tests with a little example class:

      @#include <QObject>
      #include <QQmlListProperty>

      class ListItem : public QObject
      {
      Q_OBJECT
      Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
      public:
      ListItem();

      QString name() const;
      void setName(const QString &name);
      

      signals:
      void nameChanged();

      private:
      QString _name;
      };

      class QmlListTest : public QObject
      {
      Q_OBJECT
      Q_PROPERTY(QQmlListProperty<ListItem> items READ items)

      public:
      explicit QmlListTest(QObject *parent = 0);

      QQmlListProperty<ListItem> items()
      {
          return QQmlListProperty<ListItem>(this,_items);
      }
      

      signals:

      public slots:

      private:
      QList<ListItem*> _items;

      };@

      @#include <QApplication>
      #include <QQmlApplicationEngine>
      #include <QtQml>
      #include "qmllisttest.h"

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      QQmlApplicationEngine engine;
      
      qmlRegisterType<QmlListTest>("qmllisttest",1,0,"List");
      qmlRegisterType<ListItem>("qmllisttest",1,0,"ListItem");
      
      engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
      
      return app.exec();
      

      }@

      @import QtQuick 2.2
      import QtQuick.Controls 1.1
      import qmllisttest 1.0

      ApplicationWindow {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")

      property ListItem item: List.items[0]
      
      Text {
          text: qsTr(item.name)
          anchors.centerIn: parent
      }
      

      }@

      That's the way how I want to access the list in QML. But somehow the List.items property is undefined in QML:

      @
      qrc:///main.qml:12: TypeError: Cannot read property '0' of undefined
      qrc:///main.qml:15: TypeError: Cannot read property 'name' of null
      @

      Is there another way to access a list generated in C++ in QML like above?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cesupa
        wrote on last edited by
        #3

        Okay, I have found the solution:

        In my example class I had to set an instance of the class in the root context as property of course.

        In my singleton class I just had to add the namespaces in the Q_PROPERTY declaration.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lunamystry
          wrote on last edited by
          #4

          Hi, I have the same error and I don't understand what the quoted text means. I would like the lifetime of my Object to be controlled by qml and it is not a singleton.

          [quote author="cesupa" date="1402306240"]Okay, I have found the solution:

          In my example class I had to set an instance of the class in the root context as property of course.

          In my singleton class I just had to add the namespaces in the Q_PROPERTY declaration. [/quote]

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chrisadams
            wrote on last edited by
            #5

            Hi,

            Instead of using QQmlListProperty, can you simply define a Q_PROPERTY of the QList<int> type? You should still be able to access elements of the sequence via subscript operator / index, and the metatypes should definitely be registered by the engine.

            Thanks,
            Chris.

            http://www.qinetic.com.au/ - The Qt And QML User Experience Specialists

            1 Reply Last reply
            1
            • L Offline
              L Offline
              lunamystry
              wrote on last edited by
              #6

              @chrisadams that works thank you. After reading your post, I realise that it is actually mentioned in the docs: http://qt-project.org/doc/qt-5/qtqml-cppintegration-exposecppattributes.html

              [quote]
              Properties containing lists of QObject-derived types can also be exposed to QML. For this purpose, however, one should use QQmlListProperty rather than QList<T> as the property type.
              [/quote]
              I was not reading the docs in enough detail to see on my first 5 readings :-}

              [quote author="chrisadams" date="1411524542"]Hi,

              Instead of using QQmlListProperty, can you simply define a Q_PROPERTY of the QList<int> type? You should still be able to access elements of the sequence via subscript operator / index, and the metatypes should definitely be registered by the engine.

              Thanks,
              Chris.

              http://www.qinetic.com.au/ - The Qt And QML User Experience Specialists[/quote]

              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