QT Bug: Mouse position inaccuracy in macOS Monterey
-
When the mouse is not moved, press the left mouse button, press the right mouse button correctly, It will cause the coordinate judgment click event to fail. Mac OS 11 is OK.
void mousePressEvent(event) {
pressedPoint = event->position();
}
void mouseReleaseEvent(event) {
qDebug() << pressedPoint << event->position();
qDebug() << (pressedPoint.x() - event->position().x()) << (pressedPoint.y() - event->position().y())
qDebug() << qFuzzyCompare(pressedPoint.x(), event->position().x()) << qFuzzyCompare(pressedPoint.y(), event->position().y())
}output:
QPointF(146.678,11.3472) QPointF(146.678 11.3472)
0 -2.44141e-05
true falsesolution:
template<typename T>
inline static bool equal(T a, T b, int decimals = 6)
{
T d = static_cast<T>(pow(10, -decimals));
return a - b > -d && a - b < d;
} -
you do understand that floating point numbers are approximations, right? Never expect floating point math to represent an exact value. Always compare within a properly scaled tolerance range.
isEqual = abs(a-b) < tolerance;