微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

QQmlListProperty<const QObjectDerived>注意'const':它以某种方式可行吗?

如何解决QQmlListProperty<const QObjectDerived>注意'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 端存在未注册类型错误

使用第二种变体是否可行?

附言我正在使用所谓的常量传播,因此需要在列表中返回常量指针。

解决方法

您需要在主程序中调用 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,"QObjectDerived");
    }
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。