久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 QML 訪問 C++ 枚舉?

How to access C++ enum from QML?(如何從 QML 訪問 C++ 枚舉?)
本文介紹了如何從 QML 訪問 C++ 枚舉?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

class StyleClass : public QObject {
public:
    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 派生的類中(并且您暴露給 QML):

You can wrap the enum in a class which derives from QObject (and that you expose to QML):

style.hpp :

style.hpp :

#ifndef STYLE_HPP
#define STYLE_HPP

#include <QtGlobal>
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
    // Qt 4
    #include <QDeclarativeEngine>
#else
    // Qt 5
    #include <QQmlEngine>
#endif

// Required derivation from QObject
class StyleClass : public QObject
{
    Q_OBJECT

    public:
        // Default constructor, required for classes you expose to QML.
        StyleClass() : QObject() {}

        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", 13, 37, "Style");
        }
};

#endif    // STYLE_HPP

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 13.37
import QtQuick 2.0    // Or 1.1 depending on your Qt version

Item {
    id: myitem

    //...

    property int item_style: Style.STYLE_RADIAL

    //...
}

這篇關于如何從 QML 訪問 C++ 枚舉?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 日本黄色一级 | 日本色视频 | 成人永久免费视频 | 国产日韩综合 | 欧美国产精品 | av天天看| av在线天堂网 | 久久久网站 | 91成人精品| 99久久综合 | 午夜黄色剧场 | 欧美性猛交xx | 国产草草影院 | 国产欧美一区二区精品忘忧草 | 美女久久久久 | 精品少妇 | 成人av一区| 四虎影视av | 日韩一级片在线观看 | 亚洲欧美另类在线 | 欧美香蕉视频 | 精品一区二区三区三区 | 日韩天堂在线 | 亚洲精品一二区 | 国产精品入口66mio男同 | 激情视频网 | 伊人久久综合 | 精品亚洲国产成人av制服丝袜 | 97福利视频 | 国产麻豆xxxvideo实拍 | 久久综合爱 | 免费在线黄色网址 | 成人午夜毛片 | 亚洲7777 | 欧美不卡视频 | 久久国内精品 | 久久精品一区二区三区不卡牛牛 | 在线观看视频一区二区三区 | 国产三级在线看 | 操久| 国产精品美女久久久 |