0

So I have this code:

QScriptValue result(someFunction());
if(result.isArray()) {
{
    // Print items in array
}

How can I do it? I tried to convert it to QList<QScriptValue> but that doesn't work. I considered doing this:

    const int length = (int)result.property("length").toNumber();
    for (int i = 0; i < length; ++i) {
        const QSCriptValue entry(result.property(QString::number(i)))));
        // do something with entry
    }

It works but seems kinda ugly.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Keep in mind that module got deprecated. – dtech Aug 29 '17 at 19:59
  • In case you can switch to the new `QJSEngine`, the `QJSValue` class has a property function overload that accepts an array index: https://doc.qt.io/qt-5/qjsvalue.html#property-1 – Felix Aug 29 '17 at 20:02

1 Answers1

0

Based on QScriptValue help, probably something like:

for(v: result.toVariant().as<QVariantList>()) {
    // here v is a QVariant-item
}

Needs testing to see if it actually.

bipll
  • 11,747
  • 1
  • 18
  • 32