[PyQt] identify a QTreeWidgetItem

Michael Towers gradgrind at online.de
Tue Oct 23 18:42:39 BST 2007


alteo_gange wrote:
> Le mardi 23 octobre 2007, alteo_gange a écrit :
>   
>> Hi everybody!
>>
>> I have created several QTreeWidgetItem and connected a signal
>> "itemClicked(QTreeWidgetItem *,int)" on the QTreeWidget.
>>
>>     
>>> treeWidget=QtGui.QTreeWidget(widget)
>>> ...
>>> itemTree1=QtGui.QTreeWidgetItem(treeWidget)
>>> ...
>>> itemTree2=QtGui.QTreeWidgetItem(treeWidget)
>>> ...
>>> self.connect(treeWidget,QtCore.SIGNAL("itemClicked(QTreeWidgetItem
>>> *,int)"),self.function)
>>> ...
>>> def function(self, item):
>>>         print item
>>>       
>> 'print item' return:
>> <PyQt4.QtGui.QTreeWidgetItem object at 0x82cf62c>.
>>
>>
>> It's very abstract!
>>
>> I must identify QTreeWidgetItem in order to connect it an action (view a
>> widget on the right), but how? With a method of the item reference? Which?
>> I don't know.
>>     
>
> With the .text() method, i can recover the QTreeWidgetItem name:
>  
>   
>> print item.text(0), "\n"
>>     
>
> But with no-ascii characters (ex: itemTree.setText(0,_(u"Filtres vidéos"))), 
> there is an error:
>
>   
>> Traceback (most recent call last):
>>  File "/home/login/docs/langages/python/pyqt4/QTreeWidget.py", line 126, in   
>>     
> fonction
>   
>>    print item.text(0), "\n"
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 
>> 11: ordinal not in range(128)
>>     
I find this one of the most annoying things in PyQt, it confuses me 
terribly. I think in this case you can do (for example)

print unicode(item.text(0)), "\n"

(the original result from item.text() being a QString)

But it may be that you need to specify an encoding and use the
encode or decode methods of unicode or string ...



More information about the PyQt mailing list