How to access C++ enum from QML?

后端 未结 7 641
清歌不尽
清歌不尽 2020-12-04 18:03
class StyleClass : public QObject {
public:
    typedef enum
        {
            STYLE_RADIAL,
            STYLE_ENVELOPE,
            STYLE_FILLED
        }  Styl         


        
7条回答
  •  离开以前
    2020-12-04 18:21

    All this solutions can't enabled used this enum-class as parameter for signal/slot. This code compile, but not work in QML:

    class DataEmitter : public QObject
    {
        Q_OBJECT
    
    public:
        ...
    signals:
        void setStyle(StyleClass::EnStyle style);
    }
    
    ...
    
    emit setStyle(StyleClass.STYLE_RADIAL);
    

    QML-part:

    Connections {
        target: dataEmitter
        onSetStyle: {
             myObject.style=style
        }
    }
    

    And this code generate runtime error, as this:

    IndicatorArea.qml:124: Error: Cannot assign [undefined] to int
    

    For this code working, you must additional registry Qt metaobject type:

    qRegisterMetaType("StyleClass.EnStyle");
    

    More details written here: https://webhamster.ru/mytetrashare/index/mtb0/1535044840rbtgvfmjys (rus)

提交回复
热议问题