开发者

QtScript: Passing an array of objects to C++

开发者 https://www.devze.com 2022-12-27 07:12 出处:网络
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 bef

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...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消