[PyQt] Hybrid C++ and Python in a Qt GUI
Matt Newell
newellm at blur.com
Fri Nov 21 21:13:33 GMT 2014
On Thursday, November 20, 2014 09:41:10 AM Eric Frederich wrote:
> Hello,
>
> We're starting a new project, no C++ code or Python code.
> In fact, we're porting a bunch of Tcl code.
>
> Anyway, at the end of this project I'd like to have a Qt GUI where a
> developer can create a new tool for the application in either C++ or Python.
>
> My question:
> What is the best approach for something like this?
>
> My assumption:
> Please don't dwell on this if it is completely wrong, just disregard it and
> answer the above question.
> Assumption: the host should be PyQt since PyQt can integrate C++ components
> via SIP but a C++/Qt application cannot easily integrate a PyQt component.
> Another assumption: My PyQt stuff would be able to connect to C++ signals /
> Slots but my C++ stuff would not be able to connect to my Python signals /
> slots. Is this true?
>
> Thanks in advance,
> ~Eric
I've never needed to explicitly connect to a signal defined in python. If your
application is written in c++ then you will probably integrate python
components in two ways:
1. Your python components will implement an interface and provide a factory
for creating the components. Essentially implementing a plugin. This is
needed when the c++ part will need to instantiate the python components on
demand.
2. Your python code will access python api's exposed through sip to integrate
python components into the application. Very similar to #1 but the c++ part
doesn't need to instantiate the python components.
As examples lets say you were writing a text editor.
For #1 you might have a virtual SyntaxHighlighter class with any signals
defined that the c++ part will connect to, and a SyntaxHighlighterFactory class
with a static method for registering a new factory. You would write sip
bindings for both, implement them in python for language X, and register the
factory. Then whenever a document of language X is opened, the application
asks the factory for the highligher class for X. Everything on the c++ side
is the same as if there were no python integration.
For #2 you might want to provide a file browser that can be opened via a menu
entry. For this you probably won't need any factory or need to define any
interface in c++, because the regular Qt bindings should allow grabbing the
mainwindow from the application, add an action to a menu, respond to that
action and show a dialog or embed a widget into the view. Of course depending
on the layout of your mainwindow you might need to provide bindings for the
mainwindow class to be able to easily insert a widget into a qtabwidget or
whatever.
Of course both of these design patterns can be used whether the application
itself is a c++ app with embedded python or a python app. It is probably a
little easier to get started with a python app that uses c++ components that
have python bindings because with sip you can avoid writing any code that uses
the python c api.
We have a ton of small apps/scripts that use the c++ components with python
bindings, and only a few applications that are c++ and embed python. The
major distinction is that those apps are mostly c++ code with a few python
plugins thrown in and they are heavily used so memory usage and performance
are important.
Matt
More information about the PyQt
mailing list