[PyQt] strange behavior in PyQt4/uic/uiparser.py of PyQt4 4.3.3
Alberto Berti
alberto at metapensiero.it
Fri Apr 25 01:36:33 BST 2008
Hello all,
I have some code that while uiparser.py reads an .ui file, it hooks in
the process and binds the widget to the correct data context (read:
tablemodel). It uses the child to parent widget "navigation" using
QObject.parent() to find out the correct context for a widget. It
works very well, except for some cases where parent chain is broken
and so an exception is raised. This error happens only when one of the
widgets which are "decorated" is contained, for example, in a
QTabWidget's page. The interesting piece of the .ui file appears like
this:
<widget class="QTabWidget" name="tabWidget" >
<property name="geometry" >
<rect>
...
</rect>
</property>
<property name="tabShape" >
<enum>QTabWidget::Rounded</enum>
</property>
<widget class="QWidget" name="tab" >
<property name="geometry" >
<rect>
...
</rect>
</property>
<attribute name="title" >
<string>Tab 1</string>
</attribute>
<widget class="QComboBox" name="tipologiaoperazione" >
<property name="geometry" >
<rect>
...
</rect>
</property>
<property name="column" stdset="0" >
<string>tipologiaoperazione</string>
</property>
</widget>
</widget>
</widget>
So the QTabWidget's page is "rendered" by Qt Designer as a simple
QWidget, and this is the object where the .parent() chain
breaks. While looking for source of this problem, I discovered a
strange piece of code in UIParser.createWidget() around line 147:
parent = self.stack.topwidget
if isinstance(parent, (QtGui.QToolBox, QtGui.QTabWidget,
QtGui.QStackedWidget)):
parent = None
#^^^^^^^^^^^^^^here is the strange point
self.stack.push(self.setupObject(widgetClass(elem), parent, elem))
#^^^^^^^^^^^^^^^ this calls my code
So in this case, the parent object is deliberately set to None when
the QTabWidget's page gets created!
What's the reason of such behavior?
Please note that if i replace the "parent = None" line with a "pass"
all goes on fine an the form shown without any problems, with the
right page shown and the tab swiching working fine.
tanks in advance
Alberto
P.S. all the code developed for this application is available for
review or download at http://www.pypapi.org. It will also be shown at
next PyCon.It next month.
More information about the PyQt
mailing list