2016-12-07 24 views
2

は、私がここで私はC++ QObjectをから来る列挙クラス型あるプロパティをしたいのですが、別のQMLファイルに代理コンポーネントを持っています。 これは可能ですか?ここQMLコンポーネントの列挙型クラスのプロパティ

最小(非)実施例である:

card.h

#include <QObject> 

class Card : public QObject 
{ 
    Q_OBJECT 
public: 
    explicit Card(QObject *parent = 0); 

    enum class InGameState { 
     IDLE, 
     FLIPPED, 
     HIDDEN 
    }; 
    Q_ENUM(InGameState) 

private: 
    InGameState mState; 
}; 
Q_DECLARE_METATYPE(Card::InGameState) 

main.cppに

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include "card.h" 
int main(int argc, char *argv[]) 
{ 
    QGuiApplication app(argc, argv); 

    qmlRegisterType<Card::InGameState>("com.memorygame.ingamestate", 1, 0, "InGameState"); 
    QQmlApplicationEngine engine; 
    engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 

    return app.exec(); 
} 

testcard.qml

import QtQuick 2.0 
import com.memorygame.ingamestate 1.0 

Item { 
    property InGameState state 

    Rectangle { 
     id: dummy 
     width: 10 
    } 
} 

コンパイラエラー私は得る:

D:\Programs\Qt\Qt5.7.0\5.7\mingw53_32\include\QtQml\qqml.h:89: error: 'staticMetaObject' is not a member of 'Card::InGameState' const char *className = T::staticMetaObject.className(); \

enumクラスはQObjectではないので、このエラーが発生します。しかし、Q_ENUMマクロはMetaSystemでそれを利用できるようにすべきではありませんか?

私はこれを手伝ってもらえますか? enumクラスを削除してenumに変更し、qmlのintプロパティを使用できますが、私はC++ 11の機能を使いたいと思います。

答えて

2

documentation

To use a custom enumeration as a data type, its class must be registered and the enumeration must also be declared with Q_ENUM() to register it with Qt's meta object system.

によると、あなたはあなたのクラスCard代わりに列挙InGameState登録する必要がありますで、たとえば

The enumeration type is a representation of a C++ enum type. It is not possible to refer to the enumeration type in QML itself; instead, the int or var types can be used when referring to enumeration values from QML code.

qmlRegisterType<Card>("com.memorygame.card", 1, 0, "Card"); 

Additionallyをあなたの場合、列挙型はuでなければなりません次のようになります。

import QtQuick 2.0 
import com.memorygame.card 1.0 

Item { 
    property int state: Card.FLIPPED 

    Rectangle { 
     id: dummy 
     width: 10 
    } 
} 
+0

Hey Tarod!ご協力ありがとうございました! (実際にCardクラスを登録したのは私の実際のプロジェクトでしたが、元の投稿に言及しなかったのは残念です) 本当に大きな助けがあったのは、QMLは私のオリジナルであったenumクラスのカスタムプロパティタイプ(偽)考え。 私はint型とvar型をsignal-slotsで使用しましたが、それぞれ受信側ではint型とQVariant型のパラメータを使用しました。 – speter

+0

@speter素晴らしい!おかげさまで。ハッピーコーディング:) – Tarod

関連する問題