|
48 | 48 | #include <climits>
|
49 | 49 | #include <limits>
|
50 | 50 |
|
| 51 | +#if QT_VERSION < 0x060000 |
| 52 | +#include <QStringRef> |
| 53 | + |
| 54 | +Q_DECLARE_METATYPE(QStringRef) |
| 55 | + |
| 56 | +int PythonQtConv::stringRefTypeId = 0; |
| 57 | +#else |
| 58 | +#include <QStringView> |
| 59 | +#include <QAnyStringView> |
| 60 | + |
| 61 | +int PythonQtConv::stringViewTypeId = 0; |
| 62 | +int PythonQtConv::anyStringViewTypeId = 0; |
| 63 | +#endif |
| 64 | + |
51 | 65 | QHash<int, PythonQtConvertMetaTypeToPythonCB*> PythonQtConv::_metaTypeToPythonConverters;
|
52 | 66 | QHash<int, PythonQtConvertPythonToMetaTypeCB*> PythonQtConv::_pythonToMetaTypeConverters;
|
53 | 67 |
|
@@ -633,19 +647,69 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
|
633 | 647 | // we have a exact enum type match:
|
634 | 648 | val = PyInt_AS_LONG(obj);
|
635 | 649 | ok = true;
|
636 |
| - } else if (!strict) { |
| 650 | + } |
| 651 | + else if (!strict) { |
637 | 652 | // we try to get any integer, when not being strict. If we are strict, integers are not wanted because
|
638 | 653 | // we want an integer overload to be taken first!
|
639 | 654 | val = (unsigned int)PyObjGetLongLong(obj, false, ok);
|
640 | 655 | }
|
641 | 656 | if (ok) {
|
642 |
| - PythonQtArgumentFrame_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,frame, unsigned int, val, ptr); |
| 657 | + PythonQtArgumentFrame_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject, frame, unsigned int, val, ptr); |
643 | 658 | return ptr;
|
644 |
| - } else { |
| 659 | + } |
| 660 | + else { |
645 | 661 | return nullptr;
|
646 | 662 | }
|
647 | 663 | }
|
648 | 664 |
|
| 665 | + // Handle QStringView et al, which need a reference to a persistent QString |
| 666 | +#if QT_VERSION < 0x060000 |
| 667 | + if (info.typeId == stringRefTypeId) { |
| 668 | + QString str = PyObjGetString(obj, strict, ok); |
| 669 | + if (ok) { |
| 670 | + void* ptr2 = nullptr; |
| 671 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(nullptr, frame, QVariant(str), ptr2); |
| 672 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(alreadyAllocatedCPPObject, frame, |
| 673 | + QVariant::fromValue(QStringRef((const QString*)((QVariant*)ptr2)->constData())), ptr); |
| 674 | + ptr = (void*)((QVariant*)ptr)->constData(); |
| 675 | + return ptr; |
| 676 | + } |
| 677 | + else { |
| 678 | + return nullptr; |
| 679 | + } |
| 680 | + } |
| 681 | +#else |
| 682 | + if (info.typeId == stringViewTypeId) { |
| 683 | + QString str = PyObjGetString(obj, strict, ok); |
| 684 | + if (ok) { |
| 685 | + void* ptr2 = nullptr; |
| 686 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(nullptr, frame, QVariant(str), ptr2); |
| 687 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(alreadyAllocatedCPPObject, frame, |
| 688 | + QVariant::fromValue(QStringView(*((const QString*)((QVariant*)ptr2)->constData()))), ptr); |
| 689 | + ptr = (void*)((QVariant*)ptr)->constData(); |
| 690 | + return ptr; |
| 691 | + } |
| 692 | + else { |
| 693 | + return nullptr; |
| 694 | + } |
| 695 | + } |
| 696 | + else if (info.typeId == anyStringViewTypeId) { |
| 697 | + // Handle QStringView et al, which need a reference to a persistent QString |
| 698 | + QString str = PyObjGetString(obj, strict, ok); |
| 699 | + if (ok) { |
| 700 | + void* ptr2 = nullptr; |
| 701 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(nullptr, frame, QVariant(str), ptr2); |
| 702 | + PythonQtArgumentFrame_ADD_VARIANT_VALUE_IF_NEEDED(alreadyAllocatedCPPObject, frame, |
| 703 | + QVariant::fromValue(QAnyStringView(*((const QString*)((QVariant*)ptr2)->constData()))), ptr); |
| 704 | + ptr = (void*)((QVariant*)ptr)->constData(); |
| 705 | + return ptr; |
| 706 | + } |
| 707 | + else { |
| 708 | + return nullptr; |
| 709 | + } |
| 710 | + } |
| 711 | +#endif |
| 712 | + |
649 | 713 | if (info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) {
|
650 | 714 | // check for QList<AnyPtr*> case, where we will use a QList<void*> QVariant
|
651 | 715 | if (info.isQList && (info.innerNamePointerCount == 1)) {
|
@@ -1498,6 +1562,16 @@ PyObject* PythonQtConv::convertFromStringRef(const void* inObject, int /*metaTyp
|
1498 | 1562 | {
|
1499 | 1563 | return PythonQtConv::QStringToPyObject(((QStringRef*)inObject)->toString());
|
1500 | 1564 | }
|
| 1565 | +#else |
| 1566 | +PyObject* PythonQtConv::convertFromStringView(const void* inObject, int /*metaTypeId*/) |
| 1567 | +{ |
| 1568 | + return PythonQtConv::QStringToPyObject(((QStringView*)inObject)->toString()); |
| 1569 | +} |
| 1570 | + |
| 1571 | +PyObject* PythonQtConv::convertFromAnyStringView(const void* inObject, int /*metaTypeId*/) |
| 1572 | +{ |
| 1573 | + return PythonQtConv::QStringToPyObject(((QAnyStringView*)inObject)->toString()); |
| 1574 | +} |
1501 | 1575 | #endif
|
1502 | 1576 |
|
1503 | 1577 | QByteArray PythonQtConv::getCPPTypeName(PyObject* type)
|
@@ -1550,6 +1624,19 @@ bool PythonQtConv::isStringType(PyTypeObject* type)
|
1550 | 1624 | #endif
|
1551 | 1625 | }
|
1552 | 1626 |
|
| 1627 | +void PythonQtConv::registerStringViewTypes() |
| 1628 | +{ |
| 1629 | +#if QT_VERSION < 0x060000 |
| 1630 | + stringRefTypeId = qRegisterMetaType<QStringRef>("QStringRef"); |
| 1631 | + PythonQtConv::registerMetaTypeToPythonConverter(stringRefTypeId, PythonQtConv::convertFromStringRef); |
| 1632 | +#else |
| 1633 | + stringViewTypeId = qRegisterMetaType<QStringView>("QStringView"); |
| 1634 | + PythonQtConv::registerMetaTypeToPythonConverter(stringViewTypeId, PythonQtConv::convertFromStringView); |
| 1635 | + anyStringViewTypeId = qRegisterMetaType<QAnyStringView>("QAnyStringView"); |
| 1636 | + PythonQtConv::registerMetaTypeToPythonConverter(anyStringViewTypeId, PythonQtConv::convertFromAnyStringView); |
| 1637 | +#endif |
| 1638 | +} |
| 1639 | + |
1553 | 1640 | PyObject* PythonQtConv::convertFromQListOfPythonQtObjectPtr(const void* inObject, int /*metaTypeId*/)
|
1554 | 1641 | {
|
1555 | 1642 | QList<PythonQtObjectPtr>& list = *((QList<PythonQtObjectPtr>*)inObject);
|
|
0 commit comments