Creating a C++ Qt Gui for a Python logic - python

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.
What are recommended ways of combining a C++ Qt GUI and a Python logic, perhaps with a controlling layer which is either in Python or C++?
Due to prior research, I'm aware of certain tools like PySide, PythonQt, SWIG, Shiboken and others (although I haven't used them so far) and that it is very simple to import *.ui files into Python, but the number of possible ways is a bit overwhelming and I could not come up with a "good solution" so far.
Ideally, I would not use Qt Designer, but create the GUI (windows, custom widgets, helper classes etc) by hand, combine them into one or few classes which I expose to a Python/C++ controlling layer (by wrappers? DLL?) which manages the communication between GUI and logic.
How can I achieve this?
A short version of this question might just be: How can I use a Qt C++ GUI with a Python logic?

You can create your GUI application using qt c++ and wrap controler logic written in a python api embedding the python api.
https://docs.python.org/2/extending/embedding.html
But if you have many methods that will be boring to do and you may have some memory leaks if not well written in th c++ side.
Another solution may be using rpc calls to a python api using a webservice (rest api/json rpc ....Etc...). Qt application is client of a python rest api.
like this you split the c code from the python one. You can do that using json rpc calls , or other apis,like jcon (https://github.com/joncol/jcon-cpp).
You will too be able to find python and QT xml rpc api able to talk together.
swig (http://swig.org) may have some capability , but it is better used in the other way : calling c func from python

Related

I want to create an extensible app via plugins with C++ and Python

I want to create an app that will be extensible via plugins.
I know that I have 2 options.
I can create my own interpreted language and app with a built-in interpreter for this language.
I can use one of the existing languages such as Python, Lua or another scripting language.
I want to use option 2. And I know that I must create a layer for external language to enable communication between this language and my app. But I don't know how to do it. Maybe I must use interprocess communication or something like that.
Let's assume that I have an application written in C++. In the beginning, it may be even a simple console app that displays a few options. And I want to write a plugin in Python like this:
option = "additional option"
myApp.addOption(option)
And then:
I launch my app
My app loads the plugin
I see my app with this additional option displayed
I want to do this simple thing to understand how it works and then I will be able to do something more complicated.
You could start by looking at the languages' documentation(if you're new):
Python -->https://docs.python.org/3/
Lua --> https://www.lua.org/docs.html
C++ libraries can also be called in C(If you're careful enough),you could look at this too
https://www.teddy.ch/c++_library_in_c/
You should be aware that, with care, a C++ library can be called from a C program, mostly by appropriately using extern "C" to disable name mangling. On Linux, read also the C++ dlopen mini Howto.
Then you need to read the chapter Extending and embedding the Python interpreter
At last, Python is open source, so please study its source code.
I can use one of the existing languages such as Python, Lua or another scripting language.
I strongly recommend considering using GNU Guile or extending Ocaml.
And both TensorFlow or NumPy could inspire you, since they are open source libraries (coded in C and/or C++) usable from Python.

Python support for Qt dll

I have my own C++ library project(with source) written in Qt and it uses QTcpsocket, QUdpSocket, QSerialPort signals and slots.
I would like to support this library in Python as well.
What is the preferred way to do this?
Writing a wrapper in Python, if so does it have obstacles?
Dont know if PyQt is just for this purpose?
Or do you thnink is it better to rewrite the lib in Python by just implementing the logic used in C++ library project.
As this is library is part of a SDK, same applies for supporting QT dll with .NET as well in fact, as a second step after supporting Python.
Example API of Qt.
quint16 SendCommandAsync(CmdBaseSpv1* pcommand,
ConnectionArg connectionArg,
emitLogOptions::emitLogOption logOption,
LogInfo &txLogInfo,
LogInfo &rxLogInfo);
I want to call this function from Python.
Function parameters CmdBaseSpv1, ConnectionArg, emitLogOption, LogInfo are all Qt classes.
Some of these arguments are using the QOBJECT base class.
As you see from the function name; it is an Asynchronous function call. Result will emit a signal so I need to get async result as well.
I'll write down what I know about wrapping C++ libraries and will try to source it, but as a huge disclaimer, I have only used this for something very, very simple myself.
You ask about rewriting the library in Python. I would say this depends. If the code is trivial, then I don't see why not. If it is larger and has to be kept up-to-date with other code (as you imply with .Net), I wouldn't. It makes sense to reuse the same code for both.
 My suggestion
From what I see of your code I would try to wrap it using boost::python or SWIG.
How to wrap
The main trouble is going to be to create CmdBaseSpv1, ConnectionArg, etc. in Python.
If you don't need any Qt-classes to instantiate your classes, this should be straightforward. However, in case you need the Qt types inside of Python (e.g. because the constructor of CmdBaseSpv1 requires a QString), your task is a lot more complicated because you need a way to convert a Python-string into a QString. If you can, you should only use stl-types.
Everything in Python
The simplest way to wrap a small C library is to use the cffi module (or ctypes). You can write the full binding in Python. However, this is a lot of manual work if your API is large and can get difficult.
There is another problem: ctypes is only compatible with C, not C++. So you'd need to change your interface to be compatible with C, internally you could still use C++ and Qt.
Wrap by hand
An alternative is to wrap the library calls yourself. You can either do this by using the Python API. There are also a few libraries that help you create the bindings. Boost::python seems especially promising and works with C++.
Binding generators
If your API is very large, you should use a binding generator which parses the C++ code and generates the bindings itself. For example sip is one of them. It is used to create the bindings for the whole Qt library. There are a few binding generators out there, one mentioned in the Python docs is SWIG. PySide uses Shiboken and also has a nice description of it on their website.
SWIG has the additional advantage, that you can create bindings for multiple languages, including C#.
PyQt
PyQt is a binding generated from Qt using sip. You'll probably not need it, unless you need to access the full power of Qt from inside Python. If this is the case, consider using sipfor generating the bindings, so things like the signal-slot mechanism are compatible between your library and PyQt.
Challenges with bindings
Bindings come with a few challenges because Python and C++ are different in some key areas.
Memory-management
Memory management in Python is almost automatic, in C++ you're required to do it manually. For example
def myfunc():
mywidget = QWidget()
at the end of myfunc() mywidget gets garbage collected. In C++ however
void myfunc() {
auto mywidget = new QWidget();
}
mywidget is still around. This means that even when inside Python, you need to take care of the C++ memory management. The problems I've seen are memory leaks and dangling pointers. Watch out for this when using callbacks, you don't want Python to garbage collect the callback while C++ thinks it's still alive.
Exceptions
Not all programming languages have exceptions or deal with them the same way. For example, it would be nice if an exception inside C++ can be caught inside Python.
Links to related question
How to wrap a c++ library for python? (example of boost::python)
Exposing a C++ API to Python (discussion about boost::python, SWIG and more)
https://stackoverflow.com/a/5686873 (discusses Cython, another choice)

Declarative GTK

TL;DR: Is there a library for declarative UI creation using GTK? Preferrably with Python support.
I'm a Python/Django developer, most of my experience about user interfaces is from the web, where declarative, loosely coupled UI designs are standard. Recently I've had to create a GUI app using Java/Swing for a school project and ended up using SwiXML to create declarative Swing layouts for the project.
I asked myself whether there are similar possibilities to use for Python. I'm not very fond of Java, so I won't start developing Swing applications. When looking for Python GUI Toolkits, I ended up concluding that the three main players in this field are GTK, QT and Wx.
Of those 3 I'd prefer GTK, but I could not find any way to create a declarative UI with GTK. From my experience with HTML and SwiXML, I find code based UI creation a huge mess and would prefer not having to do it that way. Declarative UI is much more flexible and more loosly coupled.
Is there a library for declarative UI creation using GTK? Preferrably with Python support.
If there isn't such a thing (which I assume, as I couldn't find anything), I might end up getting started with QT, even though I don't like the default look of it under Linux. (But maybe that's customizable too, in a way so that it looks similar to GTK.)
I think what you're looking for is gtk.Builder. Basically, gtk.Builder objects can be used to load a .ui file that contains xml data that describes the widgets for the user interface and the callbacks to the events that should be exposed by the code. The .ui file can be created with glade so you don't even need to write the xml yourself.
Look at enaml. Although there is no GTK toolkit, it is the only truly declarative framework for python that I'm aware of.

High(er) level frameworks that wrap Tkinter/ttk

Curious if there are any higher level frameworks that attempt to wrap Tkinter? For example, wxPython is wrapped by the Dabo framework (http://dabodev.com/) and PythonCard.
Motivation: We've recently moved to Python 2.7 (Windows) and are very impressed with the new ttk (Tile) support which allows one to build professional quality, platform native GUI's using the built-in Tkinter framework. In the past we would have used wxPython to create simple GUI interfaces for our command line utilities, but we're re-thinking this strategy in favor of using Tkinter/ttk for these use cases.
We're new to Tkinter (coming from wxPython) and while Tkinter/ttk seem to be simple to use, there seems to be a lot of repeated boilerplate code that we're writing. Before we try to wrap up some of our code in a home made set of classes, I want to make sure that we're not re-inventing the wheel.
Probably a little bit late for you. But I've just released a tkinter framework in beta called tKroopy. Which aims to provide a means for switching between dialogs and provide some higher level widgets, like easily displaying tabulated data.
It was designed for building lots of small to medium applications and grouping them together in a single application, but there is no reason why you couldn't use it to build a single large scale application too.
https://github.com/tKroopy/tkroopy
The only one I knew about seems quite stale, Python megawidgets.
You can find a list of others on the Tkinter wiki.
tkRAD supports python 2 and 3 and looks mature
https://pypi.python.org/pypi/tkRAD/1.6.5

Markup-based GUI for python

I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them.
I think GUI design should be done in a separate text-based file in some markup format, which is read and rendered (e.g. HTML), so that the design of the interface is not tightly coupled with the rest of the code.
I've seen HTMLayout and I love the idea, but so far it seems be only in C++.
I'm looking for a python library (or even a WIP project) for doing markup-based gui.
UPDATE
The reason I can't accept QT's xml is the same reason I hate the programatic approach; you're assembling each widget separately, and specifying each property of it on a separate line. It doesn't provide any advantage over doing it the programatic way.
You can try Mozilla's XUL. It supports Python via XPCOM.
See this project: pyxpcomext
XUL isn't compiled, it is packaged and loaded at runtime. Firefox and many other great applications use it, but most of them use Javascript for scripting instead of Python. There are one or 2 using Python though.
How about wxPython? I'm just now beginning to work with it, but there's a tool -- XRC Resource Editor -- that allows you to assemble your GUI, which is then written to an XML file. As I understand it, your Python application loads the XML file, rather than having a whole bunch of GUI-layout code mixed in with your Python code.
You should look into Qt, which you can use from Python using the excellent PyQt interface (why they didn't name it QtPy --- cutiepie, get it ? --- I will never understand).
With Qt, you can have the choice of constructing your GUI's programmatically (which you don't want), or using XML markup. This XML file can either be compiled to code beforehand, or loaded with a short command. The latter is the normal way to work using PyQt.
Qt is versatile, high-quality, cross-platform, and you're likely using it already without knowing it. The official Skype client application is written in Qt if I remember correctly.
Edit: Just adding some links so the OP get get some feel for it ...
Short intro to Qt programming with C++ -- notice the use of the Qt Designer, which outputs .ui files, which is an XML format which I remember was also quite easy to work with by hand. PyQt programming is very similar, except for being in a much easier programming language of course :-)
Info about PyQt on the Python wiki
Online book on PyQt programming
If you choose a language like Tcl or Python and Tk for your application development it becomes fairly trivial to write your own DSL for describing the interface. You can, for instance, write a DSL that lets you create menus like this:
menubar {
File => {
Open => cmd.open
Save => cmd.save
Exit => cmd.exit
}
Edit => {
Cut => cmd.cut
Copy => cmd.copy
Paste => cmd.paste
}
}
... and your main GUI forms like this:
form PropertiesForm {
Font: [fontchooser]
Foreground: [foregroundChooser]
Background: [backgroundChooser]
}
form NewUserForm {
username [_____________________]
[] administrator
enable the following features:
() feature 1
() feature 2
() feature 3
}
notebook {
Properties => PropertiesForm
New User => NewUserForm
}
... and so on. Tcl really excels at letting you write DSLs like this. Note that this capability isn't built in to Tcl per se, but the language makes DSLs trivial. Some of this type of thing exists on the Tcler's wiki, for example there's code to create menus similar to what I described at Menus Made Easy.
I think, though, that after a while you'll find it really, really hard to make professional grade UIs in this manner.
If you use GTK, you can use Glade, which is an XML file.
It's XML, not Python, but look at Open Laszlo
windows?
you can use the WinForms editor in Visual Studio and then talk to the assembly from IronPython.

Categories

Resources