[PySide] QByteArray.fromRawData from bytearray/numpy

icfwm at gmx.net icfwm at gmx.net
Fri Oct 16 21:05:19 CEST 2020


Hi,

I think this is possible the other way round. Construct a QByteArray,
cast it to memory_view and create the numpy array with
np.from_buffer(...). For example:

>>> import numpy as np
>>> from PySide2.QtCore import QByteArray
>>> a = QByteArray(20000, 0)
>>> npa = np.frombuffer(memoryview(a), dtype=np.uint8)
>>> npa.dtype
dtype('uint8')
>>> npa.size
20000
>>> npa[0]
0
>>> npa[0] = 5
>>> npa[0]
5
>>> a[0]
b'\x05'
>>> a[0] = b'x'
>>> a[0]
b'x'
>>> npa[0]
120
>>>

Best regards
Christoph

On 16.10.20 09:31, 許友綸 wrote:
> hi, QByteArray consturctor can take both bytes and bytearray as its
> argument
> The docs says it makes its own copy. I want a python buffer(numpy)
> changing in real-time to be plotted/processed in QML.
>
> However, fromRawData only takes bytes which is immutable type! Wish it
> can also be able to pass other buffer protocol types.
>
> appreciate your help, thank you
>
> -Tim
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> https://lists.qt-project.org/listinfo/pyside


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20201016/31b2559b/attachment.html>


More information about the PySide mailing list