如何从QML访问C ++枚举? [英] How to access C++ enum from QML?

查看:1587
本文介绍了如何从QML访问C ++枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef enum
    {
        STYLE_RADIAL,
        STYLE_ENVELOPE,
        STYLE_FILLED
    }  Style;

Style m_style;

.h文件具有上述代码。 如何通过QML访问上述枚举?

The .h file has the above code. How to access the above enum through QML?

推荐答案

源自QObject:

style.hpp:

style.hpp :

#ifndef STYLE_HPP
#define STYLE_HPP

#include <QObject>

#if QT_VERSION < 0x050000
    // Qt 4.x.x
    #include <QtDeclarative>
#else
    // Qt 5.x.x
    #include <QtQml>
#endif

class StyleClass : public QObject
{
    Q_OBJECT

    public:
        enum EnStyle
        {
            STYLE_RADIAL,
            STYLE_ENVELOPE,
            STYLE_FILLED
        };
        Q_ENUMS(EnStyle)

        // Do not forget to declare your class to the QML system.
        static void declareQML() {
            qmlRegisterType<StyleClass>("MyQMLEnums", 1, 0, "Style");
        }
};

#endif    // STYLE_HPP

main.cpp:

main.cpp:

#include <QApplication>
#include "style.hpp"

int main (int argc, char ** argv) {
    QApplication a(argc, argv);

    //...

    StyleClass::declareQML();

    //...

    return a.exec();
}

QML代码:

import MyQMLEnums 1.0
import QtQuick 2.0    // Or 1.1 depending on your Qt version

Item {
    id: myitem

    //...

    property int item_style: Style.STYLE_RADIAL

    //...
}

这篇关于如何从QML访问C ++枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆