'QQmlListProperty<int>' is not registered?



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



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



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



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



  • 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



  • @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]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.