mirror of
https://github.com/thp/pyotherside.git
synced 2025-01-17 23:22:53 +08:00
Add test case and documentation for bytes conversion
This commit is contained in:
parent
c67604ba4b
commit
395491f2cd
@ -411,38 +411,42 @@ PyOtherSide will automatically convert Python data types to Qt data types
|
||||
The following data types are supported and can be used to pass data
|
||||
between Python and QML (and vice versa):
|
||||
|
||||
+--------------------+------------+-----------------------------+
|
||||
| Python | QML | Remarks |
|
||||
+====================+============+=============================+
|
||||
| bool | bool | |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| int | int | |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| float | double | |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| str | string | |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| list | JS Array | JS Arrays are always |
|
||||
| | | converted to Python lists. |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| tuple | JS Array | |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| dict | JS Object | Keys must be strings |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| datetime.date | QML date | since PyOtherSide 1.2.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| datetime.time | QML time | since PyOtherSide 1.2.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| datetime.datetime | JS Date | since PyOtherSide 1.2.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| set | JS Array | since PyOtherSide 1.3.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| iterable | JS Array | since PyOtherSide 1.3.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| object | (opaque) | since PyOtherSide 1.4.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
| pyotherside.QObject| QObject | since PyOtherSide 1.4.0 |
|
||||
+--------------------+------------+-----------------------------+
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| Python | QML | Remarks |
|
||||
+====================+================+=============================+
|
||||
| bool | bool | |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| int | int | |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| float | double | |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| str | string | |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| list | JS Array | JS Arrays are always |
|
||||
| | | converted to Python lists. |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| tuple | JS Array | |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| dict | JS Object | Keys must be strings |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| datetime.date | QML date | since PyOtherSide 1.2.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| datetime.time | QML time | since PyOtherSide 1.2.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| datetime.datetime | JS Date | since PyOtherSide 1.2.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| set | JS Array | since PyOtherSide 1.3.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| iterable | JS Array | since PyOtherSide 1.3.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| object | (opaque) | since PyOtherSide 1.4.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| pyotherside.QObject| QObject | since PyOtherSide 1.4.0 |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
| bytes | JS ArrayBuffer | since PyOtherSide 1.5.6; |
|
||||
| | | requires Qt 5.8; the C++ |
|
||||
| | | data type is QByteArray |
|
||||
+--------------------+----------------+-----------------------------+
|
||||
|
||||
Trying to pass in other types than the ones listed here is undefined
|
||||
behavior and will usually result in an error.
|
||||
@ -1363,6 +1367,12 @@ Known Problems:
|
||||
ChangeLog
|
||||
=========
|
||||
|
||||
|
||||
Version 1.5.6 (UNRELEASED)
|
||||
--------------------------
|
||||
|
||||
* Add support for ``QByteArray``, JS ``ArrayBuffer`` and Python ``bytes`` conversion (by Igor Malinovskiy, PR#103)
|
||||
|
||||
Version 1.5.5 (2019-06-04)
|
||||
--------------------------
|
||||
|
||||
|
10
qtquicktests/tst_bytes.py
Normal file
10
qtquicktests/tst_bytes.py
Normal file
@ -0,0 +1,10 @@
|
||||
import struct
|
||||
|
||||
def get_bytes():
|
||||
return struct.pack('<bhi', 123, 456, 789)
|
||||
|
||||
def set_bytes(bytedata):
|
||||
print('Got this raw data from QML:', bytedata)
|
||||
a, b, c = struct.unpack('<hhI', bytedata)
|
||||
print('Got this data (unpacked) from QML:', a, b, hex(c))
|
||||
return a == 1337 and b == -4711 and c == 0xcafebabe
|
42
qtquicktests/tst_bytes.qml
Normal file
42
qtquicktests/tst_bytes.qml
Normal file
@ -0,0 +1,42 @@
|
||||
import QtQuick 2.0
|
||||
import io.thp.pyotherside 1.5
|
||||
import QtTest 1.2
|
||||
|
||||
TestCase {
|
||||
Python {
|
||||
id: py
|
||||
Component.onCompleted: {
|
||||
addImportPath(Qt.resolvedUrl('.'));
|
||||
importModule_sync('tst_bytes');
|
||||
}
|
||||
}
|
||||
|
||||
function test_bytes_from_python() {
|
||||
var buf = py.evaluate('tst_bytes.get_bytes()');
|
||||
var view = new DataView(buf);
|
||||
|
||||
var a = view.getInt8(0, true);
|
||||
var b = view.getInt16(1, true);
|
||||
var c = view.getInt32(3, true);
|
||||
|
||||
console.log('Data retrieved from Python (as string):', buf);
|
||||
console.log('First element decoded:', a);
|
||||
console.log('Second element decoded:', b);
|
||||
console.log('Third element decoded:', c);
|
||||
|
||||
compare(a, 123);
|
||||
compare(b, 456);
|
||||
compare(c, 789);
|
||||
}
|
||||
|
||||
function test_bytes_to_python() {
|
||||
var buf = new ArrayBuffer(8);
|
||||
var view = new DataView(buf);
|
||||
|
||||
view.setInt16(0, 1337, true);
|
||||
view.setInt16(2, -4711, true);
|
||||
view.setUInt32(4, 0xCAFEBABE, true);
|
||||
|
||||
compare(py.call_sync('tst_bytes.set_bytes', [buf]), true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user