Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qHash function for QRect

Tags:

c++

hash

qt

What is the best way to calculate the qHash value of a QRect? I need to use QRect (and maybe QRectF) as the key of QCache. Right now I am using something like this:

inline uint qHash(const QRect & r)
{
 return qHash(QByteArray::fromRawData((const char*)&r, sizeof(r)));
}

It seems to work but I don't like casting it into some raw bytes and since QRect is noy a simple struct, this may break sooner than later in future versions of Qt.

BTW. I don't store the hash values so it doesn't have to be persistent or cross-platform. But it does need to be reliable and fast.

Thanks.

like image 481
Stephen Chu Avatar asked Feb 15 '26 02:02

Stephen Chu


1 Answers

I would simply do return qHash(QString("%1,%2,%3,%4").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height())))

Also I've found this solution: http://thesmithfam.org/blog/2008/01/17/using-qrect-with-qhash/ (read comment)

like image 99
Kamil Klimek Avatar answered Feb 17 '26 14:02

Kamil Klimek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!