0

I know I can use below way to expose QStringList to QtScript, but how can I expose a QVariantHash?

QStringList l;
l<<"2";
QScriptValue v = qScriptValueFromSequence(&engine, l);
engine.globalObject().setProperty("v", v);
zhongzhu
  • 307
  • 2
  • 4
  • 14

2 Answers2

0

For standard containers qScriptValueFromSequence() is running fine. For other types you probably will have to create your own conversion function, take a look at qScriptRegisterMetaType().

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • just checked Qt Assistant. looks like it can auto convert QVariantMap type? But do you know how. Believe QVariantMap will serve the same purpose as QVariantHash. it'll be great if auto convert is supported. – zhongzhu Jul 10 '13 at 02:30
0

you can use engine.toStriptValue() to expose QVariantMap to QtScript

QVariantMap map;
map["a"] = 3;
map["b"] = 4;

QScriptValue v = engine.toScriptValue(map);
engine.globalObject().setProperty("m", v);

engine.evaluate("var h = new Haha()");
qDebug()<<engine.evaluate("h.add(m)").toString();
zhongzhu
  • 307
  • 2
  • 4
  • 14