"只读属性"在QML中使用QQmlListProperty和自定义类时出错

时间:2014-09-03 22:10:54

标签: c++ qt qml

我正在关注qt-project.org的示例,以在QML中将自定义类型公开为QQmlListProperty。在运行时,初始化list属性时,我收到错误Invalid property assignment: "overlayImages" is a read-only property

这就是我想在QML中做的事情:

TestPattern {
    id: pattern

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

以下是我的自定义类型声明:

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

...和C ++插件注册实现:

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

任何人都可以了解我仍然缺少的东西吗?

0 个答案:

没有答案