Fixes and tests.

This commit is contained in:
Mikaël Capelle
2022-05-02 18:56:44 +02:00
parent 6ea9d92b26
commit 30a01cf783
15 changed files with 308 additions and 51 deletions
+13 -5
View File
@@ -6,6 +6,9 @@
#include "pybind11_qt/details/pybind11_qt_utils.h"
// need to import containers to get QVariantList and QVariantMap
#include "pybind11_qt/pybind11_qt_containers.h"
namespace pybind11::detail {
template <class CharT>
@@ -83,13 +86,22 @@ namespace pybind11::detail {
bool type_caster<QVariant>::load(handle src, bool implicit)
{
if (PyList_Check(src.ptr())) {
// test for string first otherwise PyList_Check also works
if (PyBytes_Check(src.ptr()) || PyUnicode_Check(src.ptr())) {
value = src.cast<QString>();
return true;
}
else if (PySequence_Check(src.ptr())) {
// we could check if all the elements can be converted to QString
// and store a QStringList in the QVariant but I am not sure that is
// really useful.
value = src.cast<QVariantList>();
return true;
}
else if (PyMapping_Check(src.ptr())) {
value = src.cast<QVariantMap>();
return true;
}
else if (src == Py_None) {
value = QVariant();
return true;
@@ -98,10 +110,6 @@ namespace pybind11::detail {
value = src.cast<QVariantMap>();
return true;
}
else if (PyBytes_Check(src.ptr()) || PyUnicode_Check(src.ptr())) {
value = src.cast<QString>();
return true;
}
// PyBool will also return true for PyLong_Check but not the other way
// around, so the order here is relevant.
else if (PyBool_Check(src.ptr())) {