to/from a Python array
+
+ \section FeaturesQtAll Features (with PythonQt_QtAll linked in)
+
+ Thanks to the new wrapper generator, PythonQt now offers the additional PythonQt_QtAll library which wraps the complete Qt API, including all C++ classes and all non-slots on QObject derived classes.
+ This offers the following features:
+
+ - Complete Qt API wrapped and accessible
+ - The following modules are available as submodule of the PythonQt module:
+ - QtCore
+ - QtGui
+ - QtNetwork
+ - QtOpenGL
+ - QtSql
+ - QtSvg
+ - QtUiTools
+ - QtWebKit
+ - QtXml
+ - QtXmlPatterns
+ - (phonon, QtHelp, assistant, designer are currently not supported, this would require some additional effort on the code generator)
+ - For convenience, all classes are also available in the PythonQt.Qt module, for people who do not care in which module a class is located
+ - Any Qt class that has virtual methods can be easily derived from Python and the virtual methods can be reimplemented in Python
+ - Polymorphic downcasting on QEvent, QGraphicsItem, QStyleOption, ...
+ - Multiple inheritance support (e.g. QGraphicsTextItem is a QObject AND a QGraphicsItem, PythonQt will handle this well)
+
+ \section Comparision Comparision with PyQt
+
+ - PythonQt is not as Pythonic as PyQt in many details (e.g. operator mapping, pickling, translation support, ...) and it is maily thought for embedding and intercommunication between Qt/Cpp and Python
+ - PythonQt allows to communicate in both directions, e.g. calling a Python object from C++ AND calling a C++ method from Python, while PyQt only handles the Python->C++ direction
+ - PythonQt offers properties as Python attributes, while PyQt offers them as setter/getter methods (e.g. QWidget.width is a property in PythonQt and a method in PyQt)
+ - PythonQt does not auto-convert objects, e.g. when a QPainter expects a QBrush and you pass a QColor, it is rejected, you will need to write QBrush(QColor(1,2,3)) instead
+ - Probably there are lots of details that differ, I do not know PyQt that well to list them all.
- \section Non-Features
-
- Features that PythonQt does NOT support (and will not support):
-
- - you can not derive from QObjects inside of Python, this would require wrapper generation like PyQt does
- - you can only script QObject derived classes, for normal C++ classes you need to create a PythonQtCppWrapperFactory and adequate wrapper classes or add decorator slots
- - you can not access normal member functions of QObjects, only slots and properties, because the \b moc does not store normal member functions in the MetaObject system
\section Interface
The main interface to PythonQt is the PythonQt singleton.
PythonQt needs to be initialized via PythonQt::init() once.
Afterwards you communicate with the singleton via PythonQt::self().
- PythonQt offers a default binding for the complete QWidget set, which
- needs to be enabled via PythonQtGui::init().
+ PythonQt offers a complete Qt binding, which
+ needs to be enabled via PythonQt_QtAll::init().
\section Datatype Datatype Mapping
@@ -127,7 +175,8 @@
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() |
+ OwnRegisteredMetaType | C++ wrapper, optionally with additional information/wrapping provided by registerCPPClass() |
+ QList | converts to a list of CPP wrappers |
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 |
@@ -162,6 +211,7 @@
In addition to this, the wrapped objects support
- className() - returns a string that reprents the classname of the QObject
- help() - shows all properties, slots, enums, decorator slots and constructors of the object, in a printable form
+ - delete() - deletes the object (use with care, especially if you passed the ownership to C++)
- connect(signal, function) - connect the signal of the given object to a python function
- connect(signal, qobject, slot) - connect the signal of the given object to a slot of another QObject
- disconnect(signal, function) - disconnect the signal of the given object from a python function
@@ -185,8 +235,8 @@
\section CPP CPP Wrapping
-You can create dedicated wrapper QObject for any C++ class. This is done by deriving from PythonQtCppWrapperFactory
-and adding your factory via addWrapperFactory().
+You can create dedicated wrapper QObjects for any C++ class. This is done by deriving from PythonQtCppWrapperFactory
+and adding your factory via addWrapperFactory().
Whenever PythonQt encounters a CPP pointer (e.g. on a slot or signal)
and it does not known it as a QObject derived class, it will create a generic CPP wrapper. So even unknown C++ objects
can be passed through Python. If the wrapper factory supports the CPP class, a QObject wrapper will be created for each
@@ -194,33 +244,34 @@ instance that enters Python. An alternative to a complete wrapper via the wrappe
\section MetaObject Meta Object/Class access
-For each known CPP class, QObject derived class and QVariant type, PythonQt provides a Meta class. These meta classes are visible
-inside of the "PythonQt" python module.
+For each known C++ class, PythonQt provides a Python class. These classes are visible
+inside of the "PythonQt" python module or in subpackages if a package is given when the class is registered.
A Meta class supports:
- access to all declared enum values
- constructors
-- static decorator slots
+- static methods
+- unbound non-static methods
- help() and className()
-From within Python, you can import the module "PythonQt" to access these meta objects and the Qt namespace.
+From within Python, you can import the module "PythonQt" to access these classes and the Qt namespace.
\code
-from PythonQt import *
+from PythonQt import QtCore
# namespace access:
-print Qt.AlignLeft
+print QtCore.Qt.AlignLeft
# constructors
-a = QSize(12,13)
-b = QFont()
+a = QtCore.QSize(12,13)
+b = QtCore.QFont()
# static method
-QDate.currentDate()
+QtCore.QDate.currentDate()
# enum value
-QFont.UltraCondensed
+QtCore.QFont.UltraCondensed
\endcode
@@ -243,7 +294,6 @@ The basic idea about decorators is to create a QObject derived class that implem
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.
-- QVariant new_SomeClassName(...) - defines a constructor for "SomeClassName" that returns a QVariant
- SomeClassName* new_SomeClassName(...) - defines a constructor for "SomeClassName" that returns a new object of type SomeClassName (where SomeClassName can be any CPP class, not just QObject classes)
- void delete_SomeClassName(SomeClassName* o) - defines a destructor, which should delete the passed in object o
- anything static_SomeClassName_someMethodName(...) - defines a static method that is callable on instances and the meta class
@@ -272,8 +322,8 @@ 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 to QSize that takes a QPoint
+ QSize* new_QSize(const QPoint& p) { return new 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); }
@@ -300,8 +350,7 @@ public slots:
...
PythonQt::self()->addDecorators(new ExampleDecorator());
-PythonQt::self()->registerClass(&QPushButton::staticMetaObject);
-PythonQt::self()->registerCPPClassNames(QStringList() << "YourCPPObject");
+PythonQt::self()->registerCPPClass("YourCPPObject");
\endcode
@@ -309,13 +358,13 @@ After you have registered an instance of the above ExampleDecorator, you can do
(all these calls are mapped to the above decorator slots):
\code
-from PythonQt import *
+from PythonQt import QtCore, QtGui, YourCPPObject
# call our new constructor of QSize
-size = QSize(QPoint(1,2));
+size = QtCore.QSize(QPoint(1,2));
# call our new QPushButton constructor
-button = QPushButton("sometext");
+button = QtGui.QPushButton("sometext");
# call the move slot (overload1)
button.move(QPoint(0,0))
@@ -324,7 +373,7 @@ button.move(QPoint(0,0))
button.move(0,0)
# call the static method
-grabber = QWidget.mouseWrapper();
+grabber = QtGui.QWidget.mouseWrapper();
# create a CPP object via constructor
yourCpp = YourCPPObject(1,11.5)
@@ -437,6 +486,7 @@ the python2x.[lib | dll | so | dynlib].
// 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();
@@ -444,11 +494,6 @@ the python2x.[lib | dll | so | dynlib].
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");