如何为 Qt 应用程序提供脚本支持?

How to provide Scripting support for Qt-Applications?

我正在寻找一种可以集成到我的 Qt5 应用程序中的脚本语言。该应用程序有一个 public api,可用于使用插件扩展应用程序。现在我想向应用程序添加一种脚本语言,它提供对整个 public api 的访问。 脚本语言必须满足以下要求

我评估了以下脚本语言:

您建议使用什么脚本语言和工具来满足我的所有要求?

SWIG 和 Python 似乎是个不错的选择。 SWIG 仍在积极维护中。

虽然 SWIG 不能开箱即用地满足我的所有要求,但让它们全部工作应该不是什么大事:

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

开箱即用不支持。您必须在您的应用程序中嵌入一个 python 解释器。 https://docs.python.org/2/extending/embedding.html

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

使用 python 访问文件系统和网络应该不是问题。要创建图形用户界面,有很多可用的库:
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.

这是由 SWIG 完成的。他们提供了很好的 C++ 和 c++11 支持。

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.

这可以使用 swig 提供的 c++ 函数实现:

  • SWIG_TypeQuery 获取有关 C++ 类型的信息
  • SWIG_NewPointerObj 将 c++ 对象转换为 python(代理)对象
  • SWIG_ConvertPtr 将 python(代理)对象转换回 c++ 对象

更多信息在 External runtime chapter