QQmlListProperty已转换为不透明的QVariant
QQmlListProperty converted to opaque QVariant
我的QQmlListProperty有问题。
我的类返回一个QQmlListProperty列表。我调用一个c++类的qml中的函数,该函数返回相应的列表。但当我尝试访问列表时,比如list.lenght,我会得到一个未定义的javascript对象。
C++侧面
class IControllerInterface {
...
public:
QQmlListProperty<IObject> objectsQml() {
return QQmlListProperty<IObject>(this,
this,
&IController::qmlListAppend,
&IController::qmlListCount,
&IController::qmlListAt,
&IController::qmlListClear);
}
private:
static void qmlListAppend(QQmlListProperty<IObject> *list, IObject *object);
static IObject *qmlListAt(QQmlListProperty<IObject> *list, int index);
static int qmlListCount(QQmlListProperty<IObject> *list);
static void qmlListClear(QQmlListProperty<IObject> *list);
...
}
void IController::qmlListAppend(QQmlListProperty<IObject> *list, IObject *object)
{
}
IObject *IController::qmlListAt(QQmlListProperty<IObject> *list, int index)
{
return reinterpret_cast< IController* >(list->data)->objects()[index];
}
int IController::qmlListCount(QQmlListProperty<IObject> *list)
{
return reinterpret_cast< IController* >(list->data)->objects().size();
}
void IController::qmlListClear(QQmlListProperty<IObject> *list)
{
}
class RestAPI {
...
Q_INVOKABLE QQmlListProperty<IObject> lookupObjectsQml(const IObject::ObjectType type) {
Q_D(RestAPI);
return d->getController(type)->objectsQml();
}
...
}
*注意:RestAPI是一个单例类
QML侧面
RestControl {
id: ctrl
Component.onCompleted: {
var lst = RestAPI.lookupObjectsQml(IObject.FARM)
console.log(lst)
console.log('found ' + lst.length + ' objects..')
for(var obj in lst) {
console.log(obj.id)
}
}
}
当我调用RestAPI.lookupObjectsQml(IObject.FARM)并将结果分配给变量lst时。。我收到一个不透明的qvariant对象:
QVariant(QQmlListProperty)
这样我就无法访问我的对象。
例如,此调用将返回未定义的:lst.length
我做错了什么?
调试05-01-19 10:42:49:889[GUI][root]QVariant(QQmlListProperty)
调试05-01-19 10:42:49:890[GUI][root]发现未定义的对象。。
类型QQmlListProperty
应该作为属性本身公开给QML:
Q_PROPERTY(QQmlListProperty<IObject> objects READ objects)
为了正确使用这种类型,QML代码中JavaScript的语义应该是:
RestAPI.lookupObjectsQml(IObject.FARM);
for(var obj in RestAPI.objects) {
console.log(obj.id)
}
您的代码的其余部分表明,您将能够用C++完成这样的更改。但当然,您可以找到许多其他方法[例如]来根据需要提供函数调用中的对象列表,但当QQmlListProperty完全用作QML属性时,就不能这样了。
相关文章:
- 如何创建子窗口是透明的,父窗口是不透明的?
- 从静态库使用时隐藏不透明结构的内容
- 对于指向C++类的不透明C指针,正确的typedef是什么
- 使用 pybind11 混合类型转换和不透明类型
- 处理 pybind11 中的不透明指针
- Pybind11在不透明载体的不透明载体上访问
- SWIG共享_ptr会导致不透明的对象
- 具有 C 链接和C++实现的不透明结构
- 为什么C++关联容器谓词默认不透明
- QQmlListProperty已转换为不透明的QVariant
- 检查不透明的C句柄是否可以
- 提高不透明类型的序列化
- 如何处理JNA中的不透明指针
- 如何在c++绑定中用不透明指针包装c库
- 不透明和匿名枚举声明如何符合标准的要求?
- 在 C++11 中使用 decltype() 时出错(在 gcc 4.7.0 中创建不透明的错误消息)
- QGraphicsScene - 添加带有少量不透明点的大透明像素图时,应用程序会挂起很长时间
- 不透明引用而不是 PImpl.可能吗
- 如何在 SDL2 中的不透明矩形之上绘制透明或半透明矩形
- 指向shared_ptr的不透明类型 C 指针