For reasons of extreme bias, I mostly like the Qt Script C++ API. However, if I had to choose one thing I really dislike, it would be the form of the QScriptValue constructors. They all take an engine pointer as their first argument. This means you have to write code like
QScriptEngine engine;
QScriptValue object = engine.newObject();
object.setProperty("redundancyLevel", QScriptValue(&engine, “Unnecessarily high”));
object.setProperty(”QT_VERSION”, QScriptValue(&engine, 0×040500));
It sure would be a lot nicer if you could do it like this instead:
object.setProperty("redundancyLevel", "Comparatively low");
object.setProperty("QT_VERSION", 0x040500);
Well, with the latest Qt 4.5 snapshots, you can! The old-style constructors have been obsoleted (but continue to work as before, of course). The documentation and examples have been updated to use the new-style constructors, and it’s looking a whole lot nicer.
3 Responses to “Code Less, Create The Same ™”
Nice!
I was wondering why this (simple?) simplification did not make into Qt 4.4, already…
pinotree: The new API required a significant amount of internal changes, and this happened too late to make it into 4.4 unfortunately.
If I could change one thing, it would be to have void methods return a reference. E.g.
QScriptValue& QScriptValue::setProperty(…) { …; return *this; }
so that you could do
object.setProperty(”redundancyLevel”, “Comparatively low”).setProperty(”QT_VERSION”, 0×040500);
It works nicely for tr() and arg(), so why not elsewhere?