简体   繁体   中英

How to provide Scripting support for Qt-Applications?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM