About Collections Archives

QS_BIND macro magic

2 min read

At my request, Kent kindly changed the initialization of the Qt bindings (from the binding generator project) so that the class wrappers are populated in a single function for every module. This also means, you don’t have to compile everything as plugins and then deploy them with your scripted application. You can just include the appropriate .pri file (e.g. generated_cpp/com_trolltech_qt_gui/com_trolltech_qt_gui.pri for Qt Gui bindings) and then build together with your project (or compiled to non-shipped, project-wide static library, if you wish).

Another gem: for each module you will have a source file, e.g. qt_gui_init.cpp for Qt Gui, that is responsible to create the wrapper classes. This means, the two macro tricks would work:

// example: QS_BIND_MODULE(new QScriptEngine, core)
#define QS_BIND_MODULE(engine, modulename) 
extern void qtscript_initialize_com_trolltech_qt_##modulename##_bindings(QScriptValue &extensionObject;); 
  { QScriptValue extensionObject = (engine)->globalObject(); 
  qtscript_initialize_com_trolltech_qt_##modulename##_bindings(extensionObject); }

// example: QS_BIND_CLASS(new QScriptEngine, QPushButton)
#define QS_BIND_CLASS(engine, classname) 
extern QScriptValue qtscript_create_##classname##_class(QScriptEngine*); 

(or download/git-clone it from

While the first is arguable already useful, the second one is interesting because it allows you to limit the bindings to certain classes only. For example, if you want to extend the look-and-feel of some GUI elements in your cool applications, just offer the user the bindings for QPainter, QBrush, QPen, and other related classes. Neat, isn’t it?

On a related note, you should also perform some kind of dance that our QtScript hero has also created the bindings for the Animation and State Machine frameworks slated for Qt 4.6.

Isn’t the Qt Script world wonderful?

♡ this article? Explore more, check the archives, or follow me Twitter.

Share this on Twitter Facebook Google+

comments powered by Disqus