[PyQt] PyQt4 bug bool(QComboBox())

Sebastian Sauer mail at dipe.org
Tue Jul 30 18:31:20 BST 2013


On 07/30/2013 02:48 AM, Baz Walter wrote:
> On 29/07/13 17:52, Sebastian Sauer wrote:
>>  >>> from PyQt4 import QtGui, QtCore
>>  >>> import sys
>>  >>> app = QtGui.QApplication(sys.argv)
>>  >>> c = QtGui.QComboBox()
>>  >>> c
>> <PyQt4.QtGui.QComboBox object at 0x83001dc>
>>  >>> isinstance(c,QtCore.QObject)
>> True
>>  >>> bool(c)
>> False
>>
>> That, bool(c) returns False, is not expected or wanted or is it?
>
> >>> len(c)
> 0
> >>> c.addItem('foo')
> >>> len(c)
> 1
> >>> bool(c)
> True
>

Thanks for the reply!

I see, same with e.g. QListWidget but then:

 >>> c = QtGui.QTreeWidget()
 >>> bool(c)
True

 >>> c = QtGui.QTableWidget()
 >>> bool(c)
True

 >>> c = QtGui.QLineEdit()
 >>> bool(c)
True

 >>> c = QtGui.QComboBox()
 >>> c.setEditable(True)
 >>> c.setEditText("test")
 >>> bool(c)
False

 >>> c = QtGui.QComboBox()
 >>> v = QtGui.QTreeWidget()
 >>> c.setModel(v.model())
 >>> c.setView(v)
 >>> bool(c)
False
 >>> v.addTopLevelItems([QtGui.QTreeWidgetItem()])
 >>> bool(c)
True

Somehow illogical or? Sure, when poking around I see e.g. QComboBox 
implements __len__ and not __nonzero__/__bool__, whereas 
QTreeWidget-item isn't iterable, etc and that explains the results. 
Anyhow, lesson learned: be precise when using isTrue. Thanks! :)



More information about the PyQt mailing list