QtScript:将对象数组传递给 C++
我想将一个对象数组从 QtScript 传递到 C++,但我无法弄清楚如何实现这一点。一旦我创建了一个数组,其中的元素就会被转换为字符串,然后我才能访问它们。
这是我到目前为止一直在尝试的:
class myObject : public QObject, public QScriptable
{
Q_OBJECT
public Q_SLOTS:
void test(QVariantList list);
};
void myObject::test(QVariantList list)
{
for (QVariantList::const_iterator it = list.begin(); it != list.end(); ++it) {
QVariant element = *it;
qDebug() << element.typeName() << element.toString();
if (element.canConvert<QVariantMap>()) {
// Not getting here
}
}
}
以下脚本
myObject.test([{"foo": 1, "bar": 2}, {"baaz": 3, "baaaz": 4}]);
打印
"QString" "[object Object]"
"QString" "[object Object]"
我正在使用 Qt 4.6...
I want to pass an array of objects from my QtScript to C++ but I have not been able to figure out how to achieve this. As soon as I create an array, the elements inside it are converted to strings before I can access them.
This is what I have been trying so far:
class myObject : public QObject, public QScriptable
{
Q_OBJECT
public Q_SLOTS:
void test(QVariantList list);
};
void myObject::test(QVariantList list)
{
for (QVariantList::const_iterator it = list.begin(); it != list.end(); ++it) {
QVariant element = *it;
qDebug() << element.typeName() << element.toString();
if (element.canConvert<QVariantMap>()) {
// Not getting here
}
}
}
The following script
myObject.test([{"foo": 1, "bar": 2}, {"baaz": 3, "baaaz": 4}]);
prints
"QString" "[object Object]"
"QString" "[object Object]"
I am using Qt 4.6...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个已报告的 bug,您可以通过更改来解决此问题将槽中的参数转换为 QScriptValue 并自行进行转换
This is a reported bug, you might be able to get around this by changing the parameter in your slot to QScriptValue and doing the conversion yourself