AttributeError: 'QScatterSeries' object has no attribute 'points'

AttributeError: 'QScatterSeries' object has no attribute 'points'

和标题差不多。我有一个 QScatterSeries object。我可以将数据加载到其中并将其添加到 QChart,但是如果我尝试使用类似...

from PyQt5.QtChart import QScatterSeries
from PyQt5.QtCore import QPointF

my_scatter_series.points = QScatterSeries()
my_scatter_series.points.append(QPointF(1.0,3.0))
my_scatter_series.points.append(QPointF(2.0,5.0))
my_scatter_series.points.append(QPointF(4.0,7.0))
point_list = my_scatter_series.points()

我得到一个 AttributeError...

AttributeError: 'QScatterSeries' object has no attribute 'points'

points() 绝对是 QXYSeries 中的一个方法,它是 QScatterSeries 的扩展,所以这应该是我能做的事情不是吗?

https://doc.qt.io/qt-5/qscatterseries-members.html

python 3.6.7

PyQt5 5.11.3

PyQtChart 5.11.3

类似的方法有2个:

这两个容器在概念上类似于列表,因此绑定(PyQt5/PySide2)必须选择使用其中的哪一个,如果更详细地查看文档,可以观察到:

QList QXYSeries::points() const

Returns the points in the series as a list. Use pointsVector() for better performance.

QVector QXYSeries::pointsVector() const

Returns the points in the series as a vector. This is more efficient than calling points().

结论是用pointsVector()最合适,所以解决方法是:

from PyQt5.QtChart import QScatterSeries
from PyQt5.QtCore import QPointF

my_scatter_series = QScatterSeries()
my_scatter_series.append(QPointF(1.0, 3.0))
my_scatter_series.append(QPointF(2.0, 5.0))
my_scatter_series.append(QPointF(4.0, 7.0))
point_list = my_scatter_series.pointsVector()
print(point_list)

输出:

[PyQt5.QtCore.QPointF(1.0, 3.0), PyQt5.QtCore.QPointF(2.0, 5.0), PyQt5.QtCore.QPointF(4.0, 7.0)]

我遇到了同样的问题。我的解决方案是从PyQt5切换到PySide2,然后问题就解决了。