PDA

View Full Version : Qt types exposed as Variant in javascript



dioselin
15th April 2010, 00:24
Hello,

In exposing my C++ objects to javascript, I have properties of types QSizeF, QColor and other Qt types:


class Control : public QGraphicsWidget{
Q_OBJECT
Q_PROPERTY (QSizeF size READ getSize WRITE setSize )
Q_PROPERTY (QColor bgColor READ getBgColor WRITE setBgColor)
...
}

These properties are converted to Variant in javascript, so this code won't work :


myControl = new Control();
myControl.size.height = 40;


What do I have to do to access these properties in javascript? Do I have to set a default prototype for QSizeF and others, or do I have to create my own Size,Color, classes?

Thank you,

dioselin
15th April 2010, 17:08
answering my own question in case a fellow newbie encounters the same....


QScriptValue toScriptValueQPointF(QScriptEngine *engine, const QPointF &s) {
QScriptValue obj = engine->newObject();
obj.setProperty("x", QScriptValue(engine, s.x()));
obj.setProperty("y", QScriptValue(engine, s.y()));
return obj;
}

void fromScriptValueQPointF(const QScriptValue &obj, QPointF &s) {
s.setX(qreal(obj.property("x").toNumber()));
s.setY(qreal(obj.property("y").toNumber()));
}

int main() {
...
QScriptEngine engine;
qScriptRegisterMetaType(&engine, toScriptValueQPointF, fromScriptValueQPointF);
}