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

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

问题描述

我有以下问题.

我正在开发一个 C++ 模型和一个 Qml 视图,通过控制器连接它们.在我的模型中,我执行多个计算.我还为我的应用程序的用户提供了编写自定义事件处理程序的可能性,用 qml 编写.现在我遇到了一个点,我决定使用定点表示法,并且我编写了一个相应的 C++ 类.现在我想向决定在 Qml 中扩展我的应用程序的开发人员提供 FixedPoint 类 - 包括它的所有运算符.到目前为止,我以 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.

所以问题来了:我怎样才能向 QML 公开一个 c++ 类/结构,这不是一个身份?

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 中使用它作为用 C++ 编写的 QQuickItem 的属性(值):

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

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

属性 value 然后在整个 javascript 计算中使用,并在整个过程中被复制多次.因此,我认为我不能将 value 设为 FixedPoint* 类型的属性.我说的对吗?

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?

推荐答案

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

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天全站免登陆