如何为 Qt Quick Designer 模拟 C++ 枚举?
How to mock a C++ enum for the Qt Quick Designer?
我有一个这样定义的 C++ 枚举:
namespace SectionIdNamespace
{
Q_NAMESPACE
enum SectionId {
SomeValue
};
Q_ENUM_NS(SectionId)
};
我按如下方式注册该枚举:
qmlRegisterUncreatableMetaObject(
SectionIdNamespace::staticMetaObject,
"SectionIdImportName",
1, 0,
"SectionId",
"Error: only enums"
);
并在 QML 中使用它:
import SectionIdImportName 1.0
....
SectionId.SomeValue
在 Qt Quick Designer(Qt Creator 中的 "Design" 选项卡)中打开该 QML 文件时,它拒绝加载文件并显示 QML module not found (SectionIdImportName)
,因为 Designer 没有 运行 任何 C++ 代码。
如何让 Designer 使用使用 C++ 枚举的 QML 文件?
我知道 QML_DESIGNER_IMPORT_PATH
和 QML enumeration attributes。我试图通过这样的 QML 枚举来 "mock" 设计器的 C++ 枚举,但是,这些枚举的值是这样使用的 QMLType.EnumType.EnumValue
,而 C++ 枚举值必须只用 EnumType.EnumValue
.看起来代码可以与 C++ 枚举或 QML 枚举兼容,但不能同时兼容两者。
我正在使用 Qt 5.11,即将升级到 5.12。
我让它在运行时和设计器上工作,枚举封装在 class:
class SectionIdWrapper : public QObject
{
Q_OBJECT
public:
enum class SectionIdEnum {
SomeValue
};
Q_ENUM(SectionIdEnum);
};
并像这样注册:
qmlRegisterUncreatableType<SectionIdWrapper>("your.namespace", 1, 0, "SectionId", "Error: only enum");
Qml 中的用法如预期:
import your.namespace 1.0
Item {
property int test: SectionId.SomeValue
}
注意没有使用枚举的名称。
您可以在同一个 class 中添加更多枚举,但名称可能会发生冲突(域方面或文本方面)
我有一个这样定义的 C++ 枚举:
namespace SectionIdNamespace
{
Q_NAMESPACE
enum SectionId {
SomeValue
};
Q_ENUM_NS(SectionId)
};
我按如下方式注册该枚举:
qmlRegisterUncreatableMetaObject(
SectionIdNamespace::staticMetaObject,
"SectionIdImportName",
1, 0,
"SectionId",
"Error: only enums"
);
并在 QML 中使用它:
import SectionIdImportName 1.0
....
SectionId.SomeValue
在 Qt Quick Designer(Qt Creator 中的 "Design" 选项卡)中打开该 QML 文件时,它拒绝加载文件并显示 QML module not found (SectionIdImportName)
,因为 Designer 没有 运行 任何 C++ 代码。
如何让 Designer 使用使用 C++ 枚举的 QML 文件?
我知道 QML_DESIGNER_IMPORT_PATH
和 QML enumeration attributes。我试图通过这样的 QML 枚举来 "mock" 设计器的 C++ 枚举,但是,这些枚举的值是这样使用的 QMLType.EnumType.EnumValue
,而 C++ 枚举值必须只用 EnumType.EnumValue
.看起来代码可以与 C++ 枚举或 QML 枚举兼容,但不能同时兼容两者。
我正在使用 Qt 5.11,即将升级到 5.12。
我让它在运行时和设计器上工作,枚举封装在 class:
class SectionIdWrapper : public QObject
{
Q_OBJECT
public:
enum class SectionIdEnum {
SomeValue
};
Q_ENUM(SectionIdEnum);
};
并像这样注册:
qmlRegisterUncreatableType<SectionIdWrapper>("your.namespace", 1, 0, "SectionId", "Error: only enum");
Qml 中的用法如预期:
import your.namespace 1.0
Item {
property int test: SectionId.SomeValue
}
注意没有使用枚举的名称。
您可以在同一个 class 中添加更多枚举,但名称可能会发生冲突(域方面或文本方面)