Tilman Roeder

Python Extensions in QtCreator

Published Wednesday August 15th, 2018
11 Comments on Python Extensions in QtCreator
Posted in Qt for Python, QtCreator

Hello world! My name is Tilman and I have been an intern with The Qt Company in Berlin for the last few weeks. During my time here, I have worked on enabling Python extensibility for the QtCreator and I am happy to announce, that a first proof of concept version is available today!

So, what exactly do the Python extensions do?  Well, the goal is to eventually be able to do about anything a native C++ plugin could do. But for now, the scope is much narrower and only a very small part of the C++ API is exposed.

screenshot_20180809_160715

A Technical Perspective

The main goal for me was to explore how this vision could be implemented. For now the project focuses on getting the integration and setup right, rather than having as many bindings as possible.

Everything starts with a new QtCreator plugin, which initializes Python bindings and then loads the user provided Python extensions. This is done by executing their Python scripts in an embedded CPython interpreter. Getting this to work requires two main things:

  1. Bindings (and a mechanism for loading bindings only if the relevant plugins are loaded)
  2. A system for discovering, and running Python extensions

 

Generating Bindings

Some of you may be familiar with Qt for Python. This project enables developers to create Qt applications in Python by generating Python bindings for Qt’s C++ code. To do this, it uses a binding generator called Shiboken.

To generate the bindings for QtCreators APIs, I used the same tool. This means, that on top of all the QtCreator specific bindings, anything from Qt for Python is also available from Python.

Plugins in QtCreator can be disabled by the user. Thus, we can only expose bindings for the Core plugin and things like the Utils library directly without incurring dependencies. This is quite a harsh restriction on the bindings we can use.

To circumvent this problem, any other QtCreator plugin may provide an additional library, which is then dynamically loaded by the Python extensions plugin as necessary. These libraries will eventually be provided for all plugins maintained by the QtCompany. For now, there is one example of such a library available for the Project Explorer plugin.

The Embedded Interpreter

Python extensions are nothing but a directory containing a main.py file, which represents the entry point of the extension.

My main design goal was to make Python extensions ‘feel’ as if they were normal Python scripts, run from within their extension directory. Since all the extensions run in the same embedded Python, there is a good deal of code devoted to making sure extensions seem isolated, as well as setting the appropriate sys.path for each extension.

This means you can do things like import other files from your extensions directory or mess with sys.path, just like you would with a normal Python program.

If your extensions depend on any other Python modules, there is also a facility for loading these dependencies. By including a requirements.txt, all your dependencies are ‘pip installed’ before your extension is first run. Should you need to do any other setup before your main.py can run, you can also provide an optional setup.py, which is run before, and separately from, your main script.

Closing Words

While a lot of heavy lifting still needs to be done, the main challenges of this project are now solved. If you are interested in trying things out yourself, I highly encourage you to check out the projects git repository. There, you can also have a look at the code and a more in-depth documentation.

On top of the C++ code, build instructions and some initial documentation, you will find several examples of Python extensions that give a taste of what will be possible.

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Qt for Python, QtCreator

11 comments

Arturo says:

That’s cool, but is there any hope of getting proper Python project support in QtC? I mean like code completion with type hinting and all the other goodies?

Alex Blasche Alex Blasche says:

We certainly want to open up the Qt for Python opportunities in Qt Creator too but this will take time as there are many many related feature requests. For now the features you mentioned are not in the immediate list.

Eike Ziller Eike Ziller says:

We are currently experimenting with support for the language server protocol in Qt Creator, which would improve many things regarding editing code in different languages, including python. https://bugreports.qt.io/browse/QTCREATORBUG-20284

Eli says:

So next step make a package manager? https://packagecontrol.io/

Pls Qt Company make it happen! Wee need a pacakge manager like npm or cargo! You can event make money with private repos 🙂

Jean-Michaël Celerier says:

There is already qpm (https://qpm.io)

XIVN1987 says:

Qt Creator is a very good IDE, but has little plugins/externsions. The main reason is that we have to write plugin in C++, but C++ is very hard to learn and use.
If we can use python to write plugins for Qt Creator, Qt Creator will have huge mount of plugins and become a almighty IDE, like Eclipse.
So this is a wonderful work

Vincent says:

Sorry for my silly question.
Is it possible to debug python code step by step in Qt Creator?

andre says:

Yes, basic step-by-step debugging, display of simple and structured data like tuples and dicts has been possible in Qt Creator since 2010.

Alexander Rössler says:

That’s cool stuff, but I would also like to see better Qt and QML support in CLion/PyCharm. I know it’s another company, but I even would pay money for such an extension and since PyCharm is probably the most widely used Python IDE (after IDLE), it would help the acceptance of Qt in the Python world.

Eike Ziller Eike Ziller says:

I suppose this at least partially is https://bugreports.qt.io/browse/QTBUG-68406

Mike says:

This is great, so anyone can write a simple main.py that imports python-rope, and easily feed the autocompletion info from that into QtCreator.

Commenting closed.

Get started today with Qt Download now