(注意'const'):它是否可行?,qt,qml,constants,qqmllistproperty"/>

首页 > 解决方案 > QQmlListProperty(注意'const'):它是否可行?

问题描述

我已经声明了 QObject 派生类型的 QML 可访问列表属性,并且在没有“const”的形式中它工作正常:

Q_PROPERTY(QQmlListProperty<QObjectDerived> items READ items NOTIFY updated)

但使用 'const' 修饰符:

Q_PROPERTY(QQmlListProperty<const QObjectDerived> items READ items NOTIFY updated)

QML 端存在未注册的类型错误。

以某种方式使用第二种变体是否可行?

PS 我使用的是所谓的 const-propagation,所以需要在列表中返回 const-pointers。

标签: qtqmlconstantsqqmllistproperty

解决方案


您需要在 main 中调用 Test::registerQml()

class Test : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QQmlListProperty<QObjectDerived> items READ items CONSTANT)

public:

    inline QQmlListProperty<QObjectDerived> items()
    {
        return QQmlListProperty<QObjectDerived>(this, data);
    }

    static void registerQml()
    {
        qmlRegisterType<QObjectDerived>("QObjectDerived", 1, 0, "QObjectDerived");
    }
};

推荐阅读