In contrast to PyQt , PythonQt is not a complete Python wrapper around the complete Qt functionality. So if you are looking for a way to write complete applications in Python using the Qt GUI, you should use PyQt.
If you are looking for a simple way to embed the Python language into your Qt Application and to script parts of your application via Python, PythonQt is the way to go!
PythonQt is a stable library that was developed to make the Image Processing and Visualization platform MeVisLab (http://www.mevislab.de) scriptable from Python.
import replacement, so that Python scripts can be e.g. signed/verified before they are executed (PythonQtImportInterface)
| Qt/C++ | Python |
|---|---|
| bool | bool |
| double | float |
| float | float |
| char/uchar,int/uint,short,ushort,QChar | integer |
| long | integer |
| ulong,longlong,ulonglong | long |
| QString | unicode string |
| QByteArray | str |
| char* | str |
| QStringList | tuple of unicode strings |
| QVariantList | tuple of objects |
| QVariantMap | dict of objects |
| QVariant | depends on type, see below |
| QSize, QRect and all other standard Qt QVariants | variant wrapper that supports complete API of the respective Qt classes |
| OwnRegisteredMetaType | variant wrapper, optionally with a wrapper provided by addVariantWrapper() |
| EnumType | integer (all enums that are known via the moc and the Qt namespace are supported) |
| QObject (and derived classes) | QObject wrapper |
| C++ object | CPP wrapper, either wrapped via PythonQtCPPWrapperFactory or just decorated with decorators |
| PyObject | PyObject |
PyObject is passed as simple pointer, which allows to pass/return any Python Object directly to/from a Qt slot. QVariants are mapped recursively as given above, e.g. a dictionary can contain lists of dictionaries of doubles. For example a QVariant of type "String" is mapped to a python unicode string. All Qt QVariant types are implemented, PythonQt supports the complete Qt API for these object.
It is important that you call PythonQt::registerClass() for any QObject derived class that may become visible to Python, except when you add it via PythonQt::addObject(). This will register the complete parent hierachy of the registered class, so that when you register e.g. a QPushButton, QWidget will be registered as well (and all intermediate parents).
From Python, you can talk to the returned QObjects in a natural way by calling their slots and receiving the return values. You can also read/write all properties of the objects as if they where normal python properties.
In addition to this, the wrapped objects support
The below example shows how to connect signals in Python:
# define a signal handler function def someFunction(flag): print flag # button1 is a QPushButton that has been added to Python via addObject() # connect the clicked signal to a python function: button1.connect("clicked(bool)", someFunction)
A Meta class supports:
From within Python, you can import the module "PythonQt" to access these meta objects and the Qt namespace.
from PythonQt import * # namespace access: print Qt.AlignLeft # constructors a = QSize(12,13) b = QFont() # static method QDate.currentDate() # enum value QFont.UltraCondensed
The idea behind decorators is that we wanted to make it as easy as possible to extend wrapped objects. Since we already have an implementation for invoking any Qt Slot from Python, it looked promising to use this approach for the extension of wrapped objects as well. This avoids that the PythonQt user needs to care about how Python arguments are mapped from/to Qt when he wants to create static methods, constructors and additional member functions.
The basic idea about decorators is to create a QObject derived class that implements slots which take one of the above roles (e.g. constructor, destructor etc.) via a naming convention. These slots are then assigned to other classes via the naming convention.
The below example shows all kinds of decorators in action:
// an example CPP object class YourCPPObject { public: YourCPPObject(int arg1, float arg2) { a = arg1; b = arg2; } float doSomething(int arg1) { return arg1*a*b; }; private: int a; float b; }; // an example decorator class ExampleDecorator : public QObject { Q_OBJECT public slots: // add a constructor to QSize variant that takes a QPoint QVariant new_QSize(const QPoint& p) { return QSize(p.x(), p.y()); } // add a constructor for QPushButton that takes a text and a parent widget QPushButton* new_QPushButton(const QString& text, QWidget* parent=NULL) { return new QPushButton(text, parent); } // add a constructor for a CPP object YourCPPObject* new_YourCPPObject(int arg1, float arg2) { return new YourCPPObject(arg1, arg2); } // add a destructor for a CPP object void delete_YourCPPObject(YourCPPObject* obj) { delete obj; } // add a static method to QWidget QWidget* static_QWidget_mouseGrabber() { return QWidget::mouseGrabber(); } // add an additional slot to QWidget (make move() callable, which is not declared as a slot in QWidget) void move(QWidget* w, const QPoint& p) { w->move(p); } // add an additional slot to QWidget, overloading the above move method void move(QWidget* w, int x, int y) { w->move(x,y); } // add a method to your own CPP object int doSomething(YourCPPObject* obj, int arg1) { return obj->doSomething(arg1); } }; ... PythonQt::self()->addDecorators(new ExampleDecorator()); PythonQt::self()->registerClass(&QPushButton::staticMetaObject); PythonQt::self()->registerCPPClassNames(QStringList() << "YourCPPObject");
After you have registered an instance of the above ExampleDecorator, you can do the following from Python (all these calls are mapped to the above decorator slots):
from PythonQt import * # call our new constructor of QSize size = QSize(QPoint(1,2)); # call our new QPushButton constructor button = QPushButton("sometext"); # call the move slot (overload1) button.move(QPoint(0,0)) # call the move slot (overload2) button.move(0,0) # call the static method grabber = QWidget.mouseWrapper(); # create a CPP object via constructor yourCpp = YourCPPObject(1,11.5) # call the wrapped method on CPP object print yourCpp.doSomething(1); # destructor will be called: yourCpp = None
For building PythonQt, you will need to set some environment variables: PYTHON_PATH should point to the Python sources/headers. PYTHON_LIB should point to where the Python library files are located. PYTHONQT_ROOT should point to the root directory of PythonQt.
Run qmake on PythonQt.pro to generate a project file for your system and then build it.
The following shows how to integrate PythonQt into you Qt application:
#include "PythonQt.h" #include <QApplication> ... int main( int argc, char **argv ) { QApplication qapp(argc, argv); // init PythonQt and Python itself PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut); // get a smart pointer to the __main__ module of the Python interpreter PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule(); // add a QObject as variable of name "example" to the namespace of the __main__ module PyExampleObject example; PythonQt::self()->addObject(mainContext, "example", &example); // register all other QObjects that you want to script and that are returned by your API PythonQt::self()->registerClass(&QMainWindow::staticMetaObject); PythonQt::self()->registerClass(&QPushButton::staticMetaObject); ... // do something PythonQt::self()->runScript(mainContext, "print example\n"); PythonQt::self()->runScript(mainContext, "def multiply(a,b):\n return a*b;\n"); QVariantList args; args << 42 << 47; QVariant result = PythonQt::self()->call(mainContext,"multiply", args); ...
1.5.0