[PyKDE] Can I use QString as key in Python dictionary?

Sebastian Kügler sebas at kde.nl
Thu Jun 2 03:35:05 BST 2005


On Thursday 02 June 2005 04:10, Matej Cepl wrote:
> I have this simple dialog in PyQt, but whenever I press any item in
> the list (or click OK), I get this error:
>
>         chelcicky:pycatsel$ ./pycatsel
>         Traceback (most recent call last):
>           File
> "/home/matej/archiv/programky/vim-qadas/pycatsel/selectCode.py",\
> line 148, in Quit
>             note = unicode(self.dictCategories[key])
>         KeyError: <__main__.qt.QString object at 0xb7e12c5c>
>         chelcicky:pycatsel$
>
> Does anybody have any idea, what's wrong? Can I use QString as a key
> in Python dictionary (both key and dict's values are QStrings)?

>>> from qt import *
>>> QString("foo") is QString("foo")
False

As you can see these two QString are different objects, so there's 
actually no such key.

>>> k = QString("bla")
>>> mydict = {QString("bla"):"foobar"}
>>> mydict[k]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: <qt.QString object at 0xb7df350c>

As you can see, it yields a KeyError because k is actually no key in 
mydict. However, you can use a QString as a key, but you have to use 
exactly that reference (not a *similar* one):

>>> k = QString("bla")
>>> mydict = {k:"foobar"}
>>> mydict[k]
'foobar'

Cheers,

sebas
-- 
  http://vizZzion.org   |   GPG Key ID: 9119 0EF9 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Political language [...] is designed to make lies sound truthful and 
murder respectable, and to give an appearance of solidity to pure wind. 
- George Orwell, 1984

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050602/935c476c/attachment.bin


More information about the PyQt mailing list