QML 组件枚举 class 属性
QML component enum class property
我在单独的 qml 文件中有委托组件,我想在其中有一个 属性是一个 enum class 类型 来自 来自 c++ QObject。
这可能吗?
这是一个最小(非)工作示例:
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.7\mingw53_32\include\QtQml\qqml.h:89: error:
'staticMetaObject' is not a member of 'Card::InGameState'
const char *className = T::staticMetaObject.className(); \
枚举 class 不是 QObject,这就是我收到此错误的原因,对吧?但是 Q_ENUM 宏不应该让它在 MetaSystem 中可用吗?
你能帮我解决这个问题吗?我可以删除枚举 class,并将其更改为枚举,并在 qml 中使用 int 属性,但我想使用 c++11 功能。
根据 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.
所以你需要注册你的 class Card
而不是枚举 InGameState
:
qmlRegisterType<Card>("com.memorygame.card", 1, 0, "Card");
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.
例如,在您的情况下,枚举应按如下方式使用:
import QtQuick 2.0
import com.memorygame.card 1.0
Item {
property int state: Card.FLIPPED
Rectangle {
id: dummy
width: 10
}
}
我在单独的 qml 文件中有委托组件,我想在其中有一个 属性是一个 enum class 类型 来自 来自 c++ QObject。 这可能吗?
这是一个最小(非)工作示例:
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.7\mingw53_32\include\QtQml\qqml.h:89: error: 'staticMetaObject' is not a member of 'Card::InGameState' const char *className = T::staticMetaObject.className(); \
枚举 class 不是 QObject,这就是我收到此错误的原因,对吧?但是 Q_ENUM 宏不应该让它在 MetaSystem 中可用吗?
你能帮我解决这个问题吗?我可以删除枚举 class,并将其更改为枚举,并在 qml 中使用 int 属性,但我想使用 c++11 功能。
根据 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.
所以你需要注册你的 class Card
而不是枚举 InGameState
:
qmlRegisterType<Card>("com.memorygame.card", 1, 0, "Card");
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.
例如,在您的情况下,枚举应按如下方式使用:
import QtQuick 2.0
import com.memorygame.card 1.0
Item {
property int state: Card.FLIPPED
Rectangle {
id: dummy
width: 10
}
}