Oleksandr Yakovlyev's Embeding PyQt Tutorial

This is a short tutorial to embed your Qt application in PyQt . It assumes knowledge of bash, Python, Qt+PyQt and sip.

Hybrid Application?

One day I discovered that it can be really great to use PyQt scripts in a Qt application. It assumes that such Hybrid application can have two "branches" - c++ code and python code working together, exchange signals between two these branches, of course possibility to create new objects with python/pyqt and connect signal/slots beetween two parts of the application (Qt and PyQt), well between Qt objects and PyQt objects. Well, even more, with Python you can create new classes in runtime, and ... this is almost magic, these new clases/objects will work together with C++(Qt) part of application.

First challenge is event loop. Python and Qt application have their own event loop, and I have not found way to use only Qt event loop yet. So we need to use Python (PyQt) event loop. It means next change in application:

Usually Qt application does not have any critical code in main.cpp so it can not be complicated to make same code with Python. Also the modification does not reflect performance because all code is C++ still.

The application code compiles into shared library. Let's try it with Qt example (examples/application)

application.h

Here I have added signal void runScript(const QString&)

application.cpp

I added a toolbutton "Run Script". Text of script is content of QTextEdit (editor of application example)

Now we can create pro file. Remember that our qt application is shared library now.

application.pro

So, we have compiliable small qt application with editor and button "Run Script". Let us to make wrapping for the application. We create dir "coreappwrap" where we put wrapping generated with sip. Now we create sip file for our wrappings:

coreapp.sip

Note, that we use #include "../application.h" because sip generates files to coreappwrap/. It is important to wrap our signal runScript, because it will be used in python part.

Now we create configure.py according to Phil Thompson manual

configure.py

We used -Wl,-rpath,. and -Wl,-rpath,.. because generated python module located in coreappwrap directory. makefile.extra_libs = coreapp is our Qt application that we have created with application.pro. It is libcoreapp.so.XXX files

Now it is time to compile that all. First build our Qt application:

It creates libcoreapp.so.XXX in "." directory

Let's prepare wrappings:

It creates wrappings files in coreappwrap

And compile them

It creates coreappwrap/coreappwrap.so. It is python module.

At the end we need to write main.py to run it all

main.py

Note that "exec" see MainWindow variable and user have access to it with scripts. You can use other variables you want to be acceessed by scripts, etc

The example (tarball ) is located at http://yshurik.kiev.ua/PyQtScripting.tar.gz, 4k (link error) http://yshurik.kiev.ua/PyQtScripting.zip, 4k

You may also make corrections to this page. If you have questions or comments, send them to me at yshurik@me.com.

EmbedingPyQtTutorial (last edited 2010-07-13 12:55:09 by 213)

Unable to edit the page? See the FrontPage for instructions.