Read-only property error when using QQmlListProperty with a custom class in QML

I follow the qt-project.org example to expose a custom type as QQmlListProperty in QML. At runtime, when initializing a list property, I get an error Invalid property assignment: "overlayImages" is a read-only property.

This is what I want to do in QML:

TestPattern {
    id: pattern

    overlayImages: [
        OverlayImage {
            x: 100
            y: 100
            alpha: 0.1
            source: "../../images/fail.png"
            visible: true
        }
    ]
}

Here are my custom type declarations:

class TestPatternQuickPrivate;
class QMLTESTPATTERN_EXPORT TestPatternQuick : public QQuickItem
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(TestPatternQuick)
    Q_DISABLE_COPY(TestPatternQuick)
    Q_PROPERTY(QQmlListProperty<OverlayImage> overlayImages READ overlayImages)

public:
    TestPatternQuick(QQuickItem *parent = Q_NULLPTR);
    virtual ~TestPatternQuick();    
    QQmlListProperty<OverlayImage> overlayImages();

private:
    static void appendOverlayImage(QQmlListProperty<OverlayImage> *list, OverlayImage *overlayImage);
    QScopedPointer<TestPatternQuickPrivate> d_ptr; //QScopedPointer guarantees no throw
};

class OverlayImagePrivate;
class QMLTESTPATTERN_EXPORT OverlayImage : public QQuickItem
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(OverlayImage)

public:
    OverlayImage(QQuickItem *parent = Q_NULLPTR);
    OverlayImage(const OverlayImage &other);
    virtual ~OverlayImage();
    OverlayImage &operator=(const OverlayImage &other);

private:
    QScopedPointer<OverlayImagePrivate> d_ptr; //QScopedPointer guarantees no throw
};

... and implementation of C ++ plugin registration:

void QmlTestPatternPlugin::registerTypes(const char *uri)
{
    qmlRegisterType<TestPatternQuick>(uri, 1 /*major*/, 0 /*minor*/, "TestPattern");
    qmlRegisterType<OverlayImage>(uri, 1 /*major*/, 0 /*minor*/, "OverlayImage");
}

Can anyone shed some light on what I'm still missing?

+4
source share

All Articles