如何将计算的C ++结构暴露给Qml [英] How to expose C++ structs for computations to Qml

查看:121
本文介绍了如何将计算的C ++结构暴露给Qml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我想解决。

I have the following problem, that I want to solve.

我正在开发一个C ++模型和一个View在Qml,通过控制器连接它们。在我的模型中,我执行多个计算。我也为我的应用程序的用户提供了可能性,用qml编写自定义事件处理程序。
现在我碰到一个点,在那里我决定使用固定点符号,我写了一个相应的C ++类。现在我想提供FixedPoint类 - 包括所有的运营商 - 开发人员,谁决定扩展我的应用程序在Qml。到目前为止,我提供了所有的数据作为QProperties,这是编码指南需要。但我开放其他解决方案,在我的团队讨论他们。
显然,一个固定点是没有身份,算法依赖于复制它的可能性,这是从QObject继承时是不允许的。

I am developing a model in C++ and a View in Qml, connecting them via Controllers. In my model I perform multiple calculations. I also offer users of my application the possibility, to write custom event handlers, written in qml. Now I came across a point, where I decided to use Fixed point notation and I have written a corresponding C++ class. Now I want offer the FixedPoint class - including all its operators - to developers, who decide to extend my application in Qml. So far, I offered all data as QProperties, which is required by coding guidelines. But I am open for other solutions to discuss them in my team. Clearly, a fixed point is no identity and algorithms rely on the possibility of copying it, which is not allowed when inheriting from QObject.

:我怎么暴露一个c ++类/结构到QML,这不是一个身份?

So the question arrives: How can I expose a c++ class / struct to QML, which is NOT an identity?

代码中的示例:

struct FixedPoint
{
    FixedPoint(FixedPoint&);
    FixedPoint& operator=(FixedPoint&);
    ...
    int mantissa;
    int exponent;
}



我想在Qml中使用它作为QQuickItem的属性用C ++编写:

I want to use it in Qml as an property (value) of a QQuickItem written in C++:

MyQmlObject{
    value{ mantissa: 134; exponent: 3 }
}

属性在javascript中用于整个计算,并被长期复制几次。所以我不能使值类型的固定点的属性*我想。

The property value is then used throughout computations in javascript and is copied several times a long the way. So I cannot make value a property of type FixedPoint* I think. Am I right?

我非常感谢您能提供的任何见解。

I will be grateful for any insights you can offer.

来自德国的问候


Greetings from Germany

推荐答案

感谢您的帮助。
我们决定使用QObject反正,但在Wrapper-manner。也就是说,我们只是构建了一个FixPointWrapper(继承QObject),它保存了一个实际的FixedPointValue。那可以用于计算。
听起来很复杂,但工作正常。对我们来说,特别重要的是复制和分配固定点值的可能性。因此,需要包装器。

Thanks for your help. We decided to use QObject anyway, but in Wrapper-manner. That is, we just built a FixPointWrapper (inheriting QObject), which holds an actual FixedPointValue. That can be used for computations then. Sounds complicated, but works fine. Especially important for our sakes is the possibility, to copy and assign FixedPoint values. Thus, the wrapper is needed.

//QML
MyQmlObject{
    value {mantissa: 2; exponent: 4}
}


//C++
class MyQmlObject : public QQuickItem
{
    Q_Property( FixedPointWrapper* value ... )
}

class FixedPointWrapper : public QObject
{
    ...
    public slots:
       void setValue(FixedPoint){ //Forward to FixedPoint and implement your wanted effect. The same for getter} 
    private:
    FixedPoint value;    
}

在开始的时候感觉像是一个脏的黑客,更多的想法,我们可以生活的结果。

In the beginning it felt like a dirty hack, but after we spent a few more thoughts, we can live with the result.

再次感谢您的帮助。

这篇关于如何将计算的C ++结构暴露给Qml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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