blog2.shibu.jp

Python JavaScript Qt

Qt + JavaScript(1): Install QtScript Binding

Nowadays, JavaScript is the most used language for HTML client and some guys start using it for servers on node.js. And also, JavaScript can be used with the most powerful cross platform GUI framework, Qt.

Qt includes a JavaScript engine. Qt4 uses JavaScriptCore made by Apple, and Qt5 uses V8 made by Google. Qt programmers can use the JavaScript engine for the following features:

  • QtScript
  • QtQuick

QtScript is a standard JavaScript (ECMAScript) environment. QtScript provides a simple API to register existing C++ classes that are based on QObject. This engine itself doesn’t have any special features for Qt GUI programming, but you can register extensions for Qt widgets and use it for GUI programming.

QtQuick is one of the first-class programming tools for Qt5. It is a declerative programming environment. The main source code is JavaScript code segments inside QML. Qt5 starts supporting Android and iOS using QML.

On my blog, I will describe the QtScript environment.

QtScript Binding Generators

The QtScript Binding Generator generates source code that coordinates almost all Qt widgets with the QtScript engine. This tool allows you to implement GUI programs in a JavaScript.

Unfortunately there are many repositories of QtScript Binding Generators on Google Code, an old QtLabs page and so on. The following repositories are the better ones:

The Binding Generator for Qt5 supports fewer classes/modules than Qt4 now. For example, it doesn’t create WebView and OpenGL, QGraphicsView support and so on.

Install QtScript Binding Generator on MacOS

Install the Qt library for your computer. I am using Mac OS X 10.8. Download the installer from http://qt-project.org/downloads, and then install the git command to get the QtScript Binding Generator. MacPorts and Homebrew are common ways to install the command line development tools. My recommendation is MacPorts because it tries to install pre-built binary packages as much as possible from MacPorts version 2.0. It saves your time and electric cost.

Get the QtScript Binding Generator source code and set the environment variables to build it:

# for Qt4.X
$ git clone git://gitorious.org/qt-labs/qtscriptgenerator.git
$ export QTDIR=~/QtSDK/Desktop/Qt/4.8.1/gcc/
$ export PATH=$QTDIR/bin:$PATH
# for Qt5
$ git clone git://github.com/svalaskevicius/qtscriptgenerator.git
$ export QTDIR=~/Qt5.0.02/5.0.0/clang_64/
$ export PATH=$QTDIR/bin:$PATH

At first, you have to build Binding Generator and then generate registration code:

$ cd qtscriptgenerator/generator
$ qmake
$ make
(wait..)
$ ./generator --include-paths={$QTDIR}/gcc/include
(wait..)
Classes in typesystem: 532
Generated:
  - classes...: 514 (80)
  - header....: 327 (0)
  - impl......: 327 (0)
  - modules...: 10 (9)
  - pri.......: 10 (0)

Check result classes’ number. If this number is much less than this, check QTDIR environment variable.

If your MacOS X version is 10.8, you should use one of following command instead of qmake:

# for Qt 4.X
$ qmake -spec unsupported/macx-clang

# for Qt5
$ qmake -spec macx-clang

Let’s build a generated QtScript binding code.

$ cd ../qtbindings/
$ qmake
$ make
(wait...)

Try sample codes like this:

$ ./qs_eval/qs_eval ../examples/AnalogClock.js
../../../_images/analog.png