QtScript 加枚举
我正在将 QScript 添加到我的 Qt 应用程序中。我已经添加了元数据并使用一些元数据函数来通过 C++ 代码进行查询。这工作得很好 - 我可以导航对象层次结构并打印出值(包括枚举)。
但是,我看不到让枚举在 Qt 脚本中工作。
我有我的类...
class HalPin : public QObject
{
Q_OBJECT
public:
enum EHalPinType
{
Bit = HAL_BIT,
Float = HAL_FLOAT,
S32 = HAL_S32,
U32 = HAL_U32
};
enum EHalPinDirection
{
In = HAL_IN,
Out = HAL_OUT,
IO = HAL_IO
};
Q_ENUMS(EHalPinType)
Q_ENUMS(EHalPinDirection)
public:
explicit HalPin(QObject *parent = 0);
signals:
public slots:
};
Q_DECLARE_METATYPE(HalPin::EHalPinType)
Q_DECLARE_METATYPE(HalPin::EHalPinDirection)
Q_DECLARE_METATYPE(HalPin*)
我有另一个类,它有一个将枚举作为参数的方法...
class EmcHal : public QObject
{
Q_OBJECT
public:
explicit EmcHal(QString moduleName, QObject *parent = 0);
signals:
public slots:
QObject *createHalPin( HalPin::EHalPinType, HalPin::EHalPinDirection, QString name );
};
该类在另一个类中公开 - 抱歉,我应该简化该示例。如果我编写以下 jscript 代码,
var nextPagePin1 = Emc.hal.createHalPin();
我会收到预期的错误...
SyntaxError: too few arguments in call to createHalPin(); candidates are createHalPin(HalPin::EHalPinType,HalPin::EHalPinDirection,QString)
因此,看来 qtscript 已知枚举类型。
我正在努力做的是从 jscript 设置枚举参数。我尝试过很多组合……
Bit
EHalPinType.Bit
HalPin.EHalPinType.Bit
还有更多。
如果我尝试使用整数,我会得到...
TypeError: cannot call createHalPin(): argument 1 has unknown type `HalPin::EHalPinType' (register the type with qScriptRegisterMetaType())
这似乎意味着 jscript 不知道我的枚举。
有什么建议吗?
我是否需要使用 qRegisterMetaType 或 qScriptRegisterMetaType 来访问我的枚举?该文档并不建议我需要这样做。我是否需要为 qScriptRegisterMetaType 方法实现转换器函数?
或者我的 jscript 语法是错误的?
有人有一个有效的例子吗?
谢谢, 坦率
I am adding QScript to my Qt application. I have already added metadata and use some of the metadata functions to interrogate through C++ code. That works fine - I can navigate the object heirarchy and print out values (including enums).
But, I can't seen to get enums working in Qt script.
I have my class...
class HalPin : public QObject
{
Q_OBJECT
public:
enum EHalPinType
{
Bit = HAL_BIT,
Float = HAL_FLOAT,
S32 = HAL_S32,
U32 = HAL_U32
};
enum EHalPinDirection
{
In = HAL_IN,
Out = HAL_OUT,
IO = HAL_IO
};
Q_ENUMS(EHalPinType)
Q_ENUMS(EHalPinDirection)
public:
explicit HalPin(QObject *parent = 0);
signals:
public slots:
};
Q_DECLARE_METATYPE(HalPin::EHalPinType)
Q_DECLARE_METATYPE(HalPin::EHalPinDirection)
Q_DECLARE_METATYPE(HalPin*)
I have another class that has a method that takes the enums as arguments...
class EmcHal : public QObject
{
Q_OBJECT
public:
explicit EmcHal(QString moduleName, QObject *parent = 0);
signals:
public slots:
QObject *createHalPin( HalPin::EHalPinType, HalPin::EHalPinDirection, QString name );
};
This class is exposed in another class - sorry I should have simplified the example. If I write the following jscript code,
var nextPagePin1 = Emc.hal.createHalPin();
I get an error I expect...
SyntaxError: too few arguments in call to createHalPin(); candidates are createHalPin(HalPin::EHalPinType,HalPin::EHalPinDirection,QString)
So, it appears that the enum types are known to qtscript.
What I am struggling to do is to set the enum arguments from jscript. I've tried many combinations...
Bit
EHalPinType.Bit
HalPin.EHalPinType.Bit
and many more.
If I try to use integers, I get...
TypeError: cannot call createHalPin(): argument 1 has unknown type `HalPin::EHalPinType' (register the type with qScriptRegisterMetaType())
which seems to imply jscript doesn't know about my enums.
Any suggestions?
Do I need to use qRegisterMetaType or qScriptRegisterMetaType to access my enums? The documentation doesn't suggest I need to do this. Do I need to implement the converter functions for the qScriptRegisterMetaType method.
Or is my syntax just wrong for the jscript?
Does someone have a working example?
Thanks,
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答我自己的问题...
好吧,与其说是为什么的答案,不如说是一个“嗯,这有效”的例子...
正如我上面提到的,我无法让枚举在元数据和jscript同时使用qt宏。即使枚举出现在 qscript 中(我在脚本调试器的浏览器中检查),它也没有计算出正确的整数。
我必须为枚举添加一个 QMetaObject。这给了我枚举项和正确的整数值。
但这仍然给了我未知类型错误,所以我需要使用 qScriptRegisterMetaType() 来注册类型的转换函数。
这是我用于 1 个枚举的类。这是我能做到的最小的。我应该能够使用宏将其缩小一点,但是由于 qt moc 的要求,可以宏化的内容存在限制。
我的 jscript 看起来像......
To answer my own question...
Well, not so much an answer to why, but a "meh, this works" example...
As I mentioned above, I wasn't able to get the enums working in both the metadata and jscript at the same time using the qt macros. Even though the enum appeared in qscript (I checked in the browser of the script debugger), it didn't evaluate to the correct integer.
I had to add a QMetaObject for the enum. That gave me the enum items, and correct integer values.
But that still gave me the unknown type error, so I needed to use qScriptRegisterMetaType() to register conversion functions for the types.
This is the class I use for 1 enum. It is as minimal as I can make it. I should be able to use macros to shrink it down a bit more, but there are limitations on what can be macroised, because of the qt moc requirements.
And my jscript looks like...
哎呀。我就抢了这个。尽管脚本有效,但我破坏了使用 qmetaobject 数据将枚举转换为字符串的能力。
而且似乎没有一种自动的方法可以做到这一点。
问题是,我将枚举移出了定义使用枚举的属性的类。尽管 Q_ENUMS 和 Q_PROPERTY 可以编译,但如果我使用 QMetaProperty 来读取枚举,则它不起作用。返回的 QVariant 显示正确的数据类型“CEHalPinType::EHalPinType”,但它未通过 isEnum() 测试,并且 canConvert(QVariant::String) 也失败。这是因为当 qmetaobject 代码搜索枚举类型时,它只查找当前类及其派生类。它不搜索其他类。这就是为什么当枚举是也具有这些属性的类的成员时它起作用的原因。
正如其他地方所建议的,我的工作是创建我自己的已知枚举的 QMap,将字符串名称存储到 qmetaobject 映射。我使用模板化基类并使用 T::staticMetaObject 来获取元对象。
Oops. I jumped the gun on this one. Although the scripting worked, I broke the ability to convert enums to strings using the qmetaobject data.
And there doesn't seem to be an automatic way of doing it.
The problem is, I moved the enums out of the class where the properties that used the enums were defined. Although the Q_ENUMS and Q_PROPERTY compile, if I use the QMetaProperty to to read an enum, it doesn't work. The QVariant that is returned shows the correct data type, "CEHalPinType::EHalPinType", but it fails the isEnum() test and canConvert(QVariant::String) fails too. This is because when the qmetaobject code goes searching for the enum type, it only looks in the current class and its derived classes. It doesn't search other classes. Which is why it worked when the enum was a member of the class which also had the properties.
My work around, as suggested elsewhere, was to create my own QMap of known enums, storing the string name to qmetaobject mapping. I used a templated base class and used T::staticMetaObject to get the meta object.