On Tuesday 05 October 2010, 18:20:30 Phil Thompson wrote: > On Tue, 5 Oct 2010 18:07:25 +0200, "Hans-Peter Jansen" <h...@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" <h...@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
--- ./lightmaps-2.py 2010-10-05 10:13:07.601486236 +0200 +++ ./lightmaps.py 2010-10-05 19:25:10.493646218 +0200 @@ -27,13 +27,10 @@ HOLD_TIME = 701 # Hint: see above to find why I picked self one :) MAX_MAGNIFIER = 229 - -def qHash(p): - return p.x() * 17 ^ p.y() - # tile size in pixels TDIM = 256 + class Point(QtCore.QPoint): """QPoint, that is fully qualified as a dict key""" def __init__(self, *par): @@ -43,7 +40,7 @@ class Point(QtCore.QPoint): super(Point, self).__init__() def __hash__(self): - return self.y() << 24 + self.x() + return self.x() * 17 ^ self.y() def __repr__(self): return "Point(%s, %s)" % (self.x(), self.y())
_______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt