Kent
Qt
Posted by Kent
 in Qt
 on Thursday, May 08, 2008 @ 08:50

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 ™”

» Posted by pinotree
 on Thursday, May 08, 2008 @ 12:52

Nice!
I was wondering why this (simple?) simplification did not make into Qt 4.4, already…

» Reply from Kent
 on Thursday, May 08, 2008 @ 14:02
Kent

pinotree: The new API required a significant amount of internal changes, and this happened too late to make it into 4.4 unfortunately.

» Posted by Alan M. Carroll
 on Friday, May 09, 2008 @ 19:41

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?

Leave a Reply