I made the following test code for the QPainterPath::intersects test function. I wanted a way to detect if a point is in the vicinity (let's say 2 pixels) of a line stored in a QPainterPath.
path.moveTo(0, 0);
path.lineTo(0, -20);
path.lineTo(40, -20);
path.lineTo(40, -95);
int testX = 4;
int testY = -17;
QRectF rect
(testX
- 2, testY
- 2,
4,
4);
bool intersects = path.intersects(rect);
qDebug() << "Intersects?" << ((intersects) ? "true" : "false");
qDebug() << "Rect: x =" << rect.x() << "| y =" << rect.y() << "| w =" << rect.width() << "| h =" << rect.height();
QPainterPath path;
path.moveTo(0, 0);
path.lineTo(0, -20);
path.lineTo(40, -20);
path.lineTo(40, -95);
int testX = 4;
int testY = -17;
QRectF rect(testX - 2, testY - 2, 4, 4);
bool intersects = path.intersects(rect);
qDebug() << "Intersects?" << ((intersects) ? "true" : "false");
qDebug() << "Rect: x =" << rect.x() << "| y =" << rect.y() << "| w =" << rect.width() << "| h =" << rect.height();
To copy to clipboard, switch view to plain text mode
This produces the following output:
Intersects? true
Rect: x = 2 | y = -19 | w = 4 | h = 4
I'd expect the intersects function to return false in this case because no point in the rectangle overlaps with the drawn line. Am I using the function incorrectly?
Bookmarks