[PyQt] PyQt instances as method defaults eval to bool

Hans-Peter Jansen hpj at urpla.net
Mon Oct 18 15:14:50 BST 2010


Hi Phil,

current sip and PyQt show some issues with PyQt instances, that are used 
as method defaults, e.g. the usual "parent = QtCore.QModelIndex()" in 
Qt's model/view paradigm. 

Run the itemviews/editabletreemodel.py example, select an item, and try 
to insert or remove a column. Results in traceback:

Traceback (most recent call last):
  File "./editabletreemodel.py", line 340, in insertColumn
    changed = model.insertColumn(column + 1, parent)
TypeError: QAbstractItemModel.insertColumn(int, QModelIndex parent=QModelIndex()): 
	   argument 2 has unexpected type 'bool' 
Traceback (most recent call last):
  File "./editabletreemodel.py", line 367, in removeColumn
    changed = model.removeColumn(column, parent)
TypeError: QAbstractItemModel.removeColumn(int, QModelIndex parent=QModelIndex()): 
           argument 2 has unexpected type 'bool'

For some reason, the parent args in:

    def insertColumn(self, parent=QtCore.QModelIndex()):
    def removeColumn(self, parent=QtCore.QModelIndex()):

evaluate to bool, but should resolve to PyQt4.QtCore.QModelIndex 
instances of course. I know, that there are common Python pitfalls, 
where mutable types as default arguments results in mostly unexpected 
behaviour, but that should not be the case here. 

In this context, I noticed, that some examples return None instead of 
QVariant(), which is related to:

    sip.setapi('QVariant', 2)

But this very setting leads to:

Traceback (most recent call last):
  File "./editabletreemodel.py", line 327, in insertChild
    if not model.headerData(column, QtCore.Qt.Horizontal).isValid():
AttributeError: 'str' object has no attribute 'isValid'

when trying insert a child. Just removing .isValid() in line 327

            if not model.headerData(column, QtCore.Qt.Horizontal).isValid():

should be sufficient to fix this issue.

Pete


More information about the PyQt mailing list