Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I efficiently transfer data from a NumPy array to a QPolygonF when using PySide?

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?

like image 989
user3016636 Avatar asked Nov 21 '13 11:11

user3016636


People also ask

Is it possible to convert NumPy array to list in python?

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.


1 Answers

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.

like image 112
Pierre Raybaut Avatar answered Sep 30 '22 17:09

Pierre Raybaut