问题描述
我已经声明了 QObject 派生类型的 QML 可访问列表属性,并且在没有“const”的形式中它可以正常工作:
Q_PROPERTY(QQmlListProperty<QObjectDerived> items READ items NOTIFY updated)
但使用 'const' 修饰符:
Q_PROPERTY(QQmlListProperty<const QObjectDerived> items READ items NOTIFY updated)
使用第二种变体是否可行?
附言我正在使用所谓的常量传播,因此需要在列表中返回常量指针。
解决方法
您需要在主程序中调用 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");
}
};