How to expose C++ structs for computations to Qml

后端 未结 1 1807
南笙
南笙 2021-02-13 09:12

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 offe

相关标签:
1条回答
  • 2021-02-13 10:01

    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.

    0 讨论(0)
提交回复
热议问题