QtScript:将对象数组传递给 C++

发布于 2024-08-29 19:39:06 字数 815 浏览 1 评论 0原文

我想将一个对象数组从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-09-05 19:39:06

这是一个已报告的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文