[PyQt] Necessary to register type before setting context property?

Elvis Stansvik elvstone at gmail.com
Sun May 8 21:20:34 BST 2016


Hi all,

The docs at

    http://pyqt.sourceforge.net/Docs/PyQt5/qml.html

say

> * Python types that are sub-classed from QObject can be registered with QML.
> * Instances of registered Python types can be created and made available to QML scripts.
> * Instances of registered Python types can be created by QML scripts.
> * Singleton instances of registered Python types can be created automatically by a QML engine and made available to QML scripts.
> * QML scripts interact with Python objects through their properties, signals and slots.
> * Python properties, signals and slots can be given revision numbers that only those implemented by a specific version are made available to QML.

I was wondering about:

> * Instances of registered Python types can be created and made available to QML scripts.

Does this refer to setting context properties on the root context of
the QML engine?

Because I just tried, and this seems to work even with the
registration commented out:

#qmlRegisterUncreatableType(
#    Machine, 'com.example.Machine', 1, 0, 'Machine',
#    'Machine is not instantiable from QML'
#)

machine = Machine()

engine = QQmlApplicationEngine()
engine.rootContext().setContextProperty('machine', machine)

The "machine" property is available in QML, I can bind to its
properties, pass it around as argument in JS function calls and
everything seems to work.

Is there some functionality that will not work when I skip registering
the type with qmlRegister*() ?

Thanks,
Elvis


More information about the PyQt mailing list