[PyKDE] Re: Bug with QScrollView viewport binding

Phil Thompson phil at riverbankcomputing.co.uk
Wed Jun 29 15:11:32 BST 2005


On Wednesday 29 June 2005 1:56 pm, Giovanni Bajo wrote:
> Hello,
>
> -------------------------------
> from qt import *
>
> app = QApplication([])
> sv = QScrollView(None)
> w = QWidget(sv.viewport())
>
> print sv.viewport() is w.parent()
> print sv.viewport(), w.parent()
> print sv.viewport().width()
> print w.parent().width()
> -------------------------------
>
> This outputs:
>
> True
> <qt.QWidget object at 0x00813690>
> <qt.QObject object at 0x00813690>
> 949
> Traceback (most recent call last):
>   File "D:\Work\caligola3d\src\pyqtbug3.py", line 14, in ?
>     print w.parent().width()
> AttributeError: width
>
> Notice that there are two different Python-side objects for the same C++
> reference.

Yes - but they aren't in existence at the same time.

There is no existing Python object that wraps w.parent(), so it gets wrapped 
as a QObject. (Without checking the Qt sources I'd guess it's actual type is 
a Qt internal class.) width is an attribute of QWidget, not QObject.

print seems to be evaluating and releasing each of its arguments one at a 
time, ie. it doesn't evaluate them all, print them all, then release them 
all. So...

print sv.viewport() is w.parent()

...returns True because the sv.viewport() is wrapped as a QWidget and this 
still exists when w.parent() is wrapped. Therefore, w.parent() returns 
another reference to the QWidget and the test succeeds. The QWidget wrapper 
is then garbage collected. When we then get to...

print sv.viewport(), w.parent()

sv.viewport() gets wrapped as a QWidget, gets printed, then the wrapper is 
garbage collected. w.parent() then gets wrapped as a QObject (using the same 
piece of memory). It gets printed, and then the wrapper is garbage collected.

Using a temporary name to keep a reference to sv.viewport() solves the 
problem.

Phil




More information about the PyQt mailing list