tags:

views:

171

answers:

1

Hi,

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

+1  A: 

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

Harald Scheirich
Thanks! I was able to parse the values using QScriptValue and the QScriptValueIterator for arrays.
Plow