AttributeError: 'QPointF' object has no attribute 'distance'
时间: 2023-09-14 12:08:40 浏览: 137
This error occurs when you try to access the "distance" attribute of a QPointF object in PyQt5. However, QPointF objects do not have a "distance" attribute.
To calculate the distance between two QPointF objects, you can use the "pythagorean theorem" formula:
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)
Here's an example code snippet to calculate the distance between two QPointF objects:
```python
from PyQt5.QtCore import QPointF
import math
# create two QPointF objects
point1 = QPointF(0, 0)
point2 = QPointF(3, 4)
# calculate the distance between the two points
distance = math.sqrt((point2.x() - point1.x())**2 + (point2.y() - point1.y())**2)
print(distance) # output: 5.0
```
In this example, we import the QPointF class from PyQt5.QtCore, create two QPointF objects, and calculate the distance between them using the formula. Finally, we print the distance to the console.
阅读全文