Search code examples
qtqtscript

QtScript: Passing an array of objects to C++


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


Solution

  • 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