Using cpp-objects not derived from QObject
Dynamic Python binding for Qt Applications
Brought to you by:
florianlink,
marcusbarann
It's a pity PythonQt lacks proper documentation. I find this project very useful, and I think it deserves to be better documented. Hope, this will come some day :)
My question is - is it possible to use pure cpp-objects (not subclasses of QObject) in the Python-part of PythonQt without wrapping them with QObject-derived classes?
Say, I have a small cpp-class which represents an item in a collection, and I manipulate large lists of objects of this class. If for each object in a list I need a wrpper to access it properly from the Python, probably that would cause certain overhead due to the meta-object system, which is by itself not needed in that class (no slots/signals, etc.).
Let me reshape the question this way:
Does it make an overhead difference if I directly inherit the class from QObject (even if that is not the design of the class itself) and then register it with
PythonQt::self()->registerClass
, or if I leave it as a pure cpp-class and then wrap it via PythonQtCppWrapperFactory or decorat it with decorators?Last edit: Max 2019-08-11
From a performance and memory consumption point of view, using decorators is the best solution. The decorator QObject only exists once and the decorator slots are cached, so that the lookup is fast. So registering a C++ object and adding decorators is the best. Deriving from QObject will add QObject overhead to your C++ class (which is not much, but if you have thousands of small C++ objects, it's better to avoid it). Using the CPP wrapper factory has extra overhead, since it creates a wrapper QObject for each of your C++ objects, so it has the worst performance.
Thanks. So I guess I should follow the
PyCPPWrapperExample
example provided in PythonQt source-codes. If I understand correctly fromCustomObjects.h
, this example is exactly about using decorators rather than factory.