[PyQt] PyQt instances as method defaults eval to bool

Phil Thompson phil at riverbankcomputing.com
Tue Oct 19 17:46:14 BST 2010


On Mon, 18 Oct 2010 16:14:50 +0200, "Hans-Peter Jansen" <hpj at urpla.net>
wrote:
> 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. 

It's simply a bug (one of many) in the port from C++. The bool is the
argument from the action's triggered signal.

> 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.

Thanks,
Phil


More information about the PyQt mailing list