QFont: add missing qHash overload

The properties that make up the hash value are chosen to be the same as those
that make up QFontDef's op<() and op==(). Indeed, the implementation for QFont
simply delegates to the one of QFontDef, which has been added for this purpose,
but may prove useful in its own right down the line.

The code would greatly benefit from a qHash(qreal) implementation. Lacking
this, the patch uses multiplication with 10000 and qRound64() to convert
the one floating-point property used in the hash to an integer. This is
probably the right thing to do anyway, to avoid epsilon problems.

[ChangeLog][QtGui][QFont] Added qHash overload for this class.

[ChangeLog][QtCore][QHash] Allowed QFont to be used as a key in QHash/QSet.

Change-Id: I2c1cb5d9da53e26cb2c0f1a7c357731e73eea78e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2014-02-05 15:15:01 +01:00 committed by The Qt Project
parent 6afc34a72f
commit d24cceebf2
3 changed files with 31 additions and 0 deletions

View File

@ -2073,6 +2073,18 @@ QString QFont::toString() const
QString::number((int) rawMode());
}
/*!
Returns the hash value for \a font. If specified, \a seed is used
to initialize the hash.
\relates QFont
\since 5.3
*/
uint qHash(const QFont &font, uint seed) Q_DECL_NOTHROW
{
return qHash(QFontPrivate::get(font)->request, seed);
}
/*!
Sets this font to match the description \a descrip. The description

View File

@ -315,6 +315,8 @@ private:
Q_DECLARE_SHARED(QFont)
Q_GUI_EXPORT uint qHash(const QFont &font, uint seed = 0) Q_DECL_NOTHROW;
inline bool QFont::bold() const
{ return weight() > Normal; }

View File

@ -55,6 +55,7 @@
#include "QtGui/qfont.h"
#include "QtCore/qmap.h"
#include "QtCore/qhash.h"
#include "QtCore/qobject.h"
#include "QtCore/qstringlist.h"
#include <QtGui/qfontdatabase.h>
@ -133,6 +134,22 @@ struct QFontDef
}
};
inline uint qHash(const QFontDef &fd, uint seed = 0) Q_DECL_NOTHROW
{
return qHash(qRound64(fd.pixelSize*10000)) // use only 4 fractional digits
^ qHash(fd.weight)
^ qHash(fd.style)
^ qHash(fd.stretch)
^ qHash(fd.styleHint)
^ qHash(fd.styleStrategy)
^ qHash(fd.ignorePitch)
^ qHash(fd.fixedPitch)
^ qHash(fd.family, seed)
^ qHash(fd.styleName)
^ qHash(fd.hintingPreference)
;
}
class QFontEngineData
{
public: