handle zero-sized QPolygonF

depending on the implementation, a zero-sized QPolygonF may not
have any underlying buffer allocated and may return a null pointer when
queried for its "data()"

this null pointer is returned to Python as a "None" which breaks code
not expecting it.
This commit is contained in:
KIU Shueng Chuan 2021-07-31 17:16:12 +08:00
parent 9913f7c1e7
commit 75654b8495
2 changed files with 19 additions and 12 deletions

View File

@ -2060,25 +2060,25 @@ def arrayToQPath(x, y, connect='all', finiteCheck=True):
def ndarray_from_qpolygonf(polyline):
nbytes = 2 * len(polyline) * 8
if QT_LIB == "PySide2":
buffer = Qt.shiboken2.VoidPtr(polyline.data(), nbytes, True)
elif QT_LIB == "PySide6":
buffer = Qt.shiboken6.VoidPtr(polyline.data(), nbytes, True)
else:
if QT_LIB.startswith('PyQt'):
buffer = polyline.data()
if buffer is None:
buffer = Qt.sip.voidptr(0)
buffer.setsize(nbytes)
else:
ptr = polyline.data()
if ptr is None:
ptr = 0
buffer = Qt.shiboken.VoidPtr(ptr, nbytes, True)
memory = np.frombuffer(buffer, np.double).reshape((-1, 2))
return memory
def create_qpolygonf(size):
if QtVersion.startswith("5"):
polyline = QtGui.QPolygonF(size)
polyline = QtGui.QPolygonF()
if QT_LIB.startswith('PyQt'):
polyline.fill(QtCore.QPointF(), size)
else:
polyline = QtGui.QPolygonF()
if QT_LIB == "PySide6":
polyline.resize(size)
else:
polyline.fill(QtCore.QPointF(), size)
polyline.resize(size)
return polyline
def arrayToQPolygonF(x, y):

View File

@ -333,3 +333,10 @@ def test_arrayToQPath(xs, ys, connect, expected):
continue
element = path.elementAt(i)
assert eq(expected[i], (element.type, element.x, element.y))
def test_ndarray_from_qpolygonf():
# test that we get an empty ndarray from an empty QPolygonF
poly = pg.functions.create_qpolygonf(0)
arr = pg.functions.ndarray_from_qpolygonf(poly)
assert isinstance(arr, np.ndarray)