qt - 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。
解决方案
您需要在 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");
}
};
推荐阅读
- javascript - 删除字符串javascript中的连续重复字符
- c# - 在 Unity 中冻结某个动画的旋转问题
- docker - Kiwi TCMS 的生产版本 - GUI 不在本地主机上
- bash - 在bash中读取文件并同时(逐行)编辑文件
- python - python全局命名空间无法访问
- mule - 我正在尝试骡子训练演练 6-3 并获得不同的输出
- mysql - 使用 if 条件或 CASE 连接 4 个表的 Sql Query
- javascript - 使用 PHP 将 HTML 值传递给 url
- javascript - 来自json的D3颜色十六进制
- sql - 如何根据另一个查询的结果评估一个查询?