任何机会使用非QObject类与QML [英] Any chance to use non QObject classes with QML

查看:189
本文介绍了任何机会使用非QObject类与QML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将C ++类型的属性公开给QML 类使用与QML必须 QObject s。任何机会,我可以使用非 QObjects s(aka POCO,不是从 QObject ,但注册与Qt元系统)?

As of Exposing Attributes of C++ Types to QML classes used with QML have to be QObjects. Any chance I can use non QObjects s (aka POCO, not derived from QObject, but registered with Qt metasystem)?

如果没有,是否有一个简单的通用包装系统,使我的对象符合QML。我可以想到的是添加动态属性到一个简单的 QObject

If not, is there a simple generic wrapping system to make my objects QML compliant. One I can think of is to add dynamic properties to a simple QObject.

还是有一种隐式转换为QML兼容类型的方法,所以我不需要换行?

Or is there a way to implicitly convert to a QML compliant type so I do not need to wrap at all?

推荐答案

这是一个热门话题。

我相信你可以注册自己的POD,并在ito和QML端传递它们(就像黑盒子 - 没有任何成员访问,从未尝试过那)。要访问成员,可以使用一些get / set包装器代码,无论是在QML单例上的方法形式,还是在有效充当每个实例包装器的QtObject后代。

I believe you may register your own PODs and pass them around ito and within the QML side (just as black boxes -- w/o any member access, have never tried that). To access the members, some get/set wrapper code could be used, either in form of methods on a QML singleton, or on a QtObject descendant effectively serving as a per-instance wrapper.

动态属性目前不受支持 - 您可以使它们可能带有一些非常奇怪的诡计,但它不可能是值得的(但如果你不想绑定属性,事情会变得更简单,它仍然会是QObjects)。

Dynamic properties are not currently supported -- you can make them work probably with some quite weird trickery, but it will not probably be worth it (but if you don't want to bind to properties, things will get much-much simpler, and it still will be QObjects).

隐式转换将意味着有一些类型的预处理器大量的时间,以及不满的将结果提交回Qt(并支持它的生活)。

Implicitly converting would mean having some sort of preprocessor (which is doable probably but would cost lots of time, and a sudecution to submit the result back into Qt (and support it for life)).

我会这样走:


  • 如果对象可以是QObject,测量性能(如果可以,请坚持下去)

  • if

  • 如果不需要绑定,请尝试通过值传递不透明POD,如果有效,则创建包装脚手架,并查看其是否更快/和动态,调查a)嵌套的QVariants b)动态QObject属性

  • 如果应该满足极端的速度/ mem需求,考虑编写一个工具,自动生成包装器, Qt项目构建管道

  • if objects in question can be QObjects, measure the performance (and if it's ok, stick with it)
  • if not, attempt passing opaque PODs by value, if it works, create wrapper scaffolding, and see if it's faster / gives better memory usage than the previous option
  • if no bindings to proerties are needed, and dynamism is required, investigate a) nested QVariants b) dynamic QObject properties
  • if extreme speed / mem requirements should be met, consider writing a tool that auto-generates wrappers and hooking it into the Qt project build pipeline

这就是说,自定义POD不能享受与内置do相同级别的支持,语言实践是围绕操作垃圾收集的QObject实例(当然,通过指针传递的所有地方),它们已经监视类型的属性:

Thing is, custom PODs don't enjoy the same level of support the built-in do, and the standard language practices are built around manipulating garbage-collected QObject instances (passed everywhere by pointer, of course), which have watched-for-changes properties of types:


  • POD

  • 清单

  • QObject


  • POD
  • list
  • QObject
  • variant (not watched for member changes, iirc)

这篇关于任何机会使用非QObject类与QML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆