@@ -57,9 +57,11 @@ int PythonQtConv::stringRefTypeId = 0;
57
57
#else
58
58
#include < QStringView>
59
59
#include < QAnyStringView>
60
+ #include < QByteArrayView>
60
61
61
62
int PythonQtConv::stringViewTypeId = 0 ;
62
63
int PythonQtConv::anyStringViewTypeId = 0 ;
64
+ int PythonQtConv::byteArrayViewTypeId = 0 ;
63
65
#endif
64
66
65
67
QHash<int , PythonQtConvertMetaTypeToPythonCB*> PythonQtConv::_metaTypeToPythonConverters;
@@ -399,6 +401,9 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
399
401
400
402
if (PyObject_TypeCheck (obj, &PythonQtInstanceWrapper_Type) &&
401
403
info.typeId != PythonQtMethodInfo::Variant &&
404
+ #if QT_VERSION >= 0x060000
405
+ info.typeId != byteArrayViewTypeId && // this case is handled later on
406
+ #endif
402
407
!PythonQt::priv ()->isPythonQtAnyObjectPtrMetaId (info.typeId )) {
403
408
// if we have a Qt wrapper object and if we do not need a QVariant, we do the following:
404
409
// (the Variant case is handled below in a switch)
@@ -597,13 +602,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
597
602
break ;
598
603
case QMetaType::QByteArray:
599
604
{
600
- QByteArray bytes = PyObjGetBytes (obj, strict, ok);
601
- if (!ok && !strict) {
602
- // since Qt uses QByteArray in many places for identifier strings,
603
- // we need to allow implicit conversion from unicode as well.
604
- // We allow that for both Python 2.x and 3.x to be compatible.
605
- bytes = PyObjGetString (obj, true , ok).toUtf8 ();
606
- }
605
+ QByteArray bytes = PyObjGetBytesAllowString (obj, strict, ok);
607
606
if (ok) {
608
607
PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED (alreadyAllocatedCPPObject,frame, QVariant (bytes), ptr);
609
608
ptr = (void *)((QVariant*)ptr)->constData ();
@@ -680,6 +679,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
680
679
}
681
680
#else
682
681
if (info.typeId == stringViewTypeId) {
682
+ // Handle QStringView, which needs a reference to a persistent QString
683
683
QString str = PyObjGetString (obj, strict, ok);
684
684
if (ok) {
685
685
void * ptr2 = nullptr ;
@@ -694,7 +694,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
694
694
}
695
695
}
696
696
else if (info.typeId == anyStringViewTypeId) {
697
- // Handle QStringView et al , which need a reference to a persistent QString
697
+ // Handle QAnyStringView , which needs a reference to a persistent QString
698
698
QString str = PyObjGetString (obj, strict, ok);
699
699
if (ok) {
700
700
void * ptr2 = nullptr ;
@@ -708,6 +708,21 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
708
708
return nullptr ;
709
709
}
710
710
}
711
+ else if (info.typeId == byteArrayViewTypeId) {
712
+ // Handle QByteArrayView, which needs a reference to a persistent QByteArray
713
+ QByteArray ba = PyObjGetBytesAllowString (obj, strict, ok);
714
+ if (ok) {
715
+ void * ptr2 = nullptr ;
716
+ PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED (nullptr , frame, QVariant (ba), ptr2);
717
+ PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED (alreadyAllocatedCPPObject, frame,
718
+ QVariant::fromValue (QByteArrayView (*((const QByteArray*)((QVariant*)ptr2)->constData ()))), ptr);
719
+ ptr = (void *)((QVariant*)ptr)->constData ();
720
+ return ptr;
721
+ }
722
+ else {
723
+ return nullptr ;
724
+ }
725
+ }
711
726
#endif
712
727
713
728
if (info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) {
@@ -847,6 +862,15 @@ QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok)
847
862
// TODO: support buffer objects in general
848
863
QByteArray r;
849
864
ok = true ;
865
+ if (PyObject_TypeCheck (val, &PythonQtInstanceWrapper_Type)) {
866
+ // check if we already have a QByteArray wrapper here
867
+ PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)val;
868
+ bool baOk;
869
+ QByteArray* baPtr = (QByteArray*)castWrapperTo (wrapper, " QByteArray" , baOk);
870
+ if (baOk && baPtr) {
871
+ return *baPtr;
872
+ }
873
+ }
850
874
if (PyBytes_Check (val)) {
851
875
r = QByteArray (PyBytes_AS_STRING (val), PyBytes_GET_SIZE (val));
852
876
} else {
@@ -855,6 +879,18 @@ QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok)
855
879
return r;
856
880
}
857
881
882
+ QByteArray PythonQtConv::PyObjGetBytesAllowString (PyObject* val, bool strict, bool & ok)
883
+ {
884
+ QByteArray bytes = PyObjGetBytes (val, strict, ok);
885
+ if (!ok && !strict) {
886
+ // since Qt uses QByteArray in many places for identifier strings,
887
+ // we need to allow implicit conversion from unicode as well.
888
+ // We allow that for both Python 2.x and 3.x to be compatible.
889
+ bytes = PyObjGetString (val, true , ok).toUtf8 ();
890
+ }
891
+ return bytes;
892
+ }
893
+
858
894
bool PythonQtConv::PyObjGetBool (PyObject* val, bool strict, bool &ok) {
859
895
bool d = false ;
860
896
ok = false ;
@@ -1560,17 +1596,23 @@ PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
1560
1596
#if QT_VERSION < 0x060000
1561
1597
PyObject* PythonQtConv::convertFromStringRef (const void * inObject, int /* metaTypeId*/ )
1562
1598
{
1563
- return PythonQtConv:: QStringToPyObject (((QStringRef*)inObject)->toString ());
1599
+ return QStringToPyObject (((QStringRef*)inObject)->toString ());
1564
1600
}
1565
1601
#else
1566
1602
PyObject* PythonQtConv::convertFromStringView (const void * inObject, int /* metaTypeId*/ )
1567
1603
{
1568
- return PythonQtConv:: QStringToPyObject (((QStringView*)inObject)->toString ());
1604
+ return QStringToPyObject (((QStringView*)inObject)->toString ());
1569
1605
}
1570
1606
1571
1607
PyObject* PythonQtConv::convertFromAnyStringView (const void * inObject, int /* metaTypeId*/ )
1572
1608
{
1573
- return PythonQtConv::QStringToPyObject (((QAnyStringView*)inObject)->toString ());
1609
+ return QStringToPyObject (((QAnyStringView*)inObject)->toString ());
1610
+ }
1611
+
1612
+ PyObject* PythonQtConv::convertFromByteArrayView (const void * inObject, int )
1613
+ {
1614
+ QByteArray ba = ((QByteArrayView*)inObject)->toByteArray ();
1615
+ return createCopyFromMetaType (QMetaType::QByteArray, &ba);
1574
1616
}
1575
1617
#endif
1576
1618
@@ -1634,6 +1676,8 @@ void PythonQtConv::registerStringViewTypes()
1634
1676
PythonQtConv::registerMetaTypeToPythonConverter (stringViewTypeId, PythonQtConv::convertFromStringView);
1635
1677
anyStringViewTypeId = qRegisterMetaType<QAnyStringView>(" QAnyStringView" );
1636
1678
PythonQtConv::registerMetaTypeToPythonConverter (anyStringViewTypeId, PythonQtConv::convertFromAnyStringView);
1679
+ byteArrayViewTypeId = qRegisterMetaType<QByteArrayView>(" QByteArrayView" );
1680
+ PythonQtConv::registerMetaTypeToPythonConverter (byteArrayViewTypeId, PythonQtConv::convertFromByteArrayView);
1637
1681
#endif
1638
1682
}
1639
1683
0 commit comments