I want do draw polylines with many control points in a PyQt4 / PySide application. The point coordinates come from a NumPy array and must be put into a QPolygonF in order to be drawn with QPainter.drawPolyline(...).
With PyQt4, this can be done efficiently e.g. with something like this:
import numpy as np
from PyQt4.QtGui import *
n = 3
qpoints = QPolygonF(n)
vptr = qpoints.data()
vptr.setsize(8*2*n)
aa = np.ndarray( shape=(n,2), dtype=np.float64, buffer=buffer(vptr))
aa.setflags(write=True)
aa[:,0] = np.arange(n)
aa[:,1] = np.arange(n)
for i in range(n):
    print qpoints.at(i)
This works, because, when using PyQt4, QPolygonF.data() returns something (a sip.voidptr object) which speaks the Python buffer protocol.
The problem now is that if I try to run the above code using PySide instead of PyQt4, QPolygonF.data() just returns a QPointF object (with the coordinates of the first point in the QPolygonF) and is thus useless.
So my question is: is there any known workaround to this? How can I, with PySide, put data into a QPolygonF without inserting QPointF objects, element-wise?
We can convert the Numpy array to the list by tolist() method, we can have a list of data element which is converted from an array using this method.
Here is an efficient way of writing a Numpy array into the memory block pointed by a QPolygonF object using PySide2: https://github.com/PierreRaybaut/PythonQwt/blob/master/qwt/plot_curve.py#L63 (See function "array2d_to_qpolygonf")
This is as efficient as with PyQt4 or PyQt5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With