Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide Scripting support for Qt-Applications?

Tags:

c++

scripting

qt

I'm looking for a scripting language which can be integrated into my Qt5 Application. The application has a public api, which can be used to extend the application with plugins. Now I want to add a scripting language to the application which provides access to the whole public api. The scripting language must fulfill the following Requirements:

  • Script Code can be executed from within the QT-Application.
  • The user can access the file-system, network and create graphical elements from the scripting language.
  • The user can access the public api of my QT Application through bindings.
  • There should be a generator available to automatically generate script-language bindings for my public api.
  • For classes that are part of the Public Api, it should be possible to pass around objects between the QT-Application and the Scripting Engine.

I evaluated the following Script-Languages:

  • Qt-Script, together with the QT-Script Generator.
    The scripting language is based on ECMAScript/Javascript and can easily be integrated into QT apps. This fulfills all my requirements and works as expected. The generator can be used to generate bindings for the QT-Api itself and to generate bindings for the public api of my application. Sadly the qt-script module is going to be deprecated with qt5.5 and the scriptgenerator is no longer maintained.
  • Python
    There seem to be several python-qt bindings available.
    Pyside would probably be ok, but it seems to be inactive as well. Apart from that I would have to embed python into c++, which is not supported by pyside out of the box, but could be done by the python c api.

What scripting-languages and tools do you suggest, that fulfills all my requirements?

like image 659
Timo Avatar asked May 27 '15 11:05

Timo


People also ask

Is QT a scripting language?

Qt Script is based on the ECMAScript scripting language, as defined in standard ECMA-262. Microsoft's JScript, and Netscape's JavaScript are also based on the ECMAScript standard. For an overview of ECMAScript, see the ECMAScript reference.

How does Qt application work?

Qt uses a command line tool that parses these project files in order to generate "makefiles", files that are used by compilers to build an application. This tool is called qmake. But, we shouldn't bother too much about qmake, since Qt Creator will do the job for us. TEMPLATE describes the type to build.


1 Answers

SWIG with Python seems to be a good choice. SWIG is still actively maintained.

Although SWIG doesn't fulfill all of my requirements out of the box, it shouldn't be that a big thing to make all of them work:

Script Code can be executed from within the QT-Application.

This is not supported out of the box. You have to embed a python interpreter into your application. https://docs.python.org/2/extending/embedding.html

The user can access the file-system, network and create graphical elements from the scripting language.

Accessing the filesystem and network should not be a problem with python. To create graphical userinterfaces, there are a lot of libraries available:
https://wiki.python.org/moin/GuiProgramming

  • The user can access the public api of my QT Application through bindings.
  • There should be a generator available to automatically generate script-language bindings for my public api.

This is done by SWIG. They provide great C++ and c++11 support.

  • http://www.swig.org/Doc3.0/SWIGPlus.html
  • http://www.swig.org/Doc3.0/CPlusPlus11.html

For classes that are part of the Public Api, it should be possible to pass around objects between the QT-Application and the Scripting Engine.

This is possible using the c++ functions provided by swig:

  • SWIG_TypeQuery gets you information about C++ types
  • SWIG_NewPointerObj converts a c++ object to a python (proxy) object
  • SWIG_ConvertPtr converts a python (proxy) object back to c++ object

More info in the External runtime chapter

like image 55
Timo Avatar answered Nov 15 '22 07:11

Timo