mirror of
https://github.com/ModOrganizer2/modorganizer-plugin_python.git
synced 2026-07-27 14:03:33 -07:00
Prevent the C++ part of PyQt objects being deleted while the Python component is still accessible.
This commit is contained in:
@@ -499,17 +499,33 @@ struct QClass_converters
|
||||
{
|
||||
struct QClass_to_PyQt
|
||||
{
|
||||
template <typename Q>
|
||||
static typename std::enable_if_t<std::is_copy_constructible_v<Q>, T*> getSafeCopy(T *qClass)
|
||||
{
|
||||
return new T(*qClass);
|
||||
}
|
||||
|
||||
template <typename Q>
|
||||
static typename std::enable_if_t<!std::is_copy_constructible_v<Q>, T*> getSafeCopy(T *qClass)
|
||||
{
|
||||
return qClass;
|
||||
}
|
||||
|
||||
static PyObject *convert(const T &object) {
|
||||
const sipTypeDef *type = sipAPI()->api_find_type(MetaData<T>::className());
|
||||
if (type == nullptr) {
|
||||
return bpy::incref(Py_None);
|
||||
}
|
||||
|
||||
PyObject *sipObj = sipAPI()->api_convert_from_type((void*)(&object), type, 0);
|
||||
PyObject *sipObj = sipAPI()->api_convert_from_type((void*)getSafeCopy<T>((T*)&object), type, 0);
|
||||
if (sipObj == nullptr) {
|
||||
return bpy::incref(Py_None);
|
||||
}
|
||||
|
||||
if (std::is_copy_constructible_v<T>)
|
||||
// Ensure Python deletes the C++ component
|
||||
sipAPI()->api_transfer_back(sipObj);
|
||||
|
||||
return bpy::incref(sipObj);
|
||||
}
|
||||
|
||||
@@ -523,11 +539,15 @@ struct QClass_converters
|
||||
return bpy::incref(Py_None);
|
||||
}
|
||||
|
||||
PyObject *sipObj = sipAPI()->api_convert_from_type(object, type, 0);
|
||||
PyObject *sipObj = sipAPI()->api_convert_from_type(getSafeCopy<T>(object), type, 0);
|
||||
if (sipObj == nullptr) {
|
||||
return bpy::incref(Py_None);
|
||||
}
|
||||
|
||||
if (std::is_copy_constructible_v<T>)
|
||||
// Ensure Python deletes the C++ component
|
||||
sipAPI()->api_transfer_back(sipObj);
|
||||
|
||||
return bpy::incref(sipObj);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user