[PyQt] LightMaps example

Hans-Peter Jansen hpj at urpla.net
Tue Oct 5 18:32:28 BST 2010


On Tuesday 05 October 2010, 18:20:30 Phil Thompson wrote:
> On Tue, 5 Oct 2010 18:07:25 +0200, "Hans-Peter Jansen" <hpj at urpla.net>
>
> wrote:
> > On Tuesday 05 October 2010, 17:34:36 Phil Thompson wrote:
> >> On Tue, 5 Oct 2010 10:42:43 +0200, "Hans-Peter Jansen" <hpj at urpla.net>
> >>
> >> > here's another nice example for the collection.
> >> >
> >> > LightMaps
> >>
> >> Thanks - it will be in tonight's snapshot.
> >>
> >> > Phil: it might be a good idea to consider adding __hash__ to
> >> > assorted Qt
> >> >
> >> > classes in order to improve their pythonic appearance, e,g, QPoint,
> >>
> >> QRect,
> >>
> >> > QColor. What do you think?
> >>
> >> __hash__ is implemented for all classes that implement qHash(). I
>
> assume
>
> >> that that covers the sensible ones.
> >
> > Unfortunately qHash(QPoint) is missing. :-(
> >
> > What a pity. Would you take one?
>
> No. Raise it as a bug against Qt.

Done: http://bugreports.qt.nokia.com/browse/QTBUG-14263
but please notice, that Qt has an advantage in this area, since with C++ you 
only need to define a qHash function somewhere in your code with the right 
signature to be used automatically (well, I guess, the compiler will throw 
an error, if it is misssing), while this won't work with PyQt, where this 
silently breaks, and I can imagine, that you don't want to pick up qHash 
dynamically ;-).

>>> from PyQt4.QtCore import *
>>> d = {}
>>> for i in range(10):
...  p = QPoint(42, 42)
...  d[p] = True
... 
>>> print d
{PyQt4.QtCore.QPoint(42, 42): True, PyQt4.QtCore.QPoint(42, 42): True, 
PyQt4.QtCore.QPoint(42, 42): True, PyQt4.QtCore.QPoint(42, 42): True, 
PyQt4.QtCore.QPoint(42, 42): True, PyQt4.QtCore.QPoint(42, 42): True, 
PyQt4.QtCore.QPoint(42, 42): True, PyQt4.QtCore.QPoint(42, 42): True, 
PyQt4.QtCore.QPoint(42, 42): True, PyQt4.QtCore.QPoint(42, 42): True}
>>> 
>>> class Point(QPoint):
...     def __init__(self, *par):
...         if par:
...             super(Point, self).__init__(*par)
...         else:
...             super(Point, self).__init__()
...         
...     def __hash__(self):
...         return self.y() << 24 + self.x()
... 
>>> d = {}
>>> for i in range(10):
...  p = Point(42, 42)
...  d[p] = True
... 
>>> print d
{PyQt4.QtCore.QPoint(42, 42): True}
>>> 

Anyway, here's the diff for the example to remove the silly C++ qHash 
appendix, but adopt it accordingly in the Point.__hash__ method.

Pete
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lightmaps.diff
Type: text/x-diff
Size: 710 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101005/c4d79efc/attachment.diff>


More information about the PyQt mailing list