PDA

View Full Version : Accessing QtScript arrays from C++



wookoon
8th June 2010, 11:01
I wondering how to access an (QScript) array from C++.

We can use QScriptValue::isArray() to determine if the value is an array or not. Unfortunately, it doesn’t really

state there how to access contents of the array.My initial reaction was to just convert the value to a

QVariant (using toVariant()) and then convert the variant to a QVariantList (toList()). That didn’t quite work.

numbat
8th June 2010, 11:26
Works for me. Can you post your code?


#include <QtGui>
#include <QtScript>

int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QScriptEngine eng;
QScriptValue val = eng.evaluate("[1, 2, 3, 'four', 'five']");

qDebug() << val.isArray();

QVariantList arr = val.toVariant().toList();

foreach (QVariant item, arr)
{
qDebug() << item;
}

return 0;
}