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

Torsten Marek shlomme at gmx.net
Thu Jun 2 07:08:34 BST 2005

Hash: SHA1

Sebastian Kügler schrieb:
> 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
>>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"}
> 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"}
> 'foobar'

One could think about changing the QString implementation of __hash__ to use the
strings's contents only. This shouldn't be too difficult (although I didn't try
it out, so I'm probably wrong.).



- --
Torsten Marek <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net

Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


More information about the PyQt mailing list