Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose C++ structs for computations to Qml

I have the following problem.

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.

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

An example in code:

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

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

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

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?

like image 274
MattW Avatar asked Oct 16 '14 13:10

MattW


1 Answers

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.

So again, thanks for your help.

like image 87
MattW Avatar answered Nov 20 '22 02:11

MattW