diff options
Diffstat (limited to 'sources/pyside6/PySide6')
| -rw-r--r-- | sources/pyside6/PySide6/QtQml/pysideqmlregistertype.cpp | 58 | ||||
| -rw-r--r-- | sources/pyside6/PySide6/QtQml/pysideqmlregistertype.h | 16 | ||||
| -rw-r--r-- | sources/pyside6/PySide6/QtQml/typesystem_qml.xml | 4 | ||||
| -rw-r--r-- | sources/pyside6/PySide6/glue/qtqml.cpp | 5 |
4 files changed, 75 insertions, 8 deletions
diff --git a/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.cpp b/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.cpp index d2b9b689e..873ec53ce 100644 --- a/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.cpp +++ b/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.cpp @@ -180,15 +180,10 @@ int PySide::qmlRegisterSingletonType(PyObject *pyObj, const char *uri, int versi const QMetaObject *metaObject = nullptr; if (isQObject) { - static PyTypeObject *qobjectType = Conversions::getPythonTypeObject("QObject*"); - assert(qobjectType); - PyTypeObject *pyObjType = reinterpret_cast<PyTypeObject *>(pyObj); - if (!PySequence_Contains(pyObjType->tp_mro, reinterpret_cast<PyObject *>(qobjectType))) { - PyErr_Format(PyExc_TypeError, "A type inherited from %s expected, got %s.", - qobjectType->tp_name, pyObjType->tp_name); + + if (!isQObjectDerived(pyObjType, true)) return -1; - } // If we don't have a callback we'll need the pyObj to stay allocated indefinitely if (!hasCallback) @@ -273,6 +268,55 @@ int PySide::qmlRegisterSingletonType(PyObject *pyObj, const char *uri, int versi return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &type); } +int PySide::qmlRegisterSingletonInstance(PyObject *pyObj, const char *uri, int versionMajor, + int versionMinor, const char *qmlName, + PyObject *instanceObject) +{ + using namespace Shiboken; + + static PyTypeObject *qobjectType = Conversions::getPythonTypeObject("QObject*"); + assert(qobjectType); + + // Check if the Python Type inherit from QObject + PyTypeObject *pyObjType = reinterpret_cast<PyTypeObject *>(pyObj); + + if (!isQObjectDerived(pyObjType, true)) + return -1; + + // Check if the instance object derives from QObject + PyTypeObject *typeInstanceObject = instanceObject->ob_type; + + if (!isQObjectDerived(typeInstanceObject, true)) + return -1; + + // Convert the instanceObject (PyObject) into a QObject + QObject *instanceQObject = reinterpret_cast<QObject*>( + Object::cppPointer(reinterpret_cast<SbkObject*>(instanceObject), qobjectType)); + + // Create Singleton Functor to pass the QObject to the Type registration step + // similarly to the case when we have a callback + QQmlPrivate::SingletonFunctor registrationFunctor; + registrationFunctor.m_object = instanceQObject; + + const QMetaObject *metaObject = PySide::retrieveMetaObject(pyObjType); + Q_ASSERT(metaObject); + + QQmlPrivate::RegisterSingletonType type; + type.structVersion = 0; + + type.uri = uri; + type.version = QTypeRevision::fromVersion(versionMajor, versionMinor); + type.typeName = qmlName; + type.instanceMetaObject = metaObject; + + // FIXME: Fix this to assign new type ids each time. + type.typeId = QMetaType(QMetaType::QObjectStar); + type.qObjectApi = registrationFunctor; + + + return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &type); +} + extern "C" { diff --git a/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.h b/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.h index 9cc7379e9..2a90a07e0 100644 --- a/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.h +++ b/sources/pyside6/PySide6/QtQml/pysideqmlregistertype.h @@ -71,7 +71,7 @@ int qmlRegisterType(PyObject *pyObj, const char *uri, int versionMajor, int vers const char *qmlName, const char *noCreationReason = nullptr, bool creatable = true); /** - * PySide implementation of qmlRegisterType<T> function. + * PySide implementation of qmlRegisterSingletonType<T> function. * * \param pyObj Python type to be registered. * \param uri QML element uri. @@ -84,6 +84,20 @@ int qmlRegisterType(PyObject *pyObj, const char *uri, int versionMajor, int vers int qmlRegisterSingletonType(PyObject *pyObj,const char *uri, int versionMajor, int versionMinor, const char *qmlName, PyObject *callback, bool isQObject, bool hasCallback); +/** + * PySide implementation of qmlRegisterSingletonInstance<T> function. + * + * \param pyObj Python type to be registered. + * \param uri QML element uri. + * \param versionMajor QML component major version. + * \param versionMinor QML component minor version. + * \param qmlName QML element name + * \param instanceObject singleton object to be registered. + * \return the metatype id of the registered type. + */ +int qmlRegisterSingletonInstance(PyObject *pyObj, const char *uri, int versionMajor, + int versionMinor, const char *qmlName, PyObject *instanceObject); + /** * PySide implementation of the QML_ELEMENT macro diff --git a/sources/pyside6/PySide6/QtQml/typesystem_qml.xml b/sources/pyside6/PySide6/QtQml/typesystem_qml.xml index 1b855a073..ad883f09d 100644 --- a/sources/pyside6/PySide6/QtQml/typesystem_qml.xml +++ b/sources/pyside6/PySide6/QtQml/typesystem_qml.xml @@ -92,6 +92,10 @@ <inject-code class="target" file="../glue/qtqml.cpp" snippet="qmlregistersingletontype_qjsvalue"/> </add-function> + <add-function signature="qmlRegisterSingletonInstance(PyTypeObject,const char*,int,int,const char*,PyObject*)" return-type="int"> + <inject-code class="target" file="../glue/qtqml.cpp" snippet="qmlregistersingletoninstance"/> + </add-function> + <add-function signature="qmlRegisterUncreatableType(PyTypeObject,const char*,int,int,const char*,const char*)" return-type="int"> <inject-code class="target" file="../glue/qtqml.cpp" snippet="qmlregisteruncreatabletype"/> </add-function> diff --git a/sources/pyside6/PySide6/glue/qtqml.cpp b/sources/pyside6/PySide6/glue/qtqml.cpp index eab6eab95..99a1c441a 100644 --- a/sources/pyside6/PySide6/glue/qtqml.cpp +++ b/sources/pyside6/PySide6/glue/qtqml.cpp @@ -57,6 +57,11 @@ int %0 = PySide::qmlRegisterSingletonType(nullptr, %ARGUMENT_NAMES, false, true) %PYARG_0 = %CONVERTTOPYTHON[int](%0); // @snippet qmlregistersingletontype_qjsvalue +// @snippet qmlregistersingletoninstance +int %0 = PySide::qmlRegisterSingletonInstance(%ARGUMENT_NAMES); +%PYARG_0 = %CONVERTTOPYTHON[int](%0); +// @snippet qmlregistersingletoninstance + // @snippet qmlregisteruncreatabletype int %0 = PySide::qmlRegisterType(%ARGUMENT_NAMES, false); %PYARG_0 = %CONVERTTOPYTHON[int](%0); |
