14

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
MattW
  • 461
  • 3
  • 10
  • 1
    Did you try to use `Q_DECLARE_METATYPE` + `qmlRegisterMetatype`? Note that in this case you may pass values only via signals/slots. Because, to directly pass something to/from QML, it must be a `QObject`. Operators will not work inside QML, because you can't extend QML language :) – Dmitry Sazonov Oct 16 '14 at 14:39
  • 1
    You can wrap your instance as QVariant if you just need to pass it around (e.g you don't need to use it in the javascript expressions). – sergk Oct 17 '14 at 03:47
  • 1
    On the other hand if you need to actually use it in qml, you should derive from QObject or use QMap instead. – sergk Oct 17 '14 at 03:50

1 Answers1

4

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.

MattW
  • 461
  • 3
  • 10