AttributeError: 'QPoint' object has no attribute 'distanceToPoint'
时间: 2023-09-12 21:08:33 浏览: 93
This error occurs when you try to call the method `distanceToPoint` on a `QPoint` object, but this method does not exist for `QPoint`.
To calculate the distance between two points using `QPoint`, you can use the `manhattanLength` method for the Manhattan distance or calculate the Euclidean distance using the `math` module:
```
import math
point1 = QPoint(0, 0)
point2 = QPoint(3, 4)
# Calculate Manhattan distance
manhattan_distance = point1.manhattanLength(point2)
print(manhattan_distance) # Output: 7
# Calculate Euclidean distance
euclidean_distance = math.sqrt((point1.x()-point2.x())**2 + (point1.y()-point2.y())**2)
print(euclidean_distance) # Output: 5.0
```
Alternatively, you can convert the `QPoint` objects to `QPointF` objects and use the `distance` method:
```
point1 = QPointF(0, 0)
point2 = QPointF(3, 4)
distance = point1.distance(point2)
print(distance) # Output: 5.0
```
阅读全文