Code Less, Create The Same ™

Posted by Kent Hansen on May 8, 2008 · 3 comments

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, 0x040500));

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.

QShare(this)

Possibly related posts:

  1. Moving code around
  2. Designing code for QML UIs
  3. Moving code around more easily

3 comments

1 pinotree May 8, 2008 at 1:52 pm
 

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

2 Kent May 8, 2008 at 3:02 pm
 

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

3 Alan M. Carroll May 9, 2008 at 8:41 pm
 

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?

Comments on this entry are closed.

Previous post:

Next post: