How to provide Scripting support for Qt-Applications? - c++

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.
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

Related

How to use multiple programming languages in one GTK application?

I am currently learning GTK, specifically gtkmm. As i can see GTK has excellent support in different languages through bindings. So one can use c, c++, js, python and more to implement gtk apps. Thats great. But what if i wanted to create one application, say the base window and some api objects/instances in gtkmm and c++ and want to let others create modules in any other language that has gtk binding. I want to share the api objects/instances (say they are singletons) between all modules. But i definitely don't want to spawn multiple processes for this to work. I know boost has support for exposing c++ objects into python modules. I am looking for something like that. But from the documentation so far (i haven't finished it yet, so forgive me if i have missed anything), i couldn't find the proper way to do that.

Play Scala - Native access

I am developing an web application using the Play framework in Scala language. In my application I have to access the native methods which is written in C++ and converted into .so by using swig.
My aim is to call the native method which is in .so file from the Controller class. I have searched in the internet, but I didn't get any documentation for this.
I have seen some links which is used by scala language.
https://code.google.com/p/scala-native-access/
https://code.google.com/p/bridj/wiki/Download#Specialized_subsets_(smaller_JARs_!)
https://github.com/xudongyang/scala-native-access
But they didn't mention how exactly use this in the Play framework.
Can anybody have the documentation for Play scala native access?
Can anybody have the sample applcation for the same?
Like in any JVM language, JNA/JNI gives you native access. Be aware that because of Play's use of class loaders, you'll need to make sure you access from the same class. See fail to load a native library using activator (Play Framework)
This is hard to answer in general. Play! is just a Scala library and framework, some any tool that allows you to use native calls in Scala will work similarly with play. From the first link that you pointed to, there are instructions to integrate with SBT (the Scala Build Tool) which also manages your Play framework. You will need to make the changes they mention to your build.sbt file which you can find in the top level of your Play project folder.

C/C++ Python interpreter

What I'm trying to do is write an app in C/C++ which will allow users to enter a Python script, which the app will then interpret & run. The aforementioned script will have a separate API that I'll implement in C, then expose to the user.
Is there a way to do this? I've searched on Google, but I've only found ways to create 'extensions' and not actual interpreters (like Lua allows).
Documentation about Embedding Python in Another Application says:
The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.
Look especially into Extending Embedded Python:
Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application.
Just read the docs I referenced and you should be able to implement Python interpreter within your app written in C.
You can do a call from C++ to Python and vice versa using Boost Python
If you're interested I recently implemented embedded Python scripting for C++ from first principles. I created a C++ Python wrapper library called ECS:Python (Embedded C++ Scripting with Python) that allows you to expose object from a C++ application to an embedded Python interpreter for interactive scripting. It's light-weight and very easy to use (and free!).
http://sourceforge.net/projects/ecspython

Creating gui binding for c++ and YUI

I am planning to use yui or jquery ui as front end for native c++ applications. I found no bindings present for this purpose. Will it be feasible/possible to do this ? If it is so, then how shall i proceed? please suggest advice.thanks
#rwik - I can see your point and desire to do what you intend and I would presume it to be a wise move to be making at this point in time considering the vast facilities browsers bring to the table as well as other aspects... I know we are in September now and I have come across this pretty late, but this is also for others who have a similar problem...
As far as I know, descending from JavaScript in to C++ is pretty murky, mostly due to the heterogenous nature of Javascript engines [Rhino, Tracemonkey, Spidermonkey, V8, Caracan,Charka, etc] employed by different browsers. But there are indeed ways to do this - depending on how dirty you want to get your hands and, indeed, on your abilities...
If you were to go with Google as a browser, you should take a look at projects such as cproxyv8 - http://code.google.com/p/cproxyv8/ or v8-juice - http://code.google.com/p/v8-juice/ which both offer interesting facilities...
My preference however is with Mozilla - simply because of XML User-Interface Language (XUL) which, if you do not know, allows you to speedily create your own user interfaces by giving direct access to its layout engine. I.e., you can even do away with the browser look and have your own independent layout while still having all browser facilities at your disposal.
With respect to C++/Javascript interaction - projects should exist for all alternatives - it pays to check.
I wish you good luck if you haven't already solved this problem.
I'd recommend QT instead.
However since native C++ has nothing to do with GUI I'm going to assume you are making a library in C++. However Yui is basically just JavaScript as is jQuery. So if you're asking can a web page or script be a C++ application the answer is not really.
What you could do is create a web service which uses your C++ library and exposes an interface for your web based application to call it indirectly.
One way to accomplish this is to embed WebKit into your application and provide your custom C++ functionality via a plugin. Both the WebKit framework on OSX and the Chromium Embedded Framework on Windows allow this style of application development. This allows you to write a UI using local Javascript/HTML files without the need for a webserver.
There are no direct bindings in JQuery and YUI because these libraries run in a web browser. Thus unless you’re planning to write your own browser plugins, the integration with the C++ will happen at the webserver that serves the data to the browser.
You could feed the data from your C++ code directly into JQuery/YUI by building web services directly in C++. But you might want to build a web application that will be serving your JQuery/YUI pages and that act as user interface controller (like in the MVC pattern). Web application languages like Java, PHP, etc. all have ways to call native code. Java has JNDI and in PHP you build extensions that link to C++ code.

How does node.js/[my own library/plugin] run on v8?

Does it use any libraries like this:
http://code.google.com/p/v8-juice
http://ui.ajax.org/#o3
https://github.com/tsa/vu8
Or has it written its own libraries? If v8 is written for executing javascript, why do node.js libraries use C code? Just for the filesystem/event stuff? If so, why was that necessary, doesn't v8 need events and filesystem stuff itself?
If I want to work with a database that only supports a C api, how would I go about doing that? Right now I'd probably write a v8-juice plugin.
node.js includes its own embedded version of v8 (not sure if it is customized or not, but it could be).
Javascript itself provides no interface to things like file system I/O, so that you as the embedder (in this case node) have to provide native code objects to expose that functionality. The browser does the same thing for DOM and network features, by the way.
If I want to work with a database that only supports a C api, how would I go about doing that?
You'd need a node.js extension for this (a native code plugin). If you are lucky, someone has already made on for your database system, if not, look at the source code for a similar extension as to how those are written. And here is an introduction article. You'd need to be familiar with writing a v8 extension, because that is what a node extension basically is.
If you are talking to the database over a network connection, and feel like implementing the wire protocol yourself, you could also try to do that in pure Javascript, like someone did for MySQL.