Fixed reference leak on uiloader.

Fixes bug #392

Reviewer: Luciano Wolf <luciano.wolf@openbossa.org>
          Marcelo Lira <marcelo.lira@openbossa.org>
This commit is contained in:
renatofilho 2010-10-13 16:42:50 -03:00
commit e71b215f0b
3 changed files with 66 additions and 5 deletions

View file

@ -91,7 +91,7 @@ QString PyCustomWidget::whatsThis() const
} }
QWidget *PyCustomWidget::createWidget(QWidget *parent) QWidget *PyCustomWidget::createWidget(QWidget *parent)
{ {
//Create a python instance and return cpp object //Create a python instance and return cpp object
PyObject* pyParent; PyObject* pyParent;
bool unkowParent = false; bool unkowParent = false;
@ -100,8 +100,11 @@ QWidget *PyCustomWidget::createWidget(QWidget *parent)
if (!pyParent) { if (!pyParent) {
pyParent = Shiboken::Converter<QWidget*>::toPython(parent); pyParent = Shiboken::Converter<QWidget*>::toPython(parent);
unkowParent = true; unkowParent = true;
} else {
Py_INCREF(pyParent);
} }
} else { } else {
Py_INCREF(Py_None);
pyParent = Py_None; pyParent = Py_None;
} }
@ -110,14 +113,14 @@ QWidget *PyCustomWidget::createWidget(QWidget *parent)
//Call python constructor //Call python constructor
PyObject* result = PyObject_CallObject(m_data->pyObject, pyArgs); PyObject* result = PyObject_CallObject(m_data->pyObject, pyArgs);
QWidget* widget = 0; QWidget* widget = 0;
if (result) { if (result) {
if (unkowParent) //if parent does not exists in python, transfer the ownership to cpp if (unkowParent) //if parent does not exists in python, transfer the ownership to cpp
Shiboken::BindingManager::instance().transferOwnershipToCpp(result); Shiboken::BindingManager::instance().transferOwnershipToCpp(result);
else else
Shiboken::setParent(pyParent, result); Shiboken::setParent(pyParent, result);
widget = reinterpret_cast<QWidget*>(Shiboken::getCppPointer(result, result->ob_type)); widget = reinterpret_cast<QWidget*>(Shiboken::getCppPointer(result, result->ob_type));
} }

View file

@ -2,7 +2,7 @@ import unittest
import os import os
from helper import UsesQApplication from helper import UsesQApplication
from PySide import QtCore, QtGui, QtDeclarative from PySide import QtGui, QtDeclarative
from PySide.QtUiTools import QUiLoader from PySide.QtUiTools import QUiLoader
class MyWidget(QtGui.QComboBox): class MyWidget(QtGui.QComboBox):
@ -40,6 +40,16 @@ class BugTest(UsesQApplication):
self.assert_(isinstance(result.custom, MyWidget)) self.assert_(isinstance(result.custom, MyWidget))
self.assert_(result.custom.isPython()) self.assert_(result.custom.isPython())
def testPythonCustomWidgetsTwice(self):
w = QtGui.QWidget()
loader = QUiLoader()
loader.registerCustomWidget(MyWidget)
filePath = os.path.join(os.path.dirname(__file__), 'pycustomwidget2.ui')
result = loader.load(filePath, w)
self.assert_(isinstance(result.custom, MyWidget))
self.assert_(isinstance(result.custom2, MyWidget))
self.assert_(result.custom.isPython())
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>qwidget</class>
<widget class="QWidget" name="qwidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<widget class="MyWidget" name="custom">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>79</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="MyWidget" name="custom2">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>79</width>
<height>23</height>
</rect>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>MyWidget</class>
<extends>QComboBox</extends>
<header>customwidget</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>